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):
28 lighting_changed(false),
31 state_stack.reserve(16);
32 shdata_stack.reserve(32);
33 state = &state_stack.back();
35 MatrixStack::modelview().push();
38 MatrixStack::projection().push();
40 mtx_stack.load(camera->get_matrix());
41 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
45 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
46 mtx_stack.load(MatrixStack::modelview().top());
53 MatrixStack::projection().pop();
54 MatrixStack::modelview().pop();
58 Texture::unbind_from(0);
62 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
63 WindingTest::unbind();
66 void Renderer::set_texture(const Texture *t)
72 void Renderer::set_texturing(const Texturing *t)
78 unsigned Renderer::allocate_effect_texunit()
80 return --state->lowest_effect_texunit;
83 void Renderer::set_material(const Material *m)
88 void Renderer::set_lighting(const Lighting *l)
91 state->lighting_matrix = mtx_stack.top();
93 l->update_shader_data(standard_shdata, mtx_stack.top());
94 lighting_changed = true;
97 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
103 /* Even if we have no new shdata, the existing ones need to be re-applied
104 to the new program */
105 shdata_changed = true;
108 void Renderer::add_shader_data(const ProgramData &d)
110 shdata_stack.push_back(&d);
111 state->shdata_count = shdata_stack.size();
112 shdata_changed = true;
115 void Renderer::set_mesh(const Mesh *m)
120 void Renderer::set_element_buffer(const Buffer *b)
125 void Renderer::set_winding_test(const WindingTest *w)
127 state->winding_test = w;
130 void Renderer::set_reverse_winding(bool r)
132 state->reverse_winding = r;
135 void Renderer::push_state()
137 state_stack.push_back(state_stack.back());
138 state = &state_stack.back();
142 void Renderer::pop_state()
144 if(state_stack.size()==1)
145 throw stack_underflow("Renderer::pop_state");
147 state_stack.pop_back();
148 state = &state_stack.back();
149 if(shdata_stack.size()>state->shdata_count)
150 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
153 shdata_changed = true;
156 void Renderer::escape()
159 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
162 void Renderer::exclude(const Renderable &renderable)
164 excluded.insert(&renderable);
167 void Renderer::include(const Renderable &renderable)
169 excluded.erase(&renderable);
172 void Renderer::render(const Renderable &renderable, const Tag &tag)
174 if(!excluded.count(&renderable))
175 renderable.render(*this, tag);
178 void Renderer::draw(const Batch &batch)
182 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
186 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
188 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
194 void Renderer::apply_state()
196 /* We (mostly) let the objects themselves figure out if the binding has
199 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
202 state->texturing->bind();
207 state->texture->bind_to(0);
209 Texture::unbind_from(0);
215 state->material->bind();
223 MatrixStack::modelview() = state->lighting_matrix;
224 state->lighting->bind();
226 lighting_changed = false;
235 state->shprog->bind();
239 const Matrix &m = mtx_stack.top();
240 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
241 LinAl::SquareMatrix<float, 3> nm;
242 for(unsigned i=0; i<3; ++i)
243 for(unsigned j=0; j<3; ++j)
245 nm = transpose(invert(nm));
246 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
248 state->material->get_shader_data().apply();
249 standard_shdata.apply();
254 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
256 shdata_changed = false;
267 state->mesh->get_vertices().apply();
275 if(state->winding_test)
277 if(state->reverse_winding)
278 state->winding_test->get_reverse().bind();
280 state->winding_test->bind();
283 WindingTest::unbind();
285 if(legacy_bindings && mtx_changed)
287 MatrixStack::modelview() = mtx_stack.top();
293 Renderer::State::State():
296 lowest_effect_texunit(TexUnit::get_n_units()),
303 reverse_winding(false)
307 Renderer::MtxStack::MtxStack(Renderer &r):
311 void Renderer::MtxStack::update()
313 renderer.mtx_changed = true;