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)
95 changed |= MATERIAL_SHDATA;
98 void Renderer::set_lighting(const Lighting *l)
101 state->lighting_matrix = mtx_stack.top();
104 l->update_shader_data(standard_shdata, mtx_stack.top());
105 changed |= STANDARD_SHDATA;
107 changed |= LEGACY_LIGHTING;
110 void Renderer::set_clipping(const Clipping *c)
113 state->clipping_matrix = mtx_stack.top();
115 c->update_shader_data(standard_shdata, mtx_stack.top());
116 changed |= LEGACY_CLIPPING;
119 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
126 void Renderer::add_shader_data(const ProgramData &d)
128 shdata_stack.push_back(&d);
129 state->shdata_count = shdata_stack.size();
130 changed |= SHADER_DATA;
133 void Renderer::set_mesh(const Mesh *m)
138 void Renderer::set_winding_test(const WindingTest *w)
140 state->winding_test = w;
143 void Renderer::set_reverse_winding(bool r)
145 state->reverse_winding = r;
148 void Renderer::push_state()
150 state_stack.push_back(state_stack.back());
151 state = &state_stack.back();
155 void Renderer::pop_state()
157 if(state_stack.size()==1)
158 throw stack_underflow("Renderer::pop_state");
160 const Lighting *old_lighting = state->lighting;
161 const Clipping *old_clipping = state->clipping;
162 state_stack.pop_back();
163 state = &state_stack.back();
164 if(shdata_stack.size()>state->shdata_count)
166 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
167 changed |= SHADER_DATA;
169 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
172 if(state->lighting!=old_lighting)
175 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
176 changed |= LEGACY_LIGHTING;
178 if(state->clipping!=old_clipping)
181 state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
182 changed |= LEGACY_CLIPPING;
186 void Renderer::escape()
189 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
190 matrices_loaded = false;
195 if(state_stack.size()>1)
196 throw invalid_operation("Renderer::end");
202 Texture::unbind_from(0);
207 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
208 WindingTest::unbind();
211 void Renderer::exclude(const Renderable &renderable)
213 excluded.insert(&renderable);
216 void Renderer::include(const Renderable &renderable)
218 excluded.erase(&renderable);
221 void Renderer::render(const Renderable &renderable, const Tag &tag)
223 if(!excluded.count(&renderable))
224 renderable.render(*this, tag);
227 void Renderer::draw(const Batch &batch)
231 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
232 if(state->mesh && legacy_bindings)
234 if(const Buffer *ibuf = state->mesh->get_index_buffer())
235 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
237 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
243 void Renderer::apply_state()
245 /* We (mostly) let the objects themselves figure out if the binding has
248 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
251 state->texturing->bind();
256 state->texture->bind_to(0);
258 Texture::unbind_from(0);
264 state->material->bind();
268 if(changed&LEGACY_LIGHTING)
272 MatrixStack::modelview() = state->lighting_matrix;
273 state->lighting->bind();
274 changed = (changed&~LEGACY_LIGHTING)|LEGACY_MATRIX;
285 if(changed&LEGACY_CLIPPING)
287 MatrixStack::modelview() = state->clipping_matrix;
288 state->clipping->bind(true);
289 changed = (changed&~LEGACY_CLIPPING)|LEGACY_MATRIX;
293 state->clipping->bind(false);
300 bool shprog_changed = (state->shprog!=Program::current());
301 state->shprog->bind();
305 if(changed&MODERN_MATRIX)
307 const Matrix &m = mtx_stack.top();
308 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
309 LinAl::SquareMatrix<float, 3> nm = m.block<3, 3>(0, 0);
310 nm = transpose(invert(nm));
311 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
312 changed = (changed&~MODERN_MATRIX)|STANDARD_SHDATA;
315 if(state->material && (changed&MATERIAL_SHDATA))
317 state->material->get_shader_data().apply();
318 changed &= ~MATERIAL_SHDATA;
321 if(changed&STANDARD_SHDATA)
323 standard_shdata.apply();
324 changed &= ~STANDARD_SHDATA;
328 if((changed&SHADER_DATA) || shprog_changed)
330 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
333 for(; i!=shdata_stack.end(); ++i)
335 changed &= ~SHADER_DATA;
336 shdata_applied = shdata_stack.size();
347 state->mesh->get_vertices().apply();
355 if(state->winding_test)
357 if(state->reverse_winding)
358 state->winding_test->get_reverse().bind();
360 state->winding_test->bind();
363 WindingTest::unbind();
369 MatrixStack::modelview().push();
372 MatrixStack::projection().push();
375 matrices_loaded = true;
378 if(changed&LEGACY_MATRIX)
380 MatrixStack::modelview() = mtx_stack.top();
381 changed &= ~LEGACY_MATRIX;
386 void Renderer::reset_state()
392 MatrixStack::projection().pop();
393 MatrixStack::modelview().pop();
394 matrices_loaded = false;
401 Renderer::State::State():
404 lowest_effect_texunit(TexUnit::get_n_units()),
412 reverse_winding(false)
416 Renderer::MtxStack::MtxStack(Renderer &r):
420 void Renderer::MtxStack::update()
422 renderer.changed |= MATRIX;