]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
e59949a3683f938797251a4a5d3f34a7ebbddc46
[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         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
273         if(state->mesh && legacy_bindings)
274         {
275                 if(const Buffer *ibuf = state->mesh->get_index_buffer())
276                         ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
277                 else
278                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
279         }
280
281         batch.draw();
282 }
283
284 void Renderer::apply_state()
285 {
286         /* We (mostly) let the objects themselves figure out if the binding has
287         changed */
288
289         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
290         bool legacy_textures = !state->shprog;
291
292         if(state->texturing)
293                 state->texturing->bind(legacy_textures);
294         else
295         {
296                 Texturing::unbind();
297                 if(state->texture)
298                         state->texture->bind_to(0, legacy_textures);
299                 else
300                         Texture::unbind_from(0);
301         }
302
303         if(legacy_bindings)
304         {
305                 if(state->material)
306                         state->material->bind();
307                 else
308                         Material::unbind();
309
310                 if(changed&LEGACY_LIGHTING)
311                 {
312                         if(state->lighting)
313                         {
314                                 MatrixStack::modelview() = state->lighting_matrix;
315                                 state->lighting->bind();
316                                 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
317                         }
318                         else
319                                 Lighting::unbind();
320                 }
321         }
322
323         if(state->clipping)
324         {
325                 if(legacy_bindings)
326                 {
327                         if(changed&LEGACY_CLIPPING)
328                         {
329                                 MatrixStack::modelview() = state->clipping_matrix;
330                                 state->clipping->bind(true);
331                                 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
332                         }
333                 }
334                 else
335                         state->clipping->bind(false);
336         }
337         else
338                 Clipping::unbind();
339
340         if(state->shprog)
341         {
342                 bool shprog_changed = (state->shprog!=Program::current());
343                 state->shprog->bind();
344
345                 if(!legacy_bindings)
346                 {
347                         if(changed&MODERN_MATRIX)
348                         {
349                                 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
350                                 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
351                                 nm = transpose(invert(nm));
352                                 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
353                                 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
354                         }
355
356                         if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
357                         {
358                                 state->material->get_shader_data().apply();
359                                 changed &= ~MATERIAL_SHDATA;
360                         }
361
362                         if((changed&STANDARD_SHDATA) || shprog_changed)
363                         {
364                                 standard_shdata.apply();
365                                 changed &= ~STANDARD_SHDATA;
366                         }
367                 }
368
369                 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
370
371                 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
372                 {
373                         if(extra_shdata)
374                                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
375                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
376                                 (*i)->apply();
377                         changed &= ~SHADER_DATA;
378                 }
379         }
380         else
381                 Program::unbind();
382
383         if(state->mesh)
384         {
385                 if(legacy_bindings)
386                 {
387                         Mesh::unbind();
388                         state->mesh->get_vertices().apply();
389                 }
390                 else
391                         state->mesh->bind();
392         }
393         else
394         {
395                 Mesh::unbind();
396
397                 if(state->vertex_setup)
398                         state->vertex_setup->bind();
399                 else
400                         VertexSetup::unbind();
401         }
402
403         if(state->winding_test)
404         {
405                 if(state->reverse_winding)
406                         state->winding_test->get_reverse().bind();
407                 else
408                         state->winding_test->bind();
409         }
410         else
411                 WindingTest::unbind();
412
413         if(legacy_bindings)
414         {
415                 if(!matrices_loaded)
416                 {
417                         MatrixStack::modelview().push();
418                         MatrixStack::projection().push();
419                         matrices_loaded = true;
420                 }
421
422                 if(changed&LEGACY_PROJECTION)
423                 {
424                         MatrixStack::projection() = state->camera->get_projection_matrix();
425                         changed &= ~LEGACY_PROJECTION;
426                 }
427
428                 if(changed&LEGACY_MATRIX)
429                 {
430                         MatrixStack::modelview() = state->modelview_matrix;
431                         changed &= ~LEGACY_MATRIX;
432                 }
433         }
434 }
435
436
437 Renderer::State::State():
438         camera(0),
439         texture(0),
440         texturing(0),
441         lowest_effect_texunit(TexUnit::get_n_units()),
442         material(0),
443         lighting(0),
444         clipping(0),
445         shprog(0),
446         shdata_count(0),
447         mesh(0),
448         vertex_setup(0),
449         winding_test(0),
450         reverse_winding(false)
451 { }
452
453 } // namespace GL
454 } // namespace Msp