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):
27 matrices_loaded(false),
32 state_stack.reserve(16);
33 shdata_stack.reserve(32);
34 state = &state_stack.back();
44 void Renderer::begin(const Camera *c)
46 if(state_stack.size()>1)
47 throw invalid_operation("Renderer::begin");
55 mtx_stack.load(camera->get_matrix());
56 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
60 mtx_stack.load(MatrixStack::modelview().top());
61 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
65 void Renderer::set_matrix(const Matrix &matrix)
67 mtx_stack.load(matrix);
70 void Renderer::transform(const Matrix &matrix)
75 void Renderer::set_texture(const Texture *t)
81 void Renderer::set_texturing(const Texturing *t)
87 unsigned Renderer::allocate_effect_texunit()
89 return --state->lowest_effect_texunit;
92 void Renderer::set_material(const Material *m)
97 void Renderer::set_lighting(const Lighting *l)
100 state->lighting_matrix = mtx_stack.top();
102 l->update_shader_data(standard_shdata, mtx_stack.top());
106 void Renderer::set_clipping(const Clipping *c)
109 state->clipping_matrix = mtx_stack.top();
111 c->update_shader_data(standard_shdata, mtx_stack.top());
115 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
122 void Renderer::add_shader_data(const ProgramData &d)
124 shdata_stack.push_back(&d);
125 state->shdata_count = shdata_stack.size();
126 changed |= SHADER_DATA;
129 void Renderer::set_mesh(const Mesh *m)
134 void Renderer::set_winding_test(const WindingTest *w)
136 state->winding_test = w;
139 void Renderer::set_reverse_winding(bool r)
141 state->reverse_winding = r;
144 void Renderer::push_state()
146 state_stack.push_back(state_stack.back());
147 state = &state_stack.back();
151 void Renderer::pop_state()
153 if(state_stack.size()==1)
154 throw stack_underflow("Renderer::pop_state");
156 const Lighting *old_lighting = state->lighting;
157 const Clipping *old_clipping = state->clipping;
158 state_stack.pop_back();
159 state = &state_stack.back();
160 if(shdata_stack.size()>state->shdata_count)
162 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
163 changed |= SHADER_DATA;
165 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
168 if(state->lighting!=old_lighting)
171 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
174 if(state->clipping!=old_clipping)
177 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
182 void Renderer::escape()
185 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
186 matrices_loaded = false;
191 if(state_stack.size()>1)
192 throw invalid_operation("Renderer::end");
198 Texture::unbind_from(0);
203 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
204 WindingTest::unbind();
207 void Renderer::exclude(const Renderable &renderable)
209 excluded.insert(&renderable);
212 void Renderer::include(const Renderable &renderable)
214 excluded.erase(&renderable);
217 void Renderer::render(const Renderable &renderable, const Tag &tag)
219 if(!excluded.count(&renderable))
220 renderable.render(*this, tag);
223 void Renderer::draw(const Batch &batch)
227 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
228 if(state->mesh && legacy_bindings)
230 if(const Buffer *ibuf = state->mesh->get_index_buffer())
231 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
233 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
239 void Renderer::apply_state()
241 /* We (mostly) let the objects themselves figure out if the binding has
244 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
247 state->texturing->bind();
252 state->texture->bind_to(0);
254 Texture::unbind_from(0);
260 state->material->bind();
268 MatrixStack::modelview() = state->lighting_matrix;
269 state->lighting->bind();
270 changed = (changed&~LIGHTING)|LEGACY_MATRIX;
283 MatrixStack::modelview() = state->clipping_matrix;
284 state->clipping->bind(true);
285 changed = (changed&~CLIPPING)|LEGACY_MATRIX;
289 state->clipping->bind(false);
296 bool shprog_changed = (state->shprog!=Program::current());
297 state->shprog->bind();
301 if(changed&MODERN_MATRIX)
303 const Matrix &m = mtx_stack.top();
304 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
305 LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
306 nm = transpose(invert(nm));
307 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
308 changed &= ~MODERN_MATRIX;
312 state->material->get_shader_data().apply();
313 standard_shdata.apply();
316 if((changed&SHADER_DATA) || shprog_changed)
318 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
321 for(; i!=shdata_stack.end(); ++i)
323 changed &= ~SHADER_DATA;
324 shdata_applied = shdata_stack.size();
335 state->mesh->get_vertices().apply();
343 if(state->winding_test)
345 if(state->reverse_winding)
346 state->winding_test->get_reverse().bind();
348 state->winding_test->bind();
351 WindingTest::unbind();
357 MatrixStack::modelview().push();
360 MatrixStack::projection().push();
363 matrices_loaded = true;
366 if(changed&LEGACY_MATRIX)
368 MatrixStack::modelview() = mtx_stack.top();
369 changed &= ~LEGACY_MATRIX;
374 void Renderer::reset_state()
380 MatrixStack::projection().pop();
381 MatrixStack::modelview().pop();
382 matrices_loaded = false;
389 Renderer::State::State():
392 lowest_effect_texunit(TexUnit::get_n_units()),
400 reverse_winding(false)
404 Renderer::MtxStack::MtxStack(Renderer &r):
408 void Renderer::MtxStack::update()
410 renderer.changed |= MATRIX;