]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Clear matrices_loaded flag after resetting matrices
[libs/gl.git] / source / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "error.h"
5 #include "lighting.h"
6 #include "material.h"
7 #include "mesh.h"
8 #include "program.h"
9 #include "programdata.h"
10 #include "renderable.h"
11 #include "renderer.h"
12 #include "texture.h"
13 #include "texturing.h"
14 #include "texunit.h"
15 #include "vertexarray.h"
16 #include "windingtest.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace GL {
22
23 Renderer::Renderer(const Camera *c):
24         mtx_stack(*this),
25         mtx_changed(true),
26         matrices_loaded(false),
27         camera(c),
28         state_stack(1),
29         lighting_changed(false),
30         element_buffer(0)
31 {
32         state_stack.reserve(16);
33         shdata_stack.reserve(32);
34         state = &state_stack.back();
35
36         if(camera)
37         {
38                 mtx_stack.load(camera->get_matrix());
39                 standard_shdata.uniform("projection_matrix", camera->get_projection_matrix());
40         }
41         else
42         {
43                 standard_shdata.uniform("projection_matrix", MatrixStack::projection().top());
44                 mtx_stack.load(MatrixStack::modelview().top());
45         }
46 }
47
48 Renderer::~Renderer()
49 {
50         end();
51 }
52
53 void Renderer::set_texture(const Texture *t)
54 {
55         state->texture = t;
56         state->texturing = 0;
57 }
58
59 void Renderer::set_texturing(const Texturing *t)
60 {
61         state->texturing = t;
62         state->texture = 0;
63 }
64
65 unsigned Renderer::allocate_effect_texunit()
66 {
67         return --state->lowest_effect_texunit;
68 }
69
70 void Renderer::set_material(const Material *m)
71 {
72         state->material = m;
73 }
74
75 void Renderer::set_lighting(const Lighting *l)
76 {
77         state->lighting = l;
78         state->lighting_matrix = mtx_stack.top();
79         if(l)
80                 l->update_shader_data(standard_shdata, mtx_stack.top());
81         lighting_changed = true;
82 }
83
84 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
85 {
86         state->shprog = p;
87         if(p && d)
88                 add_shader_data(*d);
89
90         /* Even if we have no new shdata, the existing ones need to be re-applied
91         to the new program */
92         shdata_changed = true;
93 }
94
95 void Renderer::add_shader_data(const ProgramData &d)
96 {
97         shdata_stack.push_back(&d);
98         state->shdata_count = shdata_stack.size();
99         shdata_changed = true;
100 }
101
102 void Renderer::set_mesh(const Mesh *m)
103 {
104         state->mesh = m;
105 }
106
107 void Renderer::set_element_buffer(const Buffer *b)
108 {
109         element_buffer = b;
110 }
111
112 void Renderer::set_winding_test(const WindingTest *w)
113 {
114         state->winding_test = w;
115 }
116
117 void Renderer::set_reverse_winding(bool r)
118 {
119         state->reverse_winding = r;
120 }
121
122 void Renderer::push_state()
123 {
124         state_stack.push_back(state_stack.back());
125         state = &state_stack.back();
126         mtx_stack.push();
127 }
128
129 void Renderer::pop_state()
130 {
131         if(state_stack.size()==1)
132                 throw stack_underflow("Renderer::pop_state");
133
134         state_stack.pop_back();
135         state = &state_stack.back();
136         if(shdata_stack.size()>state->shdata_count)
137                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
138         mtx_stack.pop();
139         mtx_changed = true;
140         shdata_changed = true;
141 }
142
143 void Renderer::escape()
144 {
145         apply_state();
146         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
147         matrices_loaded = false;
148 }
149
150 void Renderer::end()
151 {
152         if(state_stack.size()>1)
153                 throw invalid_operation("Renderer::end");
154
155         if(matrices_loaded)
156         {
157                 if(camera)
158                         MatrixStack::projection().pop();
159                 MatrixStack::modelview().pop();
160                 matrices_loaded = false;
161         }
162
163         Mesh::unbind();
164         Texturing::unbind();
165         Texture::unbind_from(0);
166         Material::unbind();
167         Lighting::unbind();
168         Program::unbind();
169         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
170         WindingTest::unbind();
171
172         *state = State();
173 }
174
175 void Renderer::exclude(const Renderable &renderable)
176 {
177         excluded.insert(&renderable);
178 }
179
180 void Renderer::include(const Renderable &renderable)
181 {
182         excluded.erase(&renderable);
183 }
184
185 void Renderer::render(const Renderable &renderable, const Tag &tag)
186 {
187         if(!excluded.count(&renderable))
188                 renderable.render(*this, tag);
189 }
190
191 void Renderer::draw(const Batch &batch)
192 {
193         apply_state();
194
195         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
196         if(legacy_bindings)
197         {
198                 if(element_buffer)
199                         element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
200                 else
201                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
202         }
203
204         batch.draw();
205 }
206
207 void Renderer::apply_state()
208 {
209         /* We (mostly) let the objects themselves figure out if the binding has
210         changed */
211
212         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
213
214         if(state->texturing)
215                 state->texturing->bind();
216         else
217         {
218                 Texturing::unbind();
219                 if(state->texture)
220                         state->texture->bind_to(0);
221                 else
222                         Texture::unbind_from(0);
223         }
224
225         if(legacy_bindings)
226         {
227                 if(state->material)
228                         state->material->bind();
229                 else
230                         Material::unbind();
231
232                 if(lighting_changed)
233                 {
234                         if(state->lighting)
235                         {
236                                 MatrixStack::modelview() = state->lighting_matrix;
237                                 state->lighting->bind();
238                                 mtx_changed = true;
239                                 lighting_changed = false;
240                         }
241                         else
242                                 Lighting::unbind();
243                 }
244         }
245
246         if(state->shprog)
247         {
248                 state->shprog->bind();
249
250                 if(!legacy_bindings)
251                 {
252                         const Matrix &m = mtx_stack.top();
253                         standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
254                         LinAl::SquareMatrix<float, 3> nm;
255                         for(unsigned i=0; i<3; ++i)
256                                 for(unsigned j=0; j<3; ++j)
257                                         nm(i, j) = m(i, j);
258                         nm = transpose(invert(nm));
259                         standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
260                         if(state->material)
261                                 state->material->get_shader_data().apply();
262                         standard_shdata.apply();
263                 }
264
265                 if(shdata_changed)
266                 {
267                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
268                                 (*i)->apply();
269                         shdata_changed = false;
270                 }
271         }
272         else
273                 Program::unbind();
274
275         if(state->mesh)
276         {
277                 if(legacy_bindings)
278                 {
279                         Mesh::unbind();
280                         state->mesh->get_vertices().apply();
281                 }
282                 else
283                         state->mesh->bind();
284         }
285         else
286                 Mesh::unbind();
287
288         if(state->winding_test)
289         {
290                 if(state->reverse_winding)
291                         state->winding_test->get_reverse().bind();
292                 else
293                         state->winding_test->bind();
294         }
295         else
296                 WindingTest::unbind();
297
298         if(legacy_bindings)
299         {
300                 if(!matrices_loaded)
301                 {
302                         MatrixStack::modelview().push();
303                         if(camera)
304                         {
305                                 MatrixStack::projection().push();
306                                 camera->apply();
307                         }
308                         matrices_loaded = true;
309                 }
310
311                 if(mtx_changed)
312                 {
313                         MatrixStack::modelview() = mtx_stack.top();
314                         mtx_changed = false;
315                 }
316         }
317 }
318
319
320 Renderer::State::State():
321         texture(0),
322         texturing(0),
323         lowest_effect_texunit(TexUnit::get_n_units()),
324         material(0),
325         lighting(0),
326         shprog(0),
327         shdata_count(0),
328         mesh(0),
329         winding_test(0),
330         reverse_winding(false)
331 { }
332
333
334 Renderer::MtxStack::MtxStack(Renderer &r):
335         renderer(r)
336 { }
337
338 void Renderer::MtxStack::update()
339 {
340         renderer.mtx_changed = true;
341 }
342
343 } // namespace GL
344 } // namespace Msp