7 #include "programdata.h"
10 #include "texturing.h"
11 #include "vertexarray.h"
12 #include "windingtest.h"
19 Renderer::Renderer(const Camera *c):
24 state(&state_stack.back()),
26 vertex_array_changed(false),
29 MatrixStack::modelview().push();
32 MatrixStack::projection().push();
34 mtx_stack.load(camera->get_matrix());
37 mtx_stack.load(MatrixStack::modelview().top());
43 MatrixStack::projection().pop();
44 MatrixStack::modelview().pop();
47 Texture::unbind_from(0);
50 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
51 WindingTest::unbind();
54 void Renderer::set_texture(const Texture *t)
60 void Renderer::set_texturing(const Texturing *t)
66 void Renderer::set_material(const Material *m)
71 void Renderer::set_shader(const Program *p, const ProgramData *d)
75 state->shdata.assign(1, d);
77 state->shdata.clear();
80 void Renderer::add_shader_data(const ProgramData *d)
83 throw invalid_operation("Renderer::add_shader_data");
85 state->shdata.push_back(d);
88 void Renderer::set_vertex_array(const VertexArray *a)
90 vertex_array_changed = (a!=vertex_array);
94 void Renderer::set_element_buffer(const Buffer *b)
99 void Renderer::set_winding_test(const WindingTest *w)
101 state->winding_test = w;
104 void Renderer::push_state()
106 state_stack.push_back(state_stack.back());
107 state = &state_stack.back();
111 void Renderer::pop_state()
113 if(state_stack.size()==1)
114 throw stack_underflow("Renderer::pop_state");
116 state_stack.pop_back();
117 state = &state_stack.back();
122 void Renderer::escape()
125 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
128 void Renderer::draw(const Batch &batch)
131 throw invalid_operation("Renderer::draw");
135 // Until VertexArray acquires VAO support and becomes Bindable
136 if(vertex_array_changed)
138 vertex_array->apply();
139 vertex_array_changed = false;
143 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
145 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
150 void Renderer::apply_state()
152 // We let the objects themselves figure out if the binding has changed
155 state->texturing->bind();
160 state->texture->bind_to(0);
162 Texture::unbind_from(0);
166 state->material->bind();
172 state->shprog->bind();
173 for(vector<const ProgramData *>::const_iterator i=state->shdata.begin(); i!=state->shdata.end(); ++i)
179 if(state->winding_test)
180 state->winding_test->bind();
182 WindingTest::unbind();
186 MatrixStack::modelview() = mtx_stack.top();
192 Renderer::State::State():
201 Renderer::MtxStack::MtxStack(Renderer &r):
205 void Renderer::MtxStack::update()
207 renderer.mtx_changed = true;