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 MatrixStack::projection().pop();
54 MatrixStack::modelview().pop();
59 Texture::unbind_from(0);
63 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
64 WindingTest::unbind();
67 void Renderer::set_texture(const Texture *t)
73 void Renderer::set_texturing(const Texturing *t)
79 unsigned Renderer::allocate_effect_texunit()
81 return --state->lowest_effect_texunit;
84 void Renderer::set_material(const Material *m)
89 void Renderer::set_lighting(const Lighting *l)
92 state->lighting_matrix = mtx_stack.top();
94 l->update_shader_data(standard_shdata, mtx_stack.top());
95 lighting_changed = true;
98 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
104 /* Even if we have no new shdata, the existing ones need to be re-applied
105 to the new program */
106 shdata_changed = true;
109 void Renderer::add_shader_data(const ProgramData &d)
111 shdata_stack.push_back(&d);
112 state->shdata_count = shdata_stack.size();
113 shdata_changed = true;
116 void Renderer::set_mesh(const Mesh *m)
121 void Renderer::set_element_buffer(const Buffer *b)
126 void Renderer::set_winding_test(const WindingTest *w)
128 state->winding_test = w;
131 void Renderer::set_reverse_winding(bool r)
133 state->reverse_winding = r;
136 void Renderer::push_state()
138 state_stack.push_back(state_stack.back());
139 state = &state_stack.back();
143 void Renderer::pop_state()
145 if(state_stack.size()==1)
146 throw stack_underflow("Renderer::pop_state");
148 state_stack.pop_back();
149 state = &state_stack.back();
150 if(shdata_stack.size()>state->shdata_count)
151 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
154 shdata_changed = true;
157 void Renderer::escape()
160 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
161 matrices_loaded = false;
164 void Renderer::exclude(const Renderable &renderable)
166 excluded.insert(&renderable);
169 void Renderer::include(const Renderable &renderable)
171 excluded.erase(&renderable);
174 void Renderer::render(const Renderable &renderable, const Tag &tag)
176 if(!excluded.count(&renderable))
177 renderable.render(*this, tag);
180 void Renderer::draw(const Batch &batch)
184 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
188 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
190 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
196 void Renderer::apply_state()
198 /* We (mostly) let the objects themselves figure out if the binding has
201 bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
204 state->texturing->bind();
209 state->texture->bind_to(0);
211 Texture::unbind_from(0);
217 state->material->bind();
225 MatrixStack::modelview() = state->lighting_matrix;
226 state->lighting->bind();
228 lighting_changed = false;
237 state->shprog->bind();
241 const Matrix &m = mtx_stack.top();
242 standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
243 LinAl::SquareMatrix<float, 3> nm;
244 for(unsigned i=0; i<3; ++i)
245 for(unsigned j=0; j<3; ++j)
247 nm = transpose(invert(nm));
248 standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
250 state->material->get_shader_data().apply();
251 standard_shdata.apply();
256 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
258 shdata_changed = false;
269 state->mesh->get_vertices().apply();
277 if(state->winding_test)
279 if(state->reverse_winding)
280 state->winding_test->get_reverse().bind();
282 state->winding_test->bind();
285 WindingTest::unbind();
291 MatrixStack::modelview().push();
294 MatrixStack::projection().push();
297 matrices_loaded = true;
302 MatrixStack::modelview() = mtx_stack.top();
309 Renderer::State::State():
312 lowest_effect_texunit(TexUnit::get_n_units()),
319 reverse_winding(false)
323 Renderer::MtxStack::MtxStack(Renderer &r):
327 void Renderer::MtxStack::update()
329 renderer.mtx_changed = true;