9 #include "programdata.h"
10 #include "renderable.h"
13 #include "texturing.h"
15 #include "vertexarray.h"
16 #include "windingtest.h"
23 Renderer::Renderer(const Camera *c):
26 matrices_loaded(false),
31 state_stack.reserve(16);
32 shdata_stack.reserve(32);
33 state = &state_stack.back();
43 void Renderer::begin(const Camera *c)
45 if(state_stack.size()>1)
46 throw invalid_operation("Renderer::begin");
54 mtx_stack.load(camera->get_matrix());
55 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
59 mtx_stack.load(MatrixStack::modelview().top());
60 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
64 void Renderer::set_matrix(const Matrix &matrix)
66 mtx_stack.load(matrix);
69 void Renderer::transform(const Matrix &matrix)
74 void Renderer::set_texture(const Texture *t)
80 void Renderer::set_texturing(const Texturing *t)
86 unsigned Renderer::allocate_effect_texunit()
88 return --state->lowest_effect_texunit;
91 void Renderer::set_material(const Material *m)
96 void Renderer::set_lighting(const Lighting *l)
99 state->lighting_matrix = mtx_stack.top();
101 l->update_shader_data(standard_shdata, mtx_stack.top());
105 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
112 void Renderer::add_shader_data(const ProgramData &d)
114 shdata_stack.push_back(&d);
115 state->shdata_count = shdata_stack.size();
116 changed |= SHADER_DATA;
119 void Renderer::set_mesh(const Mesh *m)
124 void Renderer::set_winding_test(const WindingTest *w)
126 state->winding_test = w;
129 void Renderer::set_reverse_winding(bool r)
131 state->reverse_winding = r;
134 void Renderer::push_state()
136 state_stack.push_back(state_stack.back());
137 state = &state_stack.back();
141 void Renderer::pop_state()
143 if(state_stack.size()==1)
144 throw stack_underflow("Renderer::pop_state");
146 const Lighting *old_lighting = state->lighting;
147 state_stack.pop_back();
148 state = &state_stack.back();
149 if(shdata_stack.size()>state->shdata_count)
151 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
152 changed |= SHADER_DATA;
154 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
157 if(state->lighting!=old_lighting)
160 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
165 void Renderer::escape()
168 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
169 matrices_loaded = false;
174 if(state_stack.size()>1)
175 throw invalid_operation("Renderer::end");
181 Texture::unbind_from(0);
185 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
186 WindingTest::unbind();
189 void Renderer::exclude(const Renderable &renderable)
191 excluded.insert(&renderable);
194 void Renderer::include(const Renderable &renderable)
196 excluded.erase(&renderable);
199 void Renderer::render(const Renderable &renderable, const Tag &tag)
201 if(!excluded.count(&renderable))
202 renderable.render(*this, tag);
205 void Renderer::draw(const Batch &batch)
209 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
210 if(state->mesh && legacy_bindings)
212 if(const Buffer *ibuf = state->mesh->get_index_buffer())
213 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
215 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
221 void Renderer::apply_state()
223 /* We (mostly) let the objects themselves figure out if the binding has
226 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
229 state->texturing->bind();
234 state->texture->bind_to(0);
236 Texture::unbind_from(0);
242 state->material->bind();
250 MatrixStack::modelview() = state->lighting_matrix;
251 state->lighting->bind();
252 changed = (changed&~LIGHTING)|LEGACY_MATRIX;
261 bool shprog_changed = (state->shprog!=Program::current());
262 state->shprog->bind();
266 if(changed&MODERN_MATRIX)
268 const Matrix &m = mtx_stack.top();
269 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
270 LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
271 nm = transpose(invert(nm));
272 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
273 changed &= ~MODERN_MATRIX;
277 state->material->get_shader_data().apply();
278 standard_shdata.apply();
281 if((changed&SHADER_DATA) || shprog_changed)
283 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
286 for(; i!=shdata_stack.end(); ++i)
288 changed &= ~SHADER_DATA;
289 shdata_applied = shdata_stack.size();
300 state->mesh->get_vertices().apply();
308 if(state->winding_test)
310 if(state->reverse_winding)
311 state->winding_test->get_reverse().bind();
313 state->winding_test->bind();
316 WindingTest::unbind();
322 MatrixStack::modelview().push();
325 MatrixStack::projection().push();
328 matrices_loaded = true;
331 if(changed&LEGACY_MATRIX)
333 MatrixStack::modelview() = mtx_stack.top();
334 changed &= ~LEGACY_MATRIX;
339 void Renderer::reset_state()
345 MatrixStack::projection().pop();
346 MatrixStack::modelview().pop();
347 matrices_loaded = false;
354 Renderer::State::State():
357 lowest_effect_texunit(TexUnit::get_n_units()),
364 reverse_winding(false)
368 Renderer::MtxStack::MtxStack(Renderer &r):
372 void Renderer::MtxStack::update()
374 renderer.changed |= MATRIX;