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