bloboland

view 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
line source
1 #include "opengl.h"
2 #include "renderer.h"
3 #include "sdr.h"
5 Renderer::Renderer()
6 {
7 blobtex = 0;
8 leveltex = 0;
9 sdrprog = 0;
11 level = 0;
13 fov = M_PI / 4.0;
14 aspect = 1.0;
15 }
17 Renderer::~Renderer()
18 {
19 shutdown();
20 }
22 bool Renderer::init(Level *level)
23 {
24 if(!(sdrprog = create_program_load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl"))) {
25 return false;
26 }
28 Volume *vol = level->terrain;
30 leveltex = new Texture3D;
31 leveltex->create(vol->get_size(0), vol->get_size(1), vol->get_size(2));
33 blobtex = new Texture1D;
34 blobtex->create(level->blobs.size());
35 blobtex->set_filtering(GL_NEAREST);
37 this->level = level;
39 return true;
40 }
42 void Renderer::shutdown()
43 {
44 delete leveltex;
45 if(sdrprog) {
46 free_program(sdrprog);
47 }
48 }
50 void Renderer::set_fov(float fov)
51 {
52 this->fov = fov;
53 }
55 void Renderer::set_aspect(float aspect)
56 {
57 this->aspect = aspect;
58 }
60 static void draw_cube(const Vector3 &pos, float sz);
62 void Renderer::render() const
63 {
64 leveltex->update((float*)level->terrain->get_data_ptr());
65 ((Renderer*)this)->update_blobtex();
67 bind_texture(leveltex, 0);
68 bind_texture(blobtex, 1);
70 set_uniform_float3(sdrprog, "worldsize", level->world_size.x, level->world_size.y,
71 level->world_size.z);
72 set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0),
73 level->terrain->get_size(1), level->terrain->get_size(2));
74 set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0);
76 set_uniform_int(sdrprog, "voltex", 0);
77 set_uniform_int(sdrprog, "blobtex", 1);
78 bind_program(sdrprog);
80 glBegin(GL_QUADS);
81 glTexCoord2f(0, 0);
82 glVertex2f(-1, -1);
83 glTexCoord2f(1, 0);
84 glVertex2f(1, -1);
85 glTexCoord2f(1, 1);
86 glVertex2f(1, 1);
87 glTexCoord2f(0, 1);
88 glVertex2f(-1, 1);
89 glEnd();
91 bind_program(0);
92 bind_texture(0, 1);
93 bind_texture(0, 0);
95 /*glEnable(GL_COLOR_MATERIAL);
96 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
98 glBegin(GL_QUADS);
99 for(int i=0; i<vol->get_size(0); i++) {
100 for(int j=0; j<vol->get_size(1); j++) {
101 for(int k=0; k<vol->get_size(2) - 1; k++) {
102 Vector4 vox = vol->get_voxel(i, j, k);
103 Vector4 next = vol->get_voxel(i, j, k + 1);
105 if(vox.w > 0.1 && next.w < 0.1) {
106 glColor3f(vox.x, vox.y, vox.z);
108 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
109 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
110 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
111 draw_cube(Vector3(x, y, z), 0.1);
112 }
113 }
114 }
115 }
116 glEnd();
118 glDisable(GL_COLOR_MATERIAL);
119 */
120 }
123 static void draw_cube(const Vector3 &pos, float sz)
124 {
125 sz /= 2.0;
127 glNormal3f(0, 0, 1);
128 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
129 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
130 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
131 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
133 glNormal3f(1, 0, 0);
134 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
135 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
136 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
137 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
139 glNormal3f(0, 0, -1);
140 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
141 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
142 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
143 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
145 glNormal3f(-1, 0, 0);
146 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
147 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
148 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
149 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
151 glNormal3f(0, 1, 0);
152 glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
153 glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
154 glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
155 glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
157 glNormal3f(0, -1, 0);
158 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
159 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
160 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
161 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
163 }
165 void Renderer::update_blobtex()
166 {
167 int nblobs = (int)level->blobs.size();
169 float *data = (float*)alloca(nblobs * 4 * sizeof *data);
170 float *dptr = data;
172 for(int i=0; i<nblobs; i++) {
173 *dptr++ = level->blobs[i].pos.x;
174 *dptr++ = level->blobs[i].pos.y;
175 *dptr++ = level->blobs[i].pos.z;
176 *dptr++ = 1.0;
177 }
179 blobtex->update(data);
180 }