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),
29 lighting_changed(false),
32 state_stack.reserve(16);
33 shdata_stack.reserve(32);
34 state = &state_stack.back();
38 mtx_stack.load(camera->get_matrix());
39 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
43 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
44 mtx_stack.load(MatrixStack::modelview().top());
53 void Renderer::set_texture(const Texture *t)
59 void Renderer::set_texturing(const Texturing *t)
65 unsigned Renderer::allocate_effect_texunit()
67 return --state->lowest_effect_texunit;
70 void Renderer::set_material(const Material *m)
75 void Renderer::set_lighting(const Lighting *l)
78 state->lighting_matrix = mtx_stack.top();
80 l->update_shader_data(standard_shdata, mtx_stack.top());
81 lighting_changed = true;
84 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
90 /* Even if we have no new shdata, the existing ones need to be re-applied
92 shdata_changed = true;
95 void Renderer::add_shader_data(const ProgramData &d)
97 shdata_stack.push_back(&d);
98 state->shdata_count = shdata_stack.size();
99 shdata_changed = true;
102 void Renderer::set_mesh(const Mesh *m)
107 void Renderer::set_element_buffer(const Buffer *b)
112 void Renderer::set_winding_test(const WindingTest *w)
114 state->winding_test = w;
117 void Renderer::set_reverse_winding(bool r)
119 state->reverse_winding = r;
122 void Renderer::push_state()
124 state_stack.push_back(state_stack.back());
125 state = &state_stack.back();
129 void Renderer::pop_state()
131 if(state_stack.size()==1)
132 throw stack_underflow("Renderer::pop_state");
134 state_stack.pop_back();
135 state = &state_stack.back();
136 if(shdata_stack.size()>state->shdata_count)
137 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
140 shdata_changed = true;
143 void Renderer::escape()
146 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
147 matrices_loaded = false;
152 if(state_stack.size()>1)
153 throw invalid_operation("Renderer::end");
158 MatrixStack::projection().pop();
159 MatrixStack::modelview().pop();
164 Texture::unbind_from(0);
168 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
169 WindingTest::unbind();
174 void Renderer::exclude(const Renderable &renderable)
176 excluded.insert(&renderable);
179 void Renderer::include(const Renderable &renderable)
181 excluded.erase(&renderable);
184 void Renderer::render(const Renderable &renderable, const Tag &tag)
186 if(!excluded.count(&renderable))
187 renderable.render(*this, tag);
190 void Renderer::draw(const Batch &batch)
194 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
198 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
200 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
206 void Renderer::apply_state()
208 /* We (mostly) let the objects themselves figure out if the binding has
211 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
214 state->texturing->bind();
219 state->texture->bind_to(0);
221 Texture::unbind_from(0);
227 state->material->bind();
235 MatrixStack::modelview() = state->lighting_matrix;
236 state->lighting->bind();
238 lighting_changed = false;
247 state->shprog->bind();
251 const Matrix &m = mtx_stack.top();
252 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
253 LinAl::SquareMatrix<float, 3> nm;
254 for(unsigned i=0; i<3; ++i)
255 for(unsigned j=0; j<3; ++j)
257 nm = transpose(invert(nm));
258 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
260 state->material->get_shader_data().apply();
261 standard_shdata.apply();
266 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
268 shdata_changed = false;
279 state->mesh->get_vertices().apply();
287 if(state->winding_test)
289 if(state->reverse_winding)
290 state->winding_test->get_reverse().bind();
292 state->winding_test->bind();
295 WindingTest::unbind();
301 MatrixStack::modelview().push();
304 MatrixStack::projection().push();
307 matrices_loaded = true;
312 MatrixStack::modelview() = mtx_stack.top();
319 Renderer::State::State():
322 lowest_effect_texunit(TexUnit::get_n_units()),
329 reverse_winding(false)
333 Renderer::MtxStack::MtxStack(Renderer &r):
337 void Renderer::MtxStack::update()
339 renderer.mtx_changed = true;