10 #include "programdata.h"
11 #include "renderable.h"
14 #include "texturing.h"
16 #include "vertexarray.h"
17 #include "windingtest.h"
24 Renderer::Renderer(const Camera *c):
26 matrices_loaded(false),
31 state_stack.reserve(16);
32 shdata_stack.reserve(32);
33 state = &state_stack.back();
43 void Renderer::begin(const Camera *c)
45 if(state_stack.size()>1)
46 throw invalid_operation("Renderer::begin");
54 state->modelview_matrix = camera->get_view_matrix();
55 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
59 state->modelview_matrix = MatrixStack::modelview().top();
60 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
62 changed |= MATRIX|STANDARD_SHDATA;
65 void Renderer::set_matrix(const Matrix &matrix)
67 state->modelview_matrix = matrix;
71 void Renderer::transform(const Matrix &matrix)
73 state->modelview_matrix *= matrix;
77 void Renderer::set_texture(const Texture *t)
83 void Renderer::set_texturing(const Texturing *t)
89 unsigned Renderer::allocate_effect_texunit()
91 return --state->lowest_effect_texunit;
94 void Renderer::set_material(const Material *m)
97 changed |= MATERIAL_SHDATA;
100 void Renderer::set_lighting(const Lighting *l)
103 state->lighting_matrix = state->modelview_matrix;
106 l->update_shader_data(standard_shdata, state->lighting_matrix);
107 changed |= STANDARD_SHDATA;
109 changed |= LEGACY_LIGHTING;
112 void Renderer::set_clipping(const Clipping *c)
115 state->clipping_matrix = state->modelview_matrix;
118 c->update_shader_data(standard_shdata, state->clipping_matrix);
119 changed |= STANDARD_SHDATA;
121 changed |= LEGACY_CLIPPING;
124 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
131 void Renderer::add_shader_data(const ProgramData &d)
133 shdata_stack.push_back(&d);
134 state->shdata_count = shdata_stack.size();
135 changed |= SHADER_DATA;
138 void Renderer::set_mesh(const Mesh *m)
143 void Renderer::set_winding_test(const WindingTest *w)
145 state->winding_test = w;
148 void Renderer::set_reverse_winding(bool r)
150 state->reverse_winding = r;
153 void Renderer::push_state()
155 state_stack.push_back(state_stack.back());
156 state = &state_stack.back();
159 void Renderer::pop_state()
161 if(state_stack.size()==1)
162 throw stack_underflow("Renderer::pop_state");
164 const Lighting *old_lighting = state->lighting;
165 const Clipping *old_clipping = state->clipping;
166 state_stack.pop_back();
167 state = &state_stack.back();
168 if(shdata_stack.size()>state->shdata_count)
170 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
171 changed |= SHADER_DATA;
173 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
175 if(state->lighting!=old_lighting)
179 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
180 changed |= STANDARD_SHDATA;
182 changed |= LEGACY_LIGHTING;
184 if(state->clipping!=old_clipping)
188 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
189 changed |= STANDARD_SHDATA;
191 changed |= LEGACY_CLIPPING;
195 void Renderer::escape()
198 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
199 matrices_loaded = false;
204 if(state_stack.size()>1)
205 throw invalid_operation("Renderer::end");
211 Texture::unbind_from(0);
216 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
217 WindingTest::unbind();
220 void Renderer::exclude(const Renderable &renderable)
222 excluded.insert(&renderable);
225 void Renderer::include(const Renderable &renderable)
227 excluded.erase(&renderable);
230 void Renderer::render(const Renderable &renderable, const Tag &tag)
232 if(!excluded.count(&renderable))
233 renderable.render(*this, tag);
236 void Renderer::draw(const Batch &batch)
240 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
241 if(state->mesh && legacy_bindings)
243 if(const Buffer *ibuf = state->mesh->get_index_buffer())
244 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
246 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
252 void Renderer::apply_state()
254 /* We (mostly) let the objects themselves figure out if the binding has
257 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
260 state->texturing->bind();
265 state->texture->bind_to(0);
267 Texture::unbind_from(0);
273 state->material->bind();
277 if(changed&LEGACY_LIGHTING)
281 MatrixStack::modelview() = state->lighting_matrix;
282 state->lighting->bind();
283 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
294 if(changed&LEGACY_CLIPPING)
296 MatrixStack::modelview() = state->clipping_matrix;
297 state->clipping->bind(true);
298 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
302 state->clipping->bind(false);
309 bool shprog_changed = (state->shprog!=Program::current());
310 state->shprog->bind();
314 if(changed&MODERN_MATRIX)
316 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
317 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
318 nm = transpose(invert(nm));
319 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
320 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
323 if(state->material && (changed&MATERIAL_SHDATA))
325 state->material->get_shader_data().apply();
326 changed &= ~MATERIAL_SHDATA;
329 if(changed&STANDARD_SHDATA)
331 standard_shdata.apply();
332 changed &= ~STANDARD_SHDATA;
336 if((changed&SHADER_DATA) || shprog_changed)
338 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
341 for(; i!=shdata_stack.end(); ++i)
343 changed &= ~SHADER_DATA;
344 shdata_applied = shdata_stack.size();
355 state->mesh->get_vertices().apply();
363 if(state->winding_test)
365 if(state->reverse_winding)
366 state->winding_test->get_reverse().bind();
368 state->winding_test->bind();
371 WindingTest::unbind();
377 MatrixStack::modelview().push();
380 MatrixStack::projection().push();
383 matrices_loaded = true;
386 if(changed&LEGACY_MATRIX)
388 MatrixStack::modelview() = state->modelview_matrix;
389 changed &= ~LEGACY_MATRIX;
394 void Renderer::reset_state()
400 MatrixStack::projection().pop();
401 MatrixStack::modelview().pop();
402 matrices_loaded = false;
409 Renderer::State::State():
412 lowest_effect_texunit(TexUnit::get_n_units()),
420 reverse_winding(false)