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