]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Deprecate Renderer::escape() and replace it with end()
[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         }
161
162         Mesh::unbind();
163         Texturing::unbind();
164         Texture::unbind_from(0);
165         Material::unbind();
166         Lighting::unbind();
167         Program::unbind();
168         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
169         WindingTest::unbind();
170
171         *state = State();
172 }
173
174 void Renderer::exclude(const Renderable &renderable)
175 {
176         excluded.insert(&renderable);
177 }
178
179 void Renderer::include(const Renderable &renderable)
180 {
181         excluded.erase(&renderable);
182 }
183
184 void Renderer::render(const Renderable &renderable, const Tag &tag)
185 {
186         if(!excluded.count(&renderable))
187                 renderable.render(*this, tag);
188 }
189
190 void Renderer::draw(const Batch &batch)
191 {
192         apply_state();
193
194         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
195         if(legacy_bindings)
196         {
197                 if(element_buffer)
198                         element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
199                 else
200                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
201         }
202
203         batch.draw();
204 }
205
206 void Renderer::apply_state()
207 {
208         /* We (mostly) let the objects themselves figure out if the binding has
209         changed */
210
211         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
212
213         if(state->texturing)
214                 state->texturing->bind();
215         else
216         {
217                 Texturing::unbind();
218                 if(state->texture)
219                         state->texture->bind_to(0);
220                 else
221                         Texture::unbind_from(0);
222         }
223
224         if(legacy_bindings)
225         {
226                 if(state->material)
227                         state->material->bind();
228                 else
229                         Material::unbind();
230
231                 if(lighting_changed)
232                 {
233                         if(state->lighting)
234                         {
235                                 MatrixStack::modelview() = state->lighting_matrix;
236                                 state->lighting->bind();
237                                 mtx_changed = true;
238                                 lighting_changed = false;
239                         }
240                         else
241                                 Lighting::unbind();
242                 }
243         }
244
245         if(state->shprog)
246         {
247                 state->shprog->bind();
248
249                 if(!legacy_bindings)
250                 {
251                         const Matrix &m = mtx_stack.top();
252                         standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
253                         LinAl::SquareMatrix<float, 3> nm;
254                         for(unsigned i=0; i<3; ++i)
255                                 for(unsigned j=0; j<3; ++j)
256                                         nm(i, j) = m(i, j);
257                         nm = transpose(invert(nm));
258                         standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
259                         if(state->material)
260                                 state->material->get_shader_data().apply();
261                         standard_shdata.apply();
262                 }
263
264                 if(shdata_changed)
265                 {
266                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
267                                 (*i)->apply();
268                         shdata_changed = false;
269                 }
270         }
271         else
272                 Program::unbind();
273
274         if(state->mesh)
275         {
276                 if(legacy_bindings)
277                 {
278                         Mesh::unbind();
279                         state->mesh->get_vertices().apply();
280                 }
281                 else
282                         state->mesh->bind();
283         }
284         else
285                 Mesh::unbind();
286
287         if(state->winding_test)
288         {
289                 if(state->reverse_winding)
290                         state->winding_test->get_reverse().bind();
291                 else
292                         state->winding_test->bind();
293         }
294         else
295                 WindingTest::unbind();
296
297         if(legacy_bindings)
298         {
299                 if(!matrices_loaded)
300                 {
301                         MatrixStack::modelview().push();
302                         if(camera)
303                         {
304                                 MatrixStack::projection().push();
305                                 camera->apply();
306                         }
307                         matrices_loaded = true;
308                 }
309
310                 if(mtx_changed)
311                 {
312                         MatrixStack::modelview() = mtx_stack.top();
313                         mtx_changed = false;
314                 }
315         }
316 }
317
318
319 Renderer::State::State():
320         texture(0),
321         texturing(0),
322         lowest_effect_texunit(TexUnit::get_n_units()),
323         material(0),
324         lighting(0),
325         shprog(0),
326         shdata_count(0),
327         mesh(0),
328         winding_test(0),
329         reverse_winding(false)
330 { }
331
332
333 Renderer::MtxStack::MtxStack(Renderer &r):
334         renderer(r)
335 { }
336
337 void Renderer::MtxStack::update()
338 {
339         renderer.mtx_changed = true;
340 }
341
342 } // namespace GL
343 } // namespace Msp