8 #include "programdata.h"
9 #include "renderable.h"
12 #include "texturing.h"
13 #include "vertexarray.h"
14 #include "windingtest.h"
21 Renderer::Renderer(const Camera *c):
29 state_stack.reserve(16);
30 shdata_stack.reserve(32);
31 state = &state_stack.back();
33 MatrixStack::modelview().push();
36 MatrixStack::projection().push();
38 mtx_stack.load(camera->get_matrix());
41 mtx_stack.load(MatrixStack::modelview().top());
47 MatrixStack::projection().pop();
48 MatrixStack::modelview().pop();
51 Texture::unbind_from(0);
54 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
55 WindingTest::unbind();
58 void Renderer::set_texture(const Texture *t)
64 void Renderer::set_texturing(const Texturing *t)
70 void Renderer::set_material(const Material *m)
75 void Renderer::set_lighting(const Lighting *l)
78 state->lighting_matrix = mtx_stack.top();
79 lighting_changed = true;
82 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
88 /* Even if we have no new shdata, the existing ones need to be re-applied
90 shdata_changed = true;
93 void Renderer::add_shader_data(const ProgramData &d)
95 shdata_stack.push_back(&d);
96 state->shdata_count = shdata_stack.size();
97 shdata_changed = true;
100 void Renderer::set_vertex_array(const VertexArray *a)
105 void Renderer::set_element_buffer(const Buffer *b)
110 void Renderer::set_winding_test(const WindingTest *w)
112 state->winding_test = w;
115 void Renderer::set_reverse_winding(bool r)
117 state->reverse_winding = r;
120 void Renderer::push_state()
122 state_stack.push_back(state_stack.back());
123 state = &state_stack.back();
127 void Renderer::pop_state()
129 if(state_stack.size()==1)
130 throw stack_underflow("Renderer::pop_state");
132 state_stack.pop_back();
133 state = &state_stack.back();
134 if(shdata_stack.size()>state->shdata_count)
135 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
138 shdata_changed = true;
141 void Renderer::escape()
144 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
147 void Renderer::exclude(const Renderable &renderable)
149 excluded.insert(&renderable);
152 void Renderer::include(const Renderable &renderable)
154 excluded.erase(&renderable);
157 void Renderer::render(const Renderable &renderable, const Tag &tag)
159 if(!excluded.count(&renderable))
160 renderable.render(*this, tag);
163 void Renderer::draw(const Batch &batch)
166 throw invalid_operation("Renderer::draw");
170 vertex_array->apply();
173 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
175 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
180 void Renderer::apply_state()
182 /* We (mostly) let the objects themselves figure out if the binding has
186 state->texturing->bind();
191 state->texture->bind_to(0);
193 Texture::unbind_from(0);
197 state->material->bind();
205 MatrixStack::modelview() = state->lighting_matrix;
206 state->lighting->bind();
208 lighting_changed = false;
216 state->shprog->bind();
219 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
221 shdata_changed = false;
227 if(state->winding_test)
229 if(state->reverse_winding)
230 state->winding_test->get_reverse().bind();
232 state->winding_test->bind();
235 WindingTest::unbind();
239 MatrixStack::modelview() = mtx_stack.top();
245 Renderer::State::State():
256 Renderer::MtxStack::MtxStack(Renderer &r):
260 void Renderer::MtxStack::update()
262 renderer.mtx_changed = true;