9 #include "programdata.h"
10 #include "renderable.h"
13 #include "texturing.h"
15 #include "vertexarray.h"
16 #include "vertexsetup.h"
17 #include "windingtest.h"
30 Renderer::Renderer(const Camera *c):
41 state_stack.reserve(16);
42 state_stack.push_back(State());
43 shdata_stack.reserve(32);
44 state = &state_stack.back();
46 standard_shdata.uniform("projection_matrix", Matrix());
47 standard_shdata.uniform("eye_world_matrix", Matrix());
55 void Renderer::set_camera(const Camera &c)
58 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
59 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
60 changed |= STANDARD_SHDATA;
61 set_matrix(state->camera->get_view_matrix());
64 void Renderer::set_matrix(const Matrix &matrix)
66 state->modelview_matrix = matrix;
70 void Renderer::transform(const Matrix &matrix)
72 state->modelview_matrix *= matrix;
76 void Renderer::set_texture(const Texture *t)
82 void Renderer::set_texturing(const Texturing *t)
88 unsigned Renderer::allocate_effect_texunit()
90 return --state->lowest_effect_texunit;
93 void Renderer::set_material(const Material *m)
96 changed |= MATERIAL_SHDATA;
99 void Renderer::set_lighting(const Lighting *l)
102 state->lighting_matrix = state->modelview_matrix;
105 l->update_shader_data(standard_shdata, state->lighting_matrix);
106 changed |= STANDARD_SHDATA;
110 void Renderer::set_clipping(const Clipping *c)
113 state->clipping_matrix = state->modelview_matrix;
116 c->update_shader_data(standard_shdata, state->clipping_matrix);
117 changed |= STANDARD_SHDATA;
121 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
128 void Renderer::add_shader_data(const ProgramData &d)
130 if(state->shdata_count<shdata_stack.size() && shdata_stack[state->shdata_count]==&d)
131 ++state->shdata_count;
135 shdata_stack.push_back(&d);
136 state->shdata_count = shdata_stack.size();
137 changed |= SHADER_DATA;
141 void Renderer::flush_shader_data()
143 if(shdata_stack.size()>state->shdata_count)
144 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
147 void Renderer::set_vertex_setup(const VertexSetup *vs)
149 state->vertex_setup = vs;
152 void Renderer::set_winding_test(const WindingTest *w)
154 state->winding_test = w;
157 void Renderer::set_reverse_winding(bool r)
159 state->reverse_winding = r;
162 void Renderer::set_object_lod_bias(unsigned b)
164 state->object_lod_bias = b;
167 void Renderer::push_state()
169 state_stack.push_back(state_stack.back());
170 state = &state_stack.back();
173 void Renderer::pop_state()
175 if(state_stack.size()==1)
176 throw stack_underflow("Renderer::pop_state");
178 const Camera *old_camera = state->camera;
179 const Lighting *old_lighting = state->lighting;
180 const Clipping *old_clipping = state->clipping;
181 state_stack.pop_back();
182 state = &state_stack.back();
184 bool camera_changed = (state->camera!=old_camera);
189 standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
190 standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
194 standard_shdata.uniform("projection_matrix", Matrix());
195 standard_shdata.uniform("eye_world_matrix", Matrix());
197 changed |= STANDARD_SHDATA;
199 /* This actually should compare the relevant matrices rather than check for
200 camera, but in practice lighting and clipping is set right after the camera
201 and a boolean check is much faster than matrix comparison. */
202 if(state->lighting!=old_lighting || camera_changed)
206 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
207 changed |= STANDARD_SHDATA;
210 if(state->clipping!=old_clipping || camera_changed)
214 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
215 changed |= STANDARD_SHDATA;
222 if(state_stack.size()>1)
223 throw invalid_operation("Renderer::end");
227 set_camera(*default_camera);
229 standard_shdata.uniform("projection_matrix", Matrix());
230 shdata_stack.clear();
234 Texture::unbind_from(0);
237 VertexSetup::unbind();
238 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
239 WindingTest::unbind();
242 void Renderer::exclude(const Renderable &renderable)
244 excluded.insert(&renderable);
247 void Renderer::include(const Renderable &renderable)
249 excluded.erase(&renderable);
252 void Renderer::render(const Renderable &renderable, const Tag &tag)
254 if(!excluded.count(&renderable))
255 renderable.render(*this, tag);
258 void Renderer::draw(const Batch &batch)
265 void Renderer::draw_instanced(const Batch &batch, unsigned count)
269 batch.draw_instanced(count);
272 void Renderer::apply_state()
274 /* We (mostly) let the objects themselves figure out if the binding has
278 state->texturing->bind();
283 state->texture->bind_to(0);
285 Texture::unbind_from(0);
289 state->clipping->bind();
295 bool shprog_changed = (state->shprog!=Program::current());
296 state->shprog->bind();
300 standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
301 LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
302 nm = transpose(invert(nm));
303 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
304 changed = (changed&~MATRIX)|STANDARD_SHDATA;
307 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
309 state->material->get_shader_data().apply();
310 changed &= ~MATERIAL_SHDATA;
313 if((changed&STANDARD_SHDATA) || shprog_changed)
315 standard_shdata.apply();
316 changed &= ~STANDARD_SHDATA;
319 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
321 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
324 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
325 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
327 changed &= ~SHADER_DATA;
333 if(state->vertex_setup)
334 state->vertex_setup->bind();
336 VertexSetup::unbind();
338 if(state->winding_test)
340 if(state->reverse_winding)
341 state->winding_test->get_reverse().bind();
343 state->winding_test->bind();
346 WindingTest::unbind();
350 Renderer::State::State():
354 lowest_effect_texunit(TexUnit::get_n_units()),
362 reverse_winding(false),