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