10 #include "programdata.h"
11 #include "renderable.h"
14 #include "texturing.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
25 Renderer::Renderer(const Camera *c):
28 matrices_loaded(false),
31 state_stack.reserve(16);
32 shdata_stack.reserve(32);
33 state = &state_stack.back();
39 standard_shdata.uniform("projection_matrix", Matrix());
40 standard_shdata.uniform("eye_world_matrix", Matrix());
49 void Renderer::begin(const Camera *c)
56 void Renderer::set_camera(const Camera &c)
59 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
60 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
61 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
62 set_matrix(state->camera->get_view_matrix());
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 if(state->shdata_count<shdata_stack.size() && shdata_stack[state->shdata_count]==&d)
134 ++state->shdata_count;
138 shdata_stack.push_back(&d);
139 state->shdata_count = shdata_stack.size();
140 changed |= SHADER_DATA;
144 void Renderer::flush_shader_data()
146 if(shdata_stack.size()>state->shdata_count)
147 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
150 void Renderer::set_mesh(const Mesh *m)
155 void Renderer::set_vertex_setup(const VertexSetup *vs)
157 state->vertex_setup = vs;
160 void Renderer::set_winding_test(const WindingTest *w)
162 state->winding_test = w;
165 void Renderer::set_reverse_winding(bool r)
167 state->reverse_winding = r;
170 void Renderer::set_object_lod_bias(unsigned b)
172 state->object_lod_bias = b;
175 void Renderer::push_state()
177 state_stack.push_back(state_stack.back());
178 state = &state_stack.back();
181 void Renderer::pop_state()
183 if(state_stack.size()==1)
184 throw stack_underflow("Renderer::pop_state");
186 const Camera *old_camera = state->camera;
187 const Lighting *old_lighting = state->lighting;
188 const Clipping *old_clipping = state->clipping;
189 state_stack.pop_back();
190 state = &state_stack.back();
192 bool camera_changed = (state->camera!=old_camera);
197 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
198 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
202 standard_shdata.uniform("projection_matrix", Matrix());
203 standard_shdata.uniform("eye_world_matrix", Matrix());
205 changed |= STANDARD_SHDATA|LEGACY_PROJECTION;
207 /* This actually should compare the relevant matrices rather than check for
208 camera, but in practice lighting and clipping is set right after the camera
209 and a boolean check is much faster than matrix comparison. */
210 if(state->lighting!=old_lighting || camera_changed)
214 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
215 changed |= STANDARD_SHDATA;
217 changed |= LEGACY_LIGHTING;
219 if(state->clipping!=old_clipping || camera_changed)
223 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
224 changed |= STANDARD_SHDATA;
226 changed |= LEGACY_CLIPPING;
232 if(state_stack.size()>1)
233 throw invalid_operation("Renderer::end");
237 set_camera(*default_camera);
239 standard_shdata.uniform("projection_matrix", Matrix());
240 shdata_stack.clear();
245 Texture::unbind_from(0);
250 VertexSetup::unbind();
251 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
252 WindingTest::unbind();
256 MatrixStack::projection().pop();
257 MatrixStack::modelview().pop();
258 matrices_loaded = false;
262 void Renderer::exclude(const Renderable &renderable)
264 excluded.insert(&renderable);
267 void Renderer::include(const Renderable &renderable)
269 excluded.erase(&renderable);
272 void Renderer::render(const Renderable &renderable, const Tag &tag)
274 if(!excluded.count(&renderable))
275 renderable.render(*this, tag);
278 void Renderer::draw(const Batch &batch)
285 void Renderer::draw_instanced(const Batch &batch, unsigned count)
289 batch.draw_instanced(count);
292 void Renderer::apply_state()
294 /* We (mostly) let the objects themselves figure out if the binding has
297 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
298 bool legacy_textures = !state->shprog;
301 state->texturing->bind(legacy_textures);
306 state->texture->bind_to(0, legacy_textures);
308 Texture::unbind_from(0);
314 state->material->bind();
318 if(changed&LEGACY_LIGHTING)
322 MatrixStack::modelview() = state->lighting_matrix;
323 state->lighting->bind();
324 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
335 if(changed&LEGACY_CLIPPING)
337 MatrixStack::modelview() = state->clipping_matrix;
338 state->clipping->bind(true);
339 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
343 state->clipping->bind(false);
350 bool shprog_changed = (state->shprog!=Program::current());
351 state->shprog->bind();
355 if(changed&MODERN_MATRIX)
357 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
358 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
359 nm = transpose(invert(nm));
360 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
361 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
364 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
366 state->material->get_shader_data().apply();
367 changed &= ~MATERIAL_SHDATA;
370 if((changed&STANDARD_SHDATA) || shprog_changed)
372 standard_shdata.apply();
373 changed &= ~STANDARD_SHDATA;
377 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
379 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
382 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
383 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
385 changed &= ~SHADER_DATA;
396 state->mesh->get_vertices().apply();
397 if(const Buffer *ibuf = state->mesh->get_index_buffer())
398 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
400 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
409 if(state->vertex_setup)
410 state->vertex_setup->bind();
412 VertexSetup::unbind();
415 if(state->winding_test)
417 if(state->reverse_winding)
418 state->winding_test->get_reverse().bind();
420 state->winding_test->bind();
423 WindingTest::unbind();
429 MatrixStack::modelview().push();
430 MatrixStack::projection().push();
431 matrices_loaded = true;
434 if(changed&LEGACY_PROJECTION)
436 MatrixStack::projection() = state->camera->get_projection_matrix();
437 changed &= ~LEGACY_PROJECTION;
440 if(changed&LEGACY_MATRIX)
442 MatrixStack::modelview() = state->modelview_matrix;
443 changed &= ~LEGACY_MATRIX;
449 Renderer::State::State():
453 lowest_effect_texunit(TexUnit::get_n_units()),
462 reverse_winding(false),