]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
53a3f45237463e5fffed3e084805326453e5fd3a
[libs/gl.git] / source / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "clipping.h"
5 #include "error.h"
6 #include "lighting.h"
7 #include "material.h"
8 #include "mesh.h"
9 #include "program.h"
10 #include "programdata.h"
11 #include "renderable.h"
12 #include "renderer.h"
13 #include "texture.h"
14 #include "texturing.h"
15 #include "texunit.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
19
20 using namespace std;
21
22 namespace Msp {
23 namespace GL {
24
25 Renderer::Renderer(const Camera *c):
26         default_camera(c),
27         changed(0),
28         matrices_loaded(false),
29         state_stack(1)
30 {
31         state_stack.reserve(16);
32         shdata_stack.reserve(32);
33         state = &state_stack.back();
34
35         if(c)
36                 set_camera(*c);
37         else
38         {
39                 standard_shdata.uniform("projection_matrix", Matrix());
40                 standard_shdata.uniform("eye_world_matrix", Matrix());
41         }
42 }
43
44 Renderer::~Renderer()
45 {
46         end();
47 }
48
49 void Renderer::begin(const Camera *c)
50 {
51         end();
52         if(c)
53                 set_camera(*c);
54 }
55
56 void Renderer::set_camera(const Camera &c)
57 {
58         state->camera = &c;
59         standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
60         standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
61         changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
62         set_matrix(state->camera->get_view_matrix());
63 }
64
65 void Renderer::set_matrix(const Matrix &matrix)
66 {
67         state->modelview_matrix = matrix;
68         changed |= MATRIX;
69 }
70
71 void Renderer::transform(const Matrix &matrix)
72 {
73         state->modelview_matrix *= matrix;
74         changed |= MATRIX;
75 }
76
77 void Renderer::set_texture(const Texture *t)
78 {
79         state->texture = t;
80         state->texturing = 0;
81 }
82
83 void Renderer::set_texturing(const Texturing *t)
84 {
85         state->texturing = t;
86         state->texture = 0;
87 }
88
89 unsigned Renderer::allocate_effect_texunit()
90 {
91         return --state->lowest_effect_texunit;
92 }
93
94 void Renderer::set_material(const Material *m)
95 {
96         state->material = m;
97         changed |= MATERIAL_SHDATA;
98 }
99
100 void Renderer::set_lighting(const Lighting *l)
101 {
102         state->lighting = l;
103         state->lighting_matrix = state->modelview_matrix;
104         if(l)
105         {
106                 l->update_shader_data(standard_shdata, state->lighting_matrix);
107                 changed |= STANDARD_SHDATA;
108         }
109         changed |= LEGACY_LIGHTING;
110 }
111
112 void Renderer::set_clipping(const Clipping *c)
113 {
114         state->clipping = c;
115         state->clipping_matrix = state->modelview_matrix;
116         if(c)
117         {
118                 c->update_shader_data(standard_shdata, state->clipping_matrix);
119                 changed |= STANDARD_SHDATA;
120         }
121         changed |= LEGACY_CLIPPING;
122 }
123
124 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
125 {
126         state->shprog = p;
127         if(p && d)
128                 add_shader_data(*d);
129 }
130
131 void Renderer::add_shader_data(const ProgramData &d)
132 {
133         if(state->shdata_count<shdata_stack.size() && shdata_stack[state->shdata_count]==&d)
134                 ++state->shdata_count;
135         else
136         {
137                 if(shdata_stack.size()>state->shdata_count)
138                         shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
139                 shdata_stack.push_back(&d);
140                 state->shdata_count = shdata_stack.size();
141                 changed |= SHADER_DATA;
142         }
143 }
144
145 void Renderer::set_mesh(const Mesh *m)
146 {
147         state->mesh = m;
148 }
149
150 void Renderer::set_vertex_setup(const VertexSetup *vs)
151 {
152         state->vertex_setup = vs;
153 }
154
155 void Renderer::set_winding_test(const WindingTest *w)
156 {
157         state->winding_test = w;
158 }
159
160 void Renderer::set_reverse_winding(bool r)
161 {
162         state->reverse_winding = r;
163 }
164
165 void Renderer::push_state()
166 {
167         state_stack.push_back(state_stack.back());
168         state = &state_stack.back();
169 }
170
171 void Renderer::pop_state()
172 {
173         if(state_stack.size()==1)
174                 throw stack_underflow("Renderer::pop_state");
175
176         const Camera *old_camera = state->camera;
177         const Lighting *old_lighting = state->lighting;
178         const Clipping *old_clipping = state->clipping;
179         state_stack.pop_back();
180         state = &state_stack.back();
181         changed |= MATRIX;
182         bool camera_changed = (state->camera!=old_camera);
183         if(camera_changed)
184         {
185                 if(state->camera)
186                 {
187                         standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
188                         standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
189                 }
190                 else
191                 {
192                         standard_shdata.uniform("projection_matrix", Matrix());
193                         standard_shdata.uniform("eye_world_matrix", Matrix());
194                 }
195                 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
196         }
197         /* This actually should compare the relevant matrices rather than check for
198         camera, but in practice lighting and clipping is set right after the camera
199         and a boolean check is much faster than matrix comparison. */
200         if(state->lighting!=old_lighting || camera_changed)
201         {
202                 if(state->lighting)
203                 {
204                         state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
205                         changed |= STANDARD_SHDATA;
206                 }
207                 changed |= LEGACY_LIGHTING;
208         }
209         if(state->clipping!=old_clipping || camera_changed)
210         {
211                 if(state->clipping)
212                 {
213                         state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
214                         changed |= STANDARD_SHDATA;
215                 }
216                 changed |= LEGACY_CLIPPING;
217         }
218 }
219
220 void Renderer::end()
221 {
222         if(state_stack.size()>1)
223                 throw invalid_operation("Renderer::end");
224
225         *state = State();
226         if(default_camera)
227                 set_camera(*default_camera);
228         else
229                 standard_shdata.uniform("projection_matrix", Matrix());
230         shdata_stack.clear();
231         excluded.clear();
232
233         Mesh::unbind();
234         Texturing::unbind();
235         Texture::unbind_from(0);
236         Material::unbind();
237         Lighting::unbind();
238         Clipping::unbind();
239         Program::unbind();
240         VertexSetup::unbind();
241         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
242         WindingTest::unbind();
243
244         if(matrices_loaded)
245         {
246                 MatrixStack::projection().pop();
247                 MatrixStack::modelview().pop();
248                 matrices_loaded = false;
249         }
250 }
251
252 void Renderer::exclude(const Renderable &renderable)
253 {
254         excluded.insert(&renderable);
255 }
256
257 void Renderer::include(const Renderable &renderable)
258 {
259         excluded.erase(&renderable);
260 }
261
262 void Renderer::render(const Renderable &renderable, const Tag &tag)
263 {
264         if(!excluded.count(&renderable))
265                 renderable.render(*this, tag);
266 }
267
268 void Renderer::draw(const Batch &batch)
269 {
270         apply_state();
271
272         batch.draw();
273 }
274
275 void Renderer::apply_state()
276 {
277         /* We (mostly) let the objects themselves figure out if the binding has
278         changed */
279
280         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
281         bool legacy_textures = !state->shprog;
282
283         if(state->texturing)
284                 state->texturing->bind(legacy_textures);
285         else
286         {
287                 Texturing::unbind();
288                 if(state->texture)
289                         state->texture->bind_to(0, legacy_textures);
290                 else
291                         Texture::unbind_from(0);
292         }
293
294         if(legacy_bindings)
295         {
296                 if(state->material)
297                         state->material->bind();
298                 else
299                         Material::unbind();
300
301                 if(changed&LEGACY_LIGHTING)
302                 {
303                         if(state->lighting)
304                         {
305                                 MatrixStack::modelview() = state->lighting_matrix;
306                                 state->lighting->bind();
307                                 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
308                         }
309                         else
310                                 Lighting::unbind();
311                 }
312         }
313
314         if(state->clipping)
315         {
316                 if(legacy_bindings)
317                 {
318                         if(changed&LEGACY_CLIPPING)
319                         {
320                                 MatrixStack::modelview() = state->clipping_matrix;
321                                 state->clipping->bind(true);
322                                 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
323                         }
324                 }
325                 else
326                         state->clipping->bind(false);
327         }
328         else
329                 Clipping::unbind();
330
331         if(state->shprog)
332         {
333                 bool shprog_changed = (state->shprog!=Program::current());
334                 state->shprog->bind();
335
336                 if(!legacy_bindings)
337                 {
338                         if(changed&MODERN_MATRIX)
339                         {
340                                 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
341                                 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
342                                 nm = transpose(invert(nm));
343                                 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
344                                 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
345                         }
346
347                         if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
348                         {
349                                 state->material->get_shader_data().apply();
350                                 changed &= ~MATERIAL_SHDATA;
351                         }
352
353                         if((changed&STANDARD_SHDATA) || shprog_changed)
354                         {
355                                 standard_shdata.apply();
356                                 changed &= ~STANDARD_SHDATA;
357                         }
358                 }
359
360                 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
361
362                 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
363                 {
364                         if(extra_shdata)
365                                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
366                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
367                                 (*i)->apply();
368                         changed &= ~SHADER_DATA;
369                 }
370         }
371         else
372                 Program::unbind();
373
374         if(state->mesh)
375         {
376                 if(legacy_bindings)
377                 {
378                         Mesh::unbind();
379                         state->mesh->get_vertices().apply();
380                         if(const Buffer *ibuf = state->mesh->get_index_buffer())
381                                 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
382                         else
383                                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
384                 }
385                 else
386                         state->mesh->bind();
387         }
388         else
389         {
390                 Mesh::unbind();
391
392                 if(state->vertex_setup)
393                         state->vertex_setup->bind();
394                 else
395                         VertexSetup::unbind();
396         }
397
398         if(state->winding_test)
399         {
400                 if(state->reverse_winding)
401                         state->winding_test->get_reverse().bind();
402                 else
403                         state->winding_test->bind();
404         }
405         else
406                 WindingTest::unbind();
407
408         if(legacy_bindings)
409         {
410                 if(!matrices_loaded)
411                 {
412                         MatrixStack::modelview().push();
413                         MatrixStack::projection().push();
414                         matrices_loaded = true;
415                 }
416
417                 if(changed&LEGACY_PROJECTION)
418                 {
419                         MatrixStack::projection() = state->camera->get_projection_matrix();
420                         changed &= ~LEGACY_PROJECTION;
421                 }
422
423                 if(changed&LEGACY_MATRIX)
424                 {
425                         MatrixStack::modelview() = state->modelview_matrix;
426                         changed &= ~LEGACY_MATRIX;
427                 }
428         }
429 }
430
431
432 Renderer::State::State():
433         camera(0),
434         texture(0),
435         texturing(0),
436         lowest_effect_texunit(TexUnit::get_n_units()),
437         material(0),
438         lighting(0),
439         clipping(0),
440         shprog(0),
441         shdata_count(0),
442         mesh(0),
443         vertex_setup(0),
444         winding_test(0),
445         reverse_winding(false)
446 { }
447
448 } // namespace GL
449 } // namespace Msp