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