9 #include "programdata.h"
10 #include "renderable.h"
14 #include "texturing.h"
16 #include "vertexarray.h"
17 #include "vertexsetup.h"
18 #include "windingtest.h"
31 Renderer::Renderer(const Camera *c):
42 state_stack.reserve(16);
43 state_stack.push_back(State());
44 shdata_stack.reserve(32);
45 state = &state_stack.back();
47 standard_shdata.uniform("projection_matrix", Matrix());
48 standard_shdata.uniform("eye_world_matrix", Matrix());
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;
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, const Sampler *s)
84 void Renderer::set_texturing(const Texturing *t)
91 unsigned Renderer::allocate_effect_texunit()
93 return --state->lowest_effect_texunit;
96 void Renderer::set_material(const Material *m)
99 changed |= MATERIAL_SHDATA;
102 void Renderer::set_lighting(const Lighting *l)
105 state->lighting_matrix = state->modelview_matrix;
108 l->update_shader_data(standard_shdata, state->lighting_matrix);
109 changed |= STANDARD_SHDATA;
113 void Renderer::set_clipping(const Clipping *c)
116 state->clipping_matrix = state->modelview_matrix;
119 c->update_shader_data(standard_shdata, state->clipping_matrix);
120 changed |= STANDARD_SHDATA;
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_vertex_setup(const VertexSetup *vs)
152 state->vertex_setup = vs;
155 void Renderer::set_winding_test(const WindingTest *w)
157 state->winding_test = w;
160 void Renderer::set_reverse_winding(bool r)
162 state->reverse_winding = r;
165 void Renderer::set_object_lod_bias(unsigned b)
167 state->object_lod_bias = b;
170 void Renderer::push_state()
172 state_stack.push_back(state_stack.back());
173 state = &state_stack.back();
176 void Renderer::pop_state()
178 if(state_stack.size()==1)
179 throw stack_underflow("Renderer::pop_state");
181 const Camera *old_camera = state->camera;
182 const Lighting *old_lighting = state->lighting;
183 const Clipping *old_clipping = state->clipping;
184 state_stack.pop_back();
185 state = &state_stack.back();
187 bool camera_changed = (state->camera!=old_camera);
192 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
193 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
197 standard_shdata.uniform("projection_matrix", Matrix());
198 standard_shdata.uniform("eye_world_matrix", Matrix());
200 changed |= STANDARD_SHDATA;
202 /* This actually should compare the relevant matrices rather than check for
203 camera, but in practice lighting and clipping is set right after the camera
204 and a boolean check is much faster than matrix comparison. */
205 if(state->lighting!=old_lighting || camera_changed)
209 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
210 changed |= STANDARD_SHDATA;
213 if(state->clipping!=old_clipping || camera_changed)
217 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
218 changed |= STANDARD_SHDATA;
225 if(state_stack.size()>1)
226 throw invalid_operation("Renderer::end");
230 set_camera(*default_camera);
232 standard_shdata.uniform("projection_matrix", Matrix());
233 shdata_stack.clear();
237 Texture::unbind_from(0);
240 VertexSetup::unbind();
241 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
242 WindingTest::unbind();
245 void Renderer::exclude(const Renderable &renderable)
247 excluded.insert(&renderable);
250 void Renderer::include(const Renderable &renderable)
252 excluded.erase(&renderable);
255 void Renderer::render(const Renderable &renderable, const Tag &tag)
257 if(!excluded.count(&renderable))
258 renderable.render(*this, tag);
261 void Renderer::draw(const Batch &batch)
268 void Renderer::draw_instanced(const Batch &batch, unsigned count)
272 batch.draw_instanced(count);
275 void Renderer::apply_state()
278 throw invalid_operation("Renderer::apply_state");
280 /* We (mostly) let the objects themselves figure out if the binding has
284 state->texturing->bind();
291 state->sampler->bind_to(0);
293 Sampler::unbind_from(0);
294 state->texture->bind_to(0);
297 Texture::unbind_from(0);
301 state->clipping->bind();
305 bool shprog_changed = (state->shprog!=Program::current());
306 state->shprog->bind();
310 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
311 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
312 nm = transpose(invert(nm));
313 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
314 changed = (changed&~MATRIX)|STANDARD_SHDATA;
317 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
319 state->material->get_shader_data().apply();
320 changed &= ~MATERIAL_SHDATA;
323 if((changed&STANDARD_SHDATA) || shprog_changed)
325 standard_shdata.apply();
326 changed &= ~STANDARD_SHDATA;
329 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
331 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
334 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
335 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
337 changed &= ~SHADER_DATA;
340 if(state->vertex_setup)
341 state->vertex_setup->bind();
343 VertexSetup::unbind();
345 if(state->winding_test)
347 if(state->reverse_winding)
348 state->winding_test->get_reverse().bind();
350 state->winding_test->bind();
353 WindingTest::unbind();
357 Renderer::State::State():
361 lowest_effect_texunit(TexUnit::get_n_units()),
369 reverse_winding(false),