bloboland

annotate src/renderer.cc @ 5:2f4406cc341e

meh
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Dec 2012 02:37:20 +0200
parents 9021a906c5d3
children
rev   line source
nuclear@1 1 #include "opengl.h"
nuclear@1 2 #include "renderer.h"
nuclear@3 3 #include "sdr.h"
nuclear@1 4
nuclear@1 5 Renderer::Renderer()
nuclear@1 6 {
nuclear@4 7 blobtex = 0;
nuclear@1 8 leveltex = 0;
nuclear@1 9 sdrprog = 0;
nuclear@3 10
nuclear@3 11 level = 0;
nuclear@3 12
nuclear@3 13 fov = M_PI / 4.0;
nuclear@3 14 aspect = 1.0;
nuclear@1 15 }
nuclear@1 16
nuclear@1 17 Renderer::~Renderer()
nuclear@1 18 {
nuclear@1 19 shutdown();
nuclear@1 20 }
nuclear@1 21
nuclear@3 22 bool Renderer::init(Level *level)
nuclear@1 23 {
nuclear@3 24 if(!(sdrprog = create_program_load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl"))) {
nuclear@1 25 return false;
nuclear@3 26 }
nuclear@3 27
nuclear@3 28 Volume *vol = level->terrain;
nuclear@1 29
nuclear@1 30 leveltex = new Texture3D;
nuclear@3 31 leveltex->create(vol->get_size(0), vol->get_size(1), vol->get_size(2));
nuclear@3 32
nuclear@4 33 blobtex = new Texture1D;
nuclear@5 34 blobtex->set_pixel_format(GL_RGBA16F); // floating point textures
nuclear@4 35 blobtex->create(level->blobs.size());
nuclear@4 36 blobtex->set_filtering(GL_NEAREST);
nuclear@4 37
nuclear@3 38 this->level = level;
nuclear@1 39
nuclear@1 40 return true;
nuclear@1 41 }
nuclear@1 42
nuclear@1 43 void Renderer::shutdown()
nuclear@1 44 {
nuclear@1 45 delete leveltex;
nuclear@3 46 if(sdrprog) {
nuclear@3 47 free_program(sdrprog);
nuclear@3 48 }
nuclear@3 49 }
nuclear@3 50
nuclear@3 51 void Renderer::set_fov(float fov)
nuclear@3 52 {
nuclear@3 53 this->fov = fov;
nuclear@3 54 }
nuclear@3 55
nuclear@3 56 void Renderer::set_aspect(float aspect)
nuclear@3 57 {
nuclear@3 58 this->aspect = aspect;
nuclear@1 59 }
nuclear@1 60
nuclear@1 61 static void draw_cube(const Vector3 &pos, float sz);
nuclear@1 62
nuclear@5 63 void Renderer::prepare()
nuclear@5 64 {
nuclear@5 65 leveltex->update((float*)level->terrain->get_data_ptr());
nuclear@5 66 update_blobtex();
nuclear@5 67 }
nuclear@5 68
nuclear@3 69 void Renderer::render() const
nuclear@1 70 {
nuclear@4 71 bind_texture(leveltex, 0);
nuclear@4 72 bind_texture(blobtex, 1);
nuclear@4 73
nuclear@4 74 set_uniform_float3(sdrprog, "worldsize", level->world_size.x, level->world_size.y,
nuclear@4 75 level->world_size.z);
nuclear@3 76 set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0),
nuclear@3 77 level->terrain->get_size(1), level->terrain->get_size(2));
nuclear@3 78 set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0);
nuclear@4 79
nuclear@5 80 set_uniform_int(sdrprog, "num_blobs", (int)level->blobs.size());
nuclear@5 81
nuclear@4 82 set_uniform_int(sdrprog, "voltex", 0);
nuclear@4 83 set_uniform_int(sdrprog, "blobtex", 1);
nuclear@3 84 bind_program(sdrprog);
nuclear@3 85
nuclear@3 86 glBegin(GL_QUADS);
nuclear@3 87 glTexCoord2f(0, 0);
nuclear@3 88 glVertex2f(-1, -1);
nuclear@3 89 glTexCoord2f(1, 0);
nuclear@3 90 glVertex2f(1, -1);
nuclear@3 91 glTexCoord2f(1, 1);
nuclear@3 92 glVertex2f(1, 1);
nuclear@3 93 glTexCoord2f(0, 1);
nuclear@3 94 glVertex2f(-1, 1);
nuclear@3 95 glEnd();
nuclear@3 96
nuclear@3 97 bind_program(0);
nuclear@4 98 bind_texture(0, 1);
nuclear@4 99 bind_texture(0, 0);
nuclear@3 100
nuclear@3 101 /*glEnable(GL_COLOR_MATERIAL);
nuclear@1 102 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
nuclear@1 103
nuclear@1 104 glBegin(GL_QUADS);
nuclear@1 105 for(int i=0; i<vol->get_size(0); i++) {
nuclear@1 106 for(int j=0; j<vol->get_size(1); j++) {
nuclear@1 107 for(int k=0; k<vol->get_size(2) - 1; k++) {
nuclear@1 108 Vector4 vox = vol->get_voxel(i, j, k);
nuclear@1 109 Vector4 next = vol->get_voxel(i, j, k + 1);
nuclear@1 110
nuclear@1 111 if(vox.w > 0.1 && next.w < 0.1) {
nuclear@1 112 glColor3f(vox.x, vox.y, vox.z);
nuclear@1 113
nuclear@1 114 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
nuclear@1 115 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
nuclear@1 116 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
nuclear@1 117 draw_cube(Vector3(x, y, z), 0.1);
nuclear@1 118 }
nuclear@1 119 }
nuclear@1 120 }
nuclear@1 121 }
nuclear@1 122 glEnd();
nuclear@1 123
nuclear@1 124 glDisable(GL_COLOR_MATERIAL);
nuclear@3 125 */
nuclear@1 126 }
nuclear@1 127
nuclear@1 128
nuclear@1 129 static void draw_cube(const Vector3 &pos, float sz)
nuclear@1 130 {
nuclear@1 131 sz /= 2.0;
nuclear@1 132
nuclear@1 133 glNormal3f(0, 0, 1);
nuclear@1 134 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
nuclear@1 135 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
nuclear@1 136 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
nuclear@1 137 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
nuclear@1 138
nuclear@1 139 glNormal3f(1, 0, 0);
nuclear@1 140 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
nuclear@1 141 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
nuclear@1 142 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
nuclear@1 143 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
nuclear@1 144
nuclear@1 145 glNormal3f(0, 0, -1);
nuclear@1 146 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
nuclear@1 147 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
nuclear@1 148 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
nuclear@1 149 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
nuclear@1 150
nuclear@1 151 glNormal3f(-1, 0, 0);
nuclear@1 152 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
nuclear@1 153 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
nuclear@1 154 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
nuclear@1 155 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
nuclear@1 156
nuclear@1 157 glNormal3f(0, 1, 0);
nuclear@1 158 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
nuclear@1 159 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
nuclear@1 160 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
nuclear@1 161 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
nuclear@1 162
nuclear@1 163 glNormal3f(0, -1, 0);
nuclear@1 164 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
nuclear@1 165 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
nuclear@1 166 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
nuclear@1 167 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
nuclear@1 168
nuclear@1 169 }
nuclear@4 170
nuclear@4 171 void Renderer::update_blobtex()
nuclear@4 172 {
nuclear@4 173 int nblobs = (int)level->blobs.size();
nuclear@4 174
nuclear@4 175 float *data = (float*)alloca(nblobs * 4 * sizeof *data);
nuclear@4 176 float *dptr = data;
nuclear@4 177
nuclear@4 178 for(int i=0; i<nblobs; i++) {
nuclear@4 179 *dptr++ = level->blobs[i].pos.x;
nuclear@4 180 *dptr++ = level->blobs[i].pos.y;
nuclear@4 181 *dptr++ = level->blobs[i].pos.z;
nuclear@4 182 *dptr++ = 1.0;
nuclear@4 183 }
nuclear@4 184
nuclear@4 185 blobtex->update(data);
nuclear@4 186 }