7 #include "programdata.h"
8 #include "renderable.h"
11 #include "texturing.h"
12 #include "vertexarray.h"
13 #include "windingtest.h"
20 Renderer::Renderer(const Camera *c):
26 vertex_array_changed(false),
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_shader_program(const Program *p, const ProgramData *d)
81 /* Even if we have no new shdata, the existing ones need to be re-applied
83 shdata_changed = true;
86 void Renderer::add_shader_data(const ProgramData &d)
88 shdata_stack.push_back(&d);
89 state->shdata_count = shdata_stack.size();
90 shdata_changed = true;
93 void Renderer::set_vertex_array(const VertexArray *a)
95 vertex_array_changed = (a!=vertex_array);
99 void Renderer::set_element_buffer(const Buffer *b)
104 void Renderer::set_winding_test(const WindingTest *w)
106 state->winding_test = w;
109 void Renderer::push_state()
111 state_stack.push_back(state_stack.back());
112 state = &state_stack.back();
116 void Renderer::pop_state()
118 if(state_stack.size()==1)
119 throw stack_underflow("Renderer::pop_state");
121 state_stack.pop_back();
122 state = &state_stack.back();
123 if(shdata_stack.size()>state->shdata_count)
124 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
127 shdata_changed = true;
130 void Renderer::escape()
133 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
136 void Renderer::exclude(const Renderable &renderable)
138 excluded.insert(&renderable);
141 void Renderer::include(const Renderable &renderable)
143 excluded.erase(&renderable);
146 void Renderer::render(const Renderable &renderable, const Tag &tag)
148 if(!excluded.count(&renderable))
149 renderable.render(*this, tag);
152 void Renderer::draw(const Batch &batch)
155 throw invalid_operation("Renderer::draw");
159 // Until VertexArray acquires VAO support and becomes Bindable
160 if(vertex_array_changed)
162 vertex_array->apply();
163 vertex_array_changed = false;
167 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
169 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
174 void Renderer::apply_state()
176 // We let the objects themselves figure out if the binding has changed
179 state->texturing->bind();
184 state->texture->bind_to(0);
186 Texture::unbind_from(0);
190 state->material->bind();
196 state->shprog->bind();
199 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
201 shdata_changed = false;
207 if(state->winding_test)
208 state->winding_test->bind();
210 WindingTest::unbind();
214 MatrixStack::modelview() = mtx_stack.top();
220 Renderer::State::State():
230 Renderer::MtxStack::MtxStack(Renderer &r):
234 void Renderer::MtxStack::update()
236 renderer.mtx_changed = true;