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_texture(const Texture *t)
70 void Renderer::set_texturing(const Texturing *t)
76 unsigned Renderer::allocate_effect_texunit()
78 return --state->lowest_effect_texunit;
81 void Renderer::set_material(const Material *m)
86 void Renderer::set_lighting(const Lighting *l)
89 state->lighting_matrix = mtx_stack.top();
91 l->update_shader_data(standard_shdata, mtx_stack.top());
95 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
102 void Renderer::add_shader_data(const ProgramData &d)
104 shdata_stack.push_back(&d);
105 state->shdata_count = shdata_stack.size();
106 changed |= SHADER_DATA;
109 void Renderer::set_mesh(const Mesh *m)
114 void Renderer::set_winding_test(const WindingTest *w)
116 state->winding_test = w;
119 void Renderer::set_reverse_winding(bool r)
121 state->reverse_winding = r;
124 void Renderer::push_state()
126 state_stack.push_back(state_stack.back());
127 state = &state_stack.back();
131 void Renderer::pop_state()
133 if(state_stack.size()==1)
134 throw stack_underflow("Renderer::pop_state");
136 const Lighting *old_lighting = state->lighting;
137 state_stack.pop_back();
138 state = &state_stack.back();
139 if(shdata_stack.size()>state->shdata_count)
141 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
142 changed |= SHADER_DATA;
144 shdata_applied = min<unsigned>(shdata_applied, shdata_stack.size());
147 if(state->lighting!=old_lighting)
150 state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
155 void Renderer::escape()
158 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
159 matrices_loaded = false;
164 if(state_stack.size()>1)
165 throw invalid_operation("Renderer::end");
171 Texture::unbind_from(0);
175 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
176 WindingTest::unbind();
179 void Renderer::exclude(const Renderable &renderable)
181 excluded.insert(&renderable);
184 void Renderer::include(const Renderable &renderable)
186 excluded.erase(&renderable);
189 void Renderer::render(const Renderable &renderable, const Tag &tag)
191 if(!excluded.count(&renderable))
192 renderable.render(*this, tag);
195 void Renderer::draw(const Batch &batch)
199 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
200 if(state->mesh && legacy_bindings)
202 if(const Buffer *ibuf = state->mesh->get_index_buffer())
203 ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
205 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
211 void Renderer::apply_state()
213 /* We (mostly) let the objects themselves figure out if the binding has
216 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
219 state->texturing->bind();
224 state->texture->bind_to(0);
226 Texture::unbind_from(0);
232 state->material->bind();
240 MatrixStack::modelview() = state->lighting_matrix;
241 state->lighting->bind();
242 changed = (changed&~LIGHTING)|LEGACY_MATRIX;
251 bool shprog_changed = (state->shprog!=Program::current());
252 state->shprog->bind();
256 if(changed&MODERN_MATRIX)
258 const Matrix &m = mtx_stack.top();
259 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
260 LinAl::SquareMatrix<float, 3> nm;
261 for(unsigned i=0; i<3; ++i)
262 for(unsigned j=0; j<3; ++j)
264 nm = transpose(invert(nm));
265 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
266 changed &= ~MODERN_MATRIX;
270 state->material->get_shader_data().apply();
271 standard_shdata.apply();
274 if((changed&SHADER_DATA) || shprog_changed)
276 vector<const ProgramData *>::const_iterator i = shdata_stack.begin();
279 for(; i!=shdata_stack.end(); ++i)
281 changed &= ~SHADER_DATA;
282 shdata_applied = shdata_stack.size();
293 state->mesh->get_vertices().apply();
301 if(state->winding_test)
303 if(state->reverse_winding)
304 state->winding_test->get_reverse().bind();
306 state->winding_test->bind();
309 WindingTest::unbind();
315 MatrixStack::modelview().push();
318 MatrixStack::projection().push();
321 matrices_loaded = true;
324 if(changed&LEGACY_MATRIX)
326 MatrixStack::modelview() = mtx_stack.top();
327 changed &= ~LEGACY_MATRIX;
332 void Renderer::reset_state()
338 MatrixStack::projection().pop();
339 MatrixStack::modelview().pop();
340 matrices_loaded = false;
347 Renderer::State::State():
350 lowest_effect_texunit(TexUnit::get_n_units()),
357 reverse_winding(false)
361 Renderer::MtxStack::MtxStack(Renderer &r):
365 void Renderer::MtxStack::update()
367 renderer.changed |= MATRIX;