]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Appease the compiler with some extra copy-ctor's and operator='s
[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                 flush_shader_data();
138                 shdata_stack.push_back(&d);
139                 state->shdata_count = shdata_stack.size();
140                 changed |= SHADER_DATA;
141         }
142 }
143
144 void Renderer::flush_shader_data()
145 {
146         if(shdata_stack.size()>state->shdata_count)
147                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
148 }
149
150 void Renderer::set_mesh(const Mesh *m)
151 {
152         state->mesh = m;
153 }
154
155 void Renderer::set_vertex_setup(const VertexSetup *vs)
156 {
157         state->vertex_setup = vs;
158 }
159
160 void Renderer::set_winding_test(const WindingTest *w)
161 {
162         state->winding_test = w;
163 }
164
165 void Renderer::set_reverse_winding(bool r)
166 {
167         state->reverse_winding = r;
168 }
169
170 void Renderer::set_object_lod_bias(unsigned b)
171 {
172         state->object_lod_bias = b;
173 }
174
175 void Renderer::push_state()
176 {
177         state_stack.push_back(state_stack.back());
178         state = &state_stack.back();
179 }
180
181 void Renderer::pop_state()
182 {
183         if(state_stack.size()==1)
184                 throw stack_underflow("Renderer::pop_state");
185
186         const Camera *old_camera = state->camera;
187         const Lighting *old_lighting = state->lighting;
188         const Clipping *old_clipping = state->clipping;
189         state_stack.pop_back();
190         state = &state_stack.back();
191         changed |= MATRIX;
192         bool camera_changed = (state->camera!=old_camera);
193         if(camera_changed)
194         {
195                 if(state->camera)
196                 {
197                         standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
198                         standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
199                 }
200                 else
201                 {
202                         standard_shdata.uniform("projection_matrix", Matrix());
203                         standard_shdata.uniform("eye_world_matrix", Matrix());
204                 }
205                 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
206         }
207         /* This actually should compare the relevant matrices rather than check for
208         camera, but in practice lighting and clipping is set right after the camera
209         and a boolean check is much faster than matrix comparison. */
210         if(state->lighting!=old_lighting || camera_changed)
211         {
212                 if(state->lighting)
213                 {
214                         state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
215                         changed |= STANDARD_SHDATA;
216                 }
217                 changed |= LEGACY_LIGHTING;
218         }
219         if(state->clipping!=old_clipping || camera_changed)
220         {
221                 if(state->clipping)
222                 {
223                         state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
224                         changed |= STANDARD_SHDATA;
225                 }
226                 changed |= LEGACY_CLIPPING;
227         }
228 }
229
230 void Renderer::end()
231 {
232         if(state_stack.size()>1)
233                 throw invalid_operation("Renderer::end");
234
235         *state = State();
236         if(default_camera)
237                 set_camera(*default_camera);
238         else
239                 standard_shdata.uniform("projection_matrix", Matrix());
240         shdata_stack.clear();
241         excluded.clear();
242
243         Mesh::unbind();
244         Texturing::unbind();
245         Texture::unbind_from(0);
246         Material::unbind();
247         Lighting::unbind();
248         Clipping::unbind();
249         Program::unbind();
250         VertexSetup::unbind();
251         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
252         WindingTest::unbind();
253
254         if(matrices_loaded)
255         {
256                 MatrixStack::projection().pop();
257                 MatrixStack::modelview().pop();
258                 matrices_loaded = false;
259         }
260 }
261
262 void Renderer::exclude(const Renderable &renderable)
263 {
264         excluded.insert(&renderable);
265 }
266
267 void Renderer::include(const Renderable &renderable)
268 {
269         excluded.erase(&renderable);
270 }
271
272 void Renderer::render(const Renderable &renderable, const Tag &tag)
273 {
274         if(!excluded.count(&renderable))
275                 renderable.render(*this, tag);
276 }
277
278 void Renderer::draw(const Batch &batch)
279 {
280         apply_state();
281
282         batch.draw();
283 }
284
285 void Renderer::draw_instanced(const Batch &batch, unsigned count)
286 {
287         apply_state();
288
289         batch.draw_instanced(count);
290 }
291
292 void Renderer::apply_state()
293 {
294         /* We (mostly) let the objects themselves figure out if the binding has
295         changed */
296
297         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
298         bool legacy_textures = !state->shprog;
299
300         if(state->texturing)
301                 state->texturing->bind(legacy_textures);
302         else
303         {
304                 Texturing::unbind();
305                 if(state->texture)
306                         state->texture->bind_to(0, legacy_textures);
307                 else
308                         Texture::unbind_from(0);
309         }
310
311         if(legacy_bindings)
312         {
313                 if(state->material)
314                         state->material->bind();
315                 else
316                         Material::unbind();
317
318                 if(changed&LEGACY_LIGHTING)
319                 {
320                         if(state->lighting)
321                         {
322                                 MatrixStack::modelview() = state->lighting_matrix;
323                                 state->lighting->bind();
324                                 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
325                         }
326                         else
327                                 Lighting::unbind();
328                 }
329         }
330
331         if(state->clipping)
332         {
333                 if(legacy_bindings)
334                 {
335                         if(changed&LEGACY_CLIPPING)
336                         {
337                                 MatrixStack::modelview() = state->clipping_matrix;
338                                 state->clipping->bind(true);
339                                 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
340                         }
341                 }
342                 else
343                         state->clipping->bind(false);
344         }
345         else
346                 Clipping::unbind();
347
348         if(state->shprog)
349         {
350                 bool shprog_changed = (state->shprog!=Program::current());
351                 state->shprog->bind();
352
353                 if(!legacy_bindings)
354                 {
355                         if(changed&MODERN_MATRIX)
356                         {
357                                 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
358                                 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
359                                 nm = transpose(invert(nm));
360                                 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
361                                 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
362                         }
363
364                         if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
365                         {
366                                 state->material->get_shader_data().apply();
367                                 changed &= ~MATERIAL_SHDATA;
368                         }
369
370                         if((changed&STANDARD_SHDATA) || shprog_changed)
371                         {
372                                 standard_shdata.apply();
373                                 changed &= ~STANDARD_SHDATA;
374                         }
375                 }
376
377                 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
378
379                 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
380                 {
381                         if(extra_shdata)
382                                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
383                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
384                                 (*i)->apply();
385                         changed &= ~SHADER_DATA;
386                 }
387         }
388         else
389                 Program::unbind();
390
391         if(state->mesh)
392         {
393                 if(legacy_bindings)
394                 {
395                         Mesh::unbind();
396                         state->mesh->get_vertices().apply();
397                         if(const Buffer *ibuf = state->mesh->get_index_buffer())
398                                 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
399                         else
400                                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
401                 }
402                 else
403                         state->mesh->bind();
404         }
405         else
406         {
407                 Mesh::unbind();
408
409                 if(state->vertex_setup)
410                         state->vertex_setup->bind();
411                 else
412                         VertexSetup::unbind();
413         }
414
415         if(state->winding_test)
416         {
417                 if(state->reverse_winding)
418                         state->winding_test->get_reverse().bind();
419                 else
420                         state->winding_test->bind();
421         }
422         else
423                 WindingTest::unbind();
424
425         if(legacy_bindings)
426         {
427                 if(!matrices_loaded)
428                 {
429                         MatrixStack::modelview().push();
430                         MatrixStack::projection().push();
431                         matrices_loaded = true;
432                 }
433
434                 if(changed&LEGACY_PROJECTION)
435                 {
436                         MatrixStack::projection() = state->camera->get_projection_matrix();
437                         changed &= ~LEGACY_PROJECTION;
438                 }
439
440                 if(changed&LEGACY_MATRIX)
441                 {
442                         MatrixStack::modelview() = state->modelview_matrix;
443                         changed &= ~LEGACY_MATRIX;
444                 }
445         }
446 }
447
448
449 Renderer::State::State():
450         camera(0),
451         texture(0),
452         texturing(0),
453         lowest_effect_texunit(TexUnit::get_n_units()),
454         material(0),
455         lighting(0),
456         clipping(0),
457         shprog(0),
458         shdata_count(0),
459         mesh(0),
460         vertex_setup(0),
461         winding_test(0),
462         reverse_winding(false),
463         object_lod_bias(0)
464 { }
465
466 } // namespace GL
467 } // namespace Msp