bloboland
view 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 |
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->set_pixel_format(GL_RGBA16F); // floating point textures
35 blobtex->create(level->blobs.size());
36 blobtex->set_filtering(GL_NEAREST);
38 this->level = level;
40 return true;
41 }
43 void Renderer::shutdown()
44 {
45 delete leveltex;
46 if(sdrprog) {
47 free_program(sdrprog);
48 }
49 }
51 void Renderer::set_fov(float fov)
52 {
53 this->fov = fov;
54 }
56 void Renderer::set_aspect(float aspect)
57 {
58 this->aspect = aspect;
59 }
61 static void draw_cube(const Vector3 &pos, float sz);
63 void Renderer::prepare()
64 {
65 leveltex->update((float*)level->terrain->get_data_ptr());
66 update_blobtex();
67 }
69 void Renderer::render() const
70 {
71 bind_texture(leveltex, 0);
72 bind_texture(blobtex, 1);
74 set_uniform_float3(sdrprog, "worldsize", level->world_size.x, level->world_size.y,
75 level->world_size.z);
76 set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0),
77 level->terrain->get_size(1), level->terrain->get_size(2));
78 set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0);
80 set_uniform_int(sdrprog, "num_blobs", (int)level->blobs.size());
82 set_uniform_int(sdrprog, "voltex", 0);
83 set_uniform_int(sdrprog, "blobtex", 1);
84 bind_program(sdrprog);
86 glBegin(GL_QUADS);
87 glTexCoord2f(0, 0);
88 glVertex2f(-1, -1);
89 glTexCoord2f(1, 0);
90 glVertex2f(1, -1);
91 glTexCoord2f(1, 1);
92 glVertex2f(1, 1);
93 glTexCoord2f(0, 1);
94 glVertex2f(-1, 1);
95 glEnd();
97 bind_program(0);
98 bind_texture(0, 1);
99 bind_texture(0, 0);
101 /*glEnable(GL_COLOR_MATERIAL);
102 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
104 glBegin(GL_QUADS);
105 for(int i=0; i<vol->get_size(0); i++) {
106 for(int j=0; j<vol->get_size(1); j++) {
107 for(int k=0; k<vol->get_size(2) - 1; k++) {
108 Vector4 vox = vol->get_voxel(i, j, k);
109 Vector4 next = vol->get_voxel(i, j, k + 1);
111 if(vox.w > 0.1 && next.w < 0.1) {
112 glColor3f(vox.x, vox.y, vox.z);
114 float x = ((float)i - vol->get_size(0) / 2) * 0.1;
115 float z = ((float)j - vol->get_size(1) / 2) * 0.1;
116 float y = ((float)k - vol->get_size(2) / 2) * 0.1;
117 draw_cube(Vector3(x, y, z), 0.1);
118 }
119 }
120 }
121 }
122 glEnd();
124 glDisable(GL_COLOR_MATERIAL);
125 */
126 }
129 static void draw_cube(const Vector3 &pos, float sz)
130 {
131 sz /= 2.0;
133 glNormal3f(0, 0, 1);
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(1, 0, 0);
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(0, 0, -1);
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(-1, 0, 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 glNormal3f(0, -1, 0);
164 glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
165 glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
166 glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
167 glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
169 }
171 void Renderer::update_blobtex()
172 {
173 int nblobs = (int)level->blobs.size();
175 float *data = (float*)alloca(nblobs * 4 * sizeof *data);
176 float *dptr = data;
178 for(int i=0; i<nblobs; i++) {
179 *dptr++ = level->blobs[i].pos.x;
180 *dptr++ = level->blobs[i].pos.y;
181 *dptr++ = level->blobs[i].pos.z;
182 *dptr++ = 1.0;
183 }
185 blobtex->update(data);
186 }