]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Unbind Clipping in Renderer::end
[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         Clipping::unbind();
202         Program::unbind();
203         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
204         WindingTest::unbind();
205 }
206
207 void Renderer::exclude(const Renderable &renderable)
208 {
209         excluded.insert(&renderable);
210 }
211
212 void Renderer::include(const Renderable &renderable)
213 {
214         excluded.erase(&renderable);
215 }
216
217 void Renderer::render(const Renderable &renderable, const Tag &tag)
218 {
219         if(!excluded.count(&renderable))
220                 renderable.render(*this, tag);
221 }
222
223 void Renderer::draw(const Batch &batch)
224 {
225         apply_state();
226
227         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
228         if(state->mesh && legacy_bindings)
229         {
230                 if(const Buffer *ibuf = state->mesh->get_index_buffer())
231                         ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
232                 else
233                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
234         }
235
236         batch.draw();
237 }
238
239 void Renderer::apply_state()
240 {
241         /* We (mostly) let the objects themselves figure out if the binding has
242         changed */
243
244         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
245
246         if(state->texturing)
247                 state->texturing->bind();
248         else
249         {
250                 Texturing::unbind();
251                 if(state->texture)
252                         state->texture->bind_to(0);
253                 else
254                         Texture::unbind_from(0);
255         }
256
257         if(legacy_bindings)
258         {
259                 if(state->material)
260                         state->material->bind();
261                 else
262                         Material::unbind();
263
264                 if(changed&LIGHTING)
265                 {
266                         if(state->lighting)
267                         {
268                                 MatrixStack::modelview() = state->lighting_matrix;
269                                 state->lighting->bind();
270                                 changed = (changed&~LIGHTING)|LEGACY_MATRIX;
271                         }
272                         else
273                                 Lighting::unbind();
274                 }
275         }
276
277         if(state->clipping)
278         {
279                 if(legacy_bindings)
280                 {
281                         if(changed&CLIPPING)
282                         {
283                                 MatrixStack::modelview() = state->clipping_matrix;
284                                 state->clipping->bind(true);
285                                 changed = (changed&~CLIPPING)|LEGACY_MATRIX;
286                         }
287                 }
288                 else
289                         state->clipping->bind(false);
290         }
291         else
292                 Clipping::unbind();
293
294         if(state->shprog)
295         {
296                 bool shprog_changed = (state->shprog!=Program::current());
297                 state->shprog->bind();
298
299                 if(!legacy_bindings)
300                 {
301                         if(changed&MODERN_MATRIX)
302                         {
303                                 const Matrix &m = mtx_stack.top();
304                                 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
305                                 LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
306                                 nm = transpose(invert(nm));
307                                 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
308                                 changed &= ~MODERN_MATRIX;
309                         }
310
311                         if(state->material)
312                                 state->material->get_shader_data().apply();
313                         standard_shdata.apply();
314                 }
315
316                 if((changed&SHADER_DATA) || shprog_changed)
317                 {
318                         vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
319                         if(!shprog_changed)
320                                 i += shdata_applied;
321                         for(; i!=shdata_stack.end(); ++i)
322                                 (*i)->apply();
323                         changed &= ~SHADER_DATA;
324                         shdata_applied = shdata_stack.size();
325                 }
326         }
327         else
328                 Program::unbind();
329
330         if(state->mesh)
331         {
332                 if(legacy_bindings)
333                 {
334                         Mesh::unbind();
335                         state->mesh->get_vertices().apply();
336                 }
337                 else
338                         state->mesh->bind();
339         }
340         else
341                 Mesh::unbind();
342
343         if(state->winding_test)
344         {
345                 if(state->reverse_winding)
346                         state->winding_test->get_reverse().bind();
347                 else
348                         state->winding_test->bind();
349         }
350         else
351                 WindingTest::unbind();
352
353         if(legacy_bindings)
354         {
355                 if(!matrices_loaded)
356                 {
357                         MatrixStack::modelview().push();
358                         if(camera)
359                         {
360                                 MatrixStack::projection().push();
361                                 camera->apply();
362                         }
363                         matrices_loaded = true;
364                 }
365
366                 if(changed&LEGACY_MATRIX)
367                 {
368                         MatrixStack::modelview() = mtx_stack.top();
369                         changed &= ~LEGACY_MATRIX;
370                 }
371         }
372 }
373
374 void Renderer::reset_state()
375 {
376         if(!matrices_loaded)
377                 return;
378
379         if(camera)
380                 MatrixStack::projection().pop();
381         MatrixStack::modelview().pop();
382         matrices_loaded = false;
383         changed |= MATRIX;
384
385         *state = State();
386 }
387
388
389 Renderer::State::State():
390         texture(0),
391         texturing(0),
392         lowest_effect_texunit(TexUnit::get_n_units()),
393         material(0),
394         lighting(0),
395         clipping(0),
396         shprog(0),
397         shdata_count(0),
398         mesh(0),
399         winding_test(0),
400         reverse_winding(false)
401 { }
402
403
404 Renderer::MtxStack::MtxStack(Renderer &r):
405         renderer(r)
406 { }
407
408 void Renderer::MtxStack::update()
409 {
410         renderer.changed |= MATRIX;
411 }
412
413 } // namespace GL
414 } // namespace Msp