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