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),
30 state_stack.reserve(16);
31 shdata_stack.reserve(32);
32 state = &state_stack.back();
37 standard_shdata.uniform("projection_matrix", Matrix());
45 void Renderer::begin(const Camera *c)
52 void Renderer::set_camera(const Camera &c)
55 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
56 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
57 set_matrix(state->camera->get_view_matrix());
60 void Renderer::set_matrix(const Matrix &matrix)
62 state->modelview_matrix = matrix;
66 void Renderer::transform(const Matrix &matrix)
68 state->modelview_matrix *= matrix;
72 void Renderer::set_texture(const Texture *t)
78 void Renderer::set_texturing(const Texturing *t)
84 unsigned Renderer::allocate_effect_texunit()
86 return --state->lowest_effect_texunit;
89 void Renderer::set_material(const Material *m)
92 changed |= MATERIAL_SHDATA;
95 void Renderer::set_lighting(const Lighting *l)
98 state->lighting_matrix = state->modelview_matrix;
101 l->update_shader_data(standard_shdata, state->lighting_matrix);
102 changed |= STANDARD_SHDATA;
104 changed |= LEGACY_LIGHTING;
107 void Renderer::set_clipping(const Clipping *c)
110 state->clipping_matrix = state->modelview_matrix;
113 c->update_shader_data(standard_shdata, state->clipping_matrix);
114 changed |= STANDARD_SHDATA;
116 changed |= LEGACY_CLIPPING;
119 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
126 void Renderer::add_shader_data(const ProgramData &d)
128 shdata_stack.push_back(&d);
129 state->shdata_count = shdata_stack.size();
130 changed |= SHADER_DATA;
133 void Renderer::set_mesh(const Mesh *m)
138 void Renderer::set_winding_test(const WindingTest *w)
140 state->winding_test = w;
143 void Renderer::set_reverse_winding(bool r)
145 state->reverse_winding = r;
148 void Renderer::push_state()
150 state_stack.push_back(state_stack.back());
151 state = &state_stack.back();
154 void Renderer::pop_state()
156 if(state_stack.size()==1)
157 throw stack_underflow("Renderer::pop_state");
159 const Camera *old_camera = state->camera;
160 const Lighting *old_lighting = state->lighting;
161 const Clipping *old_clipping = state->clipping;
162 state_stack.pop_back();
163 state = &state_stack.back();
164 if(shdata_stack.size()>state->shdata_count)
166 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
167 changed |= SHADER_DATA;
169 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
171 bool camera_changed = (state->camera!=old_camera);
175 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
177 standard_shdata.uniform("projection_matrix", Matrix());
178 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
180 /* This actually should compare the relevant matrices rather than check for
181 camera, but in practice lighting and clipping is set right after the camera
182 and a boolean check is much faster than matrix comparison. */
183 if(state->lighting!=old_lighting || camera_changed)
187 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
188 changed |= STANDARD_SHDATA;
190 changed |= LEGACY_LIGHTING;
192 if(state->clipping!=old_clipping || camera_changed)
196 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
197 changed |= STANDARD_SHDATA;
199 changed |= LEGACY_CLIPPING;
205 if(state_stack.size()>1)
206 throw invalid_operation("Renderer::end");
209 shdata_stack.clear();
214 Texture::unbind_from(0);
219 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
220 WindingTest::unbind();
224 MatrixStack::projection().pop();
225 MatrixStack::modelview().pop();
226 matrices_loaded = false;
230 void Renderer::exclude(const Renderable &renderable)
232 excluded.insert(&renderable);
235 void Renderer::include(const Renderable &renderable)
237 excluded.erase(&renderable);
240 void Renderer::render(const Renderable &renderable, const Tag &tag)
242 if(!excluded.count(&renderable))
243 renderable.render(*this, tag);
246 void Renderer::draw(const Batch &batch)
250 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
251 if(state->mesh && legacy_bindings)
253 if(const Buffer *ibuf = state->mesh->get_index_buffer())
254 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
256 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
262 void Renderer::apply_state()
264 /* We (mostly) let the objects themselves figure out if the binding has
267 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
270 state->texturing->bind();
275 state->texture->bind_to(0);
277 Texture::unbind_from(0);
283 state->material->bind();
287 if(changed&LEGACY_LIGHTING)
291 MatrixStack::modelview() = state->lighting_matrix;
292 state->lighting->bind();
293 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
304 if(changed&LEGACY_CLIPPING)
306 MatrixStack::modelview() = state->clipping_matrix;
307 state->clipping->bind(true);
308 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
312 state->clipping->bind(false);
319 bool shprog_changed = (state->shprog!=Program::current());
320 state->shprog->bind();
324 if(changed&MODERN_MATRIX)
326 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
327 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
328 nm = transpose(invert(nm));
329 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
330 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
333 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
335 state->material->get_shader_data().apply();
336 changed &= ~MATERIAL_SHDATA;
339 if((changed&STANDARD_SHDATA) || shprog_changed)
341 standard_shdata.apply();
342 changed &= ~STANDARD_SHDATA;
346 if((changed&SHADER_DATA) || shprog_changed)
348 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
351 for(; i!=shdata_stack.end(); ++i)
353 changed &= ~SHADER_DATA;
354 shdata_applied = shdata_stack.size();
365 state->mesh->get_vertices().apply();
373 if(state->winding_test)
375 if(state->reverse_winding)
376 state->winding_test->get_reverse().bind();
378 state->winding_test->bind();
381 WindingTest::unbind();
387 MatrixStack::modelview().push();
388 MatrixStack::projection().push();
389 matrices_loaded = true;
392 if(changed&LEGACY_PROJECTION)
394 MatrixStack::projection() = state->camera->get_projection_matrix();
395 changed &= ~LEGACY_PROJECTION;
398 if(changed&LEGACY_MATRIX)
400 MatrixStack::modelview() = state->modelview_matrix;
401 changed &= ~LEGACY_MATRIX;
407 Renderer::State::State():
411 lowest_effect_texunit(TexUnit::get_n_units()),
419 reverse_winding(false)