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):
28 state_stack.reserve(16);
29 shdata_stack.reserve(32);
30 state = &state_stack.back();
32 MatrixStack::modelview().push();
35 MatrixStack::projection().push();
37 mtx_stack.load(camera->get_matrix());
40 mtx_stack.load(MatrixStack::modelview().top());
46 MatrixStack::projection().pop();
47 MatrixStack::modelview().pop();
50 Texture::unbind_from(0);
53 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
54 WindingTest::unbind();
57 void Renderer::set_texture(const Texture *t)
63 void Renderer::set_texturing(const Texturing *t)
69 void Renderer::set_material(const Material *m)
74 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
80 /* Even if we have no new shdata, the existing ones need to be re-applied
82 shdata_changed = true;
85 void Renderer::add_shader_data(const ProgramData &d)
87 shdata_stack.push_back(&d);
88 state->shdata_count = shdata_stack.size();
89 shdata_changed = true;
92 void Renderer::set_vertex_array(const VertexArray *a)
97 void Renderer::set_element_buffer(const Buffer *b)
102 void Renderer::set_winding_test(const WindingTest *w)
104 state->winding_test = w;
107 void Renderer::push_state()
109 state_stack.push_back(state_stack.back());
110 state = &state_stack.back();
114 void Renderer::pop_state()
116 if(state_stack.size()==1)
117 throw stack_underflow("Renderer::pop_state");
119 state_stack.pop_back();
120 state = &state_stack.back();
121 if(shdata_stack.size()>state->shdata_count)
122 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
125 shdata_changed = true;
128 void Renderer::escape()
131 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
134 void Renderer::exclude(const Renderable &renderable)
136 excluded.insert(&renderable);
139 void Renderer::include(const Renderable &renderable)
141 excluded.erase(&renderable);
144 void Renderer::render(const Renderable &renderable, const Tag &tag)
146 if(!excluded.count(&renderable))
147 renderable.render(*this, tag);
150 void Renderer::draw(const Batch &batch)
153 throw invalid_operation("Renderer::draw");
157 vertex_array->apply();
160 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
162 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
167 void Renderer::apply_state()
169 // We let the objects themselves figure out if the binding has changed
172 state->texturing->bind();
177 state->texture->bind_to(0);
179 Texture::unbind_from(0);
183 state->material->bind();
189 state->shprog->bind();
192 for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
194 shdata_changed = false;
200 if(state->winding_test)
201 state->winding_test->bind();
203 WindingTest::unbind();
207 MatrixStack::modelview() = mtx_stack.top();
213 Renderer::State::State():
223 Renderer::MtxStack::MtxStack(Renderer &r):
227 void Renderer::MtxStack::update()
229 renderer.mtx_changed = true;