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