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