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 /* XXX I'm not happy with this, but can't come up with anything better
95 const_cast<Lighting *>(l)->update_shader_data(mtx_stack.top());
96 lighting_changed = true;
99 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
105 /* Even if we have no new shdata, the existing ones need to be re-applied
106 to the new program */
107 shdata_changed = true;
110 void Renderer::add_shader_data(const ProgramData &d)
112 shdata_stack.push_back(&d);
113 state->shdata_count = shdata_stack.size();
114 shdata_changed = true;
117 void Renderer::set_mesh(const Mesh *m)
122 void Renderer::set_element_buffer(const Buffer *b)
127 void Renderer::set_winding_test(const WindingTest *w)
129 state->winding_test = w;
132 void Renderer::set_reverse_winding(bool r)
134 state->reverse_winding = r;
137 void Renderer::push_state()
139 state_stack.push_back(state_stack.back());
140 state = &state_stack.back();
144 void Renderer::pop_state()
146 if(state_stack.size()==1)
147 throw stack_underflow("Renderer::pop_state");
149 state_stack.pop_back();
150 state = &state_stack.back();
151 if(shdata_stack.size()>state->shdata_count)
152 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
155 shdata_changed = true;
158 void Renderer::escape()
161 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
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->lighting->get_shader_data().apply();
252 state->material->get_shader_data().apply();
253 standard_shdata.apply();
258 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
260 shdata_changed = false;
271 state->mesh->get_vertices().apply();
279 if(state->winding_test)
281 if(state->reverse_winding)
282 state->winding_test->get_reverse().bind();
284 state->winding_test->bind();
287 WindingTest::unbind();
289 if(legacy_bindings && mtx_changed)
291 MatrixStack::modelview() = mtx_stack.top();
297 Renderer::State::State():
300 lowest_effect_texunit(TexUnit::get_n_units()),
307 reverse_winding(false)
311 Renderer::MtxStack::MtxStack(Renderer &r):
315 void Renderer::MtxStack::update()
317 renderer.mtx_changed = true;