bloboland

annotate src/renderer.cc @ 4:9021a906c5d3

lots of stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Dec 2012 06:13:09 +0200
parents a39c301cdcce
children 2f4406cc341e
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@4 34 blobtex->create(level->blobs.size());
nuclear@4 35 blobtex->set_filtering(GL_NEAREST);
nuclear@4 36
nuclear@3 37 this->level = level;
nuclear@1 38
nuclear@1 39 return true;
nuclear@1 40 }
nuclear@1 41
nuclear@1 42 void Renderer::shutdown()
nuclear@1 43 {
nuclear@1 44 delete leveltex;
nuclear@3 45 if(sdrprog) {
nuclear@3 46 free_program(sdrprog);
nuclear@3 47 }
nuclear@3 48 }
nuclear@3 49
nuclear@3 50 void Renderer::set_fov(float fov)
nuclear@3 51 {
nuclear@3 52 this->fov = fov;
nuclear@3 53 }
nuclear@3 54
nuclear@3 55 void Renderer::set_aspect(float aspect)
nuclear@3 56 {
nuclear@3 57 this->aspect = aspect;
nuclear@1 58 }
nuclear@1 59
nuclear@1 60 static void draw_cube(const Vector3 &pos, float sz);
nuclear@1 61
nuclear@3 62 void Renderer::render() const
nuclear@1 63 {
nuclear@3 64 leveltex->update((float*)level->terrain->get_data_ptr());
nuclear@4 65 ((Renderer*)this)->update_blobtex();
nuclear@1 66
nuclear@4 67 bind_texture(leveltex, 0);
nuclear@4 68 bind_texture(blobtex, 1);
nuclear@4 69
nuclear@4 70 set_uniform_float3(sdrprog, "worldsize", level->world_size.x, level->world_size.y,
nuclear@4 71 level->world_size.z);
nuclear@3 72 set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0),
nuclear@3 73 level->terrain->get_size(1), level->terrain->get_size(2));
nuclear@3 74 set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0);
nuclear@4 75
nuclear@4 76 set_uniform_int(sdrprog, "voltex", 0);
nuclear@4 77 set_uniform_int(sdrprog, "blobtex", 1);
nuclear@3 78 bind_program(sdrprog);
nuclear@3 79
nuclear@3 80 glBegin(GL_QUADS);
nuclear@3 81 glTexCoord2f(0, 0);
nuclear@3 82 glVertex2f(-1, -1);
nuclear@3 83 glTexCoord2f(1, 0);
nuclear@3 84 glVertex2f(1, -1);
nuclear@3 85 glTexCoord2f(1, 1);
nuclear@3 86 glVertex2f(1, 1);
nuclear@3 87 glTexCoord2f(0, 1);
nuclear@3 88 glVertex2f(-1, 1);
nuclear@3 89 glEnd();
nuclear@3 90
nuclear@3 91 bind_program(0);
nuclear@4 92 bind_texture(0, 1);
nuclear@4 93 bind_texture(0, 0);
nuclear@3 94
nuclear@3 95 /*glEnable(GL_COLOR_MATERIAL);
nuclear@1 96 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
nuclear@1 97
nuclear@1 98 glBegin(GL_QUADS);
nuclear@1 99 for(int i=0; i<vol->get_size(0); i++) {
nuclear@1 100 for(int j=0; j<vol->get_size(1); j++) {
nuclear@1 101 for(int k=0; k<vol->get_size(2) - 1; k++) {
nuclear@1 102 Vector4 vox = vol->get_voxel(i, j, k);
nuclear@1 103 Vector4 next = vol->get_voxel(i, j, k + 1);
nuclear@1 104
nuclear@1 105 if(vox.w > 0.1 && next.w < 0.1) {
nuclear@1 106 glColor3f(vox.x, vox.y, vox.z);
nuclear@1 107
nuclear@1 108 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
nuclear@1 109 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
nuclear@1 110 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
nuclear@1 111 draw_cube(Vector3(x, y, z), 0.1);
nuclear@1 112 }
nuclear@1 113 }
nuclear@1 114 }
nuclear@1 115 }
nuclear@1 116 glEnd();
nuclear@1 117
nuclear@1 118 glDisable(GL_COLOR_MATERIAL);
nuclear@3 119 */
nuclear@1 120 }
nuclear@1 121
nuclear@1 122
nuclear@1 123 static void draw_cube(const Vector3 &pos, float sz)
nuclear@1 124 {
nuclear@1 125 sz /= 2.0;
nuclear@1 126
nuclear@1 127 glNormal3f(0, 0, 1);
nuclear@1 128 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
nuclear@1 129 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
nuclear@1 130 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
nuclear@1 131 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
nuclear@1 132
nuclear@1 133 glNormal3f(1, 0, 0);
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(0, 0, -1);
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(-1, 0, 0);
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(0, 1, 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 }
nuclear@4 164
nuclear@4 165 void Renderer::update_blobtex()
nuclear@4 166 {
nuclear@4 167 int nblobs = (int)level->blobs.size();
nuclear@4 168
nuclear@4 169 float *data = (float*)alloca(nblobs * 4 * sizeof *data);
nuclear@4 170 float *dptr = data;
nuclear@4 171
nuclear@4 172 for(int i=0; i<nblobs; i++) {
nuclear@4 173 *dptr++ = level->blobs[i].pos.x;
nuclear@4 174 *dptr++ = level->blobs[i].pos.y;
nuclear@4 175 *dptr++ = level->blobs[i].pos.z;
nuclear@4 176 *dptr++ = 1.0;
nuclear@4 177 }
nuclear@4 178
nuclear@4 179 blobtex->update(data);
nuclear@4 180 }