]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Check for null camera in View::render
[libs/gl.git] / source / renderer.cpp
1 #include "batch.h"
2 #include "buffer.h"
3 #include "camera.h"
4 #include "clipping.h"
5 #include "error.h"
6 #include "lighting.h"
7 #include "material.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 "vertexsetup.h"
17 #include "windingtest.h"
18
19 using namespace std;
20
21 namespace Msp {
22 namespace GL {
23
24 Renderer::Renderer():
25         default_camera(0)
26 {
27         init();
28 }
29
30 Renderer::Renderer(const Camera *c):
31         default_camera(c)
32 {
33         init();
34
35         if(c)
36                 set_camera(*c);
37 }
38
39 void Renderer::init()
40 {
41         state_stack.reserve(16);
42         state_stack.push_back(State());
43         shdata_stack.reserve(32);
44         state = &state_stack.back();
45
46         standard_shdata.uniform("projection_matrix", Matrix());
47         standard_shdata.uniform("eye_world_matrix", Matrix());
48 }
49
50 Renderer::~Renderer()
51 {
52         end();
53 }
54
55 void Renderer::set_camera(const Camera &c)
56 {
57         state->camera = &c;
58         standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
59         standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
60         changed |= STANDARD_SHDATA;
61         set_matrix(state->camera->get_view_matrix());
62 }
63
64 void Renderer::set_matrix(const Matrix &matrix)
65 {
66         state->modelview_matrix = matrix;
67         changed |= MATRIX;
68 }
69
70 void Renderer::transform(const Matrix &matrix)
71 {
72         state->modelview_matrix *= matrix;
73         changed |= MATRIX;
74 }
75
76 void Renderer::set_texture(const Texture *t)
77 {
78         state->texture = t;
79         state->texturing = 0;
80 }
81
82 void Renderer::set_texturing(const Texturing *t)
83 {
84         state->texturing = t;
85         state->texture = 0;
86 }
87
88 unsigned Renderer::allocate_effect_texunit()
89 {
90         return --state->lowest_effect_texunit;
91 }
92
93 void Renderer::set_material(const Material *m)
94 {
95         state->material = m;
96         changed |= MATERIAL_SHDATA;
97 }
98
99 void Renderer::set_lighting(const Lighting *l)
100 {
101         state->lighting = l;
102         state->lighting_matrix = state->modelview_matrix;
103         if(l)
104         {
105                 l->update_shader_data(standard_shdata, state->lighting_matrix);
106                 changed |= STANDARD_SHDATA;
107         }
108 }
109
110 void Renderer::set_clipping(const Clipping *c)
111 {
112         state->clipping = c;
113         state->clipping_matrix = state->modelview_matrix;
114         if(c)
115         {
116                 c->update_shader_data(standard_shdata, state->clipping_matrix);
117                 changed |= STANDARD_SHDATA;
118         }
119 }
120
121 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
122 {
123         state->shprog = p;
124         if(p && d)
125                 add_shader_data(*d);
126 }
127
128 void Renderer::add_shader_data(const ProgramData &d)
129 {
130         if(state->shdata_count<shdata_stack.size() && shdata_stack[state->shdata_count]==&d)
131                 ++state->shdata_count;
132         else
133         {
134                 flush_shader_data();
135                 shdata_stack.push_back(&d);
136                 state->shdata_count = shdata_stack.size();
137                 changed |= SHADER_DATA;
138         }
139 }
140
141 void Renderer::flush_shader_data()
142 {
143         if(shdata_stack.size()>state->shdata_count)
144                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
145 }
146
147 void Renderer::set_vertex_setup(const VertexSetup *vs)
148 {
149         state->vertex_setup = vs;
150 }
151
152 void Renderer::set_winding_test(const WindingTest *w)
153 {
154         state->winding_test = w;
155 }
156
157 void Renderer::set_reverse_winding(bool r)
158 {
159         state->reverse_winding = r;
160 }
161
162 void Renderer::set_object_lod_bias(unsigned b)
163 {
164         state->object_lod_bias = b;
165 }
166
167 void Renderer::push_state()
168 {
169         state_stack.push_back(state_stack.back());
170         state = &state_stack.back();
171 }
172
173 void Renderer::pop_state()
174 {
175         if(state_stack.size()==1)
176                 throw stack_underflow("Renderer::pop_state");
177
178         const Camera *old_camera = state->camera;
179         const Lighting *old_lighting = state->lighting;
180         const Clipping *old_clipping = state->clipping;
181         state_stack.pop_back();
182         state = &state_stack.back();
183         changed |= MATRIX;
184         bool camera_changed = (state->camera!=old_camera);
185         if(camera_changed)
186         {
187                 if(state->camera)
188                 {
189                         standard_shdata.uniform("projection_matrix", state->camera->get_projection_matrix());
190                         standard_shdata.uniform("eye_world_matrix", state->camera->get_view_matrix());
191                 }
192                 else
193                 {
194                         standard_shdata.uniform("projection_matrix", Matrix());
195                         standard_shdata.uniform("eye_world_matrix", Matrix());
196                 }
197                 changed |= STANDARD_SHDATA;
198         }
199         /* This actually should compare the relevant matrices rather than check for
200         camera, but in practice lighting and clipping is set right after the camera
201         and a boolean check is much faster than matrix comparison. */
202         if(state->lighting!=old_lighting || camera_changed)
203         {
204                 if(state->lighting)
205                 {
206                         state->lighting->update_shader_data(standard_shdata, state->lighting_matrix);
207                         changed |= STANDARD_SHDATA;
208                 }
209         }
210         if(state->clipping!=old_clipping || camera_changed)
211         {
212                 if(state->clipping)
213                 {
214                         state->clipping->update_shader_data(standard_shdata, state->clipping_matrix);
215                         changed |= STANDARD_SHDATA;
216                 }
217         }
218 }
219
220 void Renderer::end()
221 {
222         if(state_stack.size()>1)
223                 throw invalid_operation("Renderer::end");
224
225         *state = State();
226         if(default_camera)
227                 set_camera(*default_camera);
228         else
229                 standard_shdata.uniform("projection_matrix", Matrix());
230         shdata_stack.clear();
231         excluded.clear();
232
233         Texturing::unbind();
234         Texture::unbind_from(0);
235         Clipping::unbind();
236         Program::unbind();
237         VertexSetup::unbind();
238         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
239         WindingTest::unbind();
240 }
241
242 void Renderer::exclude(const Renderable &renderable)
243 {
244         excluded.insert(&renderable);
245 }
246
247 void Renderer::include(const Renderable &renderable)
248 {
249         excluded.erase(&renderable);
250 }
251
252 void Renderer::render(const Renderable &renderable, const Tag &tag)
253 {
254         if(!excluded.count(&renderable))
255                 renderable.render(*this, tag);
256 }
257
258 void Renderer::draw(const Batch &batch)
259 {
260         apply_state();
261
262         batch.draw();
263 }
264
265 void Renderer::draw_instanced(const Batch &batch, unsigned count)
266 {
267         apply_state();
268
269         batch.draw_instanced(count);
270 }
271
272 void Renderer::apply_state()
273 {
274         /* We (mostly) let the objects themselves figure out if the binding has
275         changed */
276
277         if(state->texturing)
278                 state->texturing->bind();
279         else
280         {
281                 Texturing::unbind();
282                 if(state->texture)
283                         state->texture->bind_to(0);
284                 else
285                         Texture::unbind_from(0);
286         }
287
288         if(state->clipping)
289                 state->clipping->bind();
290         else
291                 Clipping::unbind();
292
293         if(state->shprog)
294         {
295                 bool shprog_changed = (state->shprog!=Program::current());
296                 state->shprog->bind();
297
298                 if(changed&MATRIX)
299                 {
300                         standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix);
301                         LinAl::SquareMatrix<float, 3> nm = state->modelview_matrix.block<3, 3>(0, 0);
302                         nm = transpose(invert(nm));
303                         standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
304                         changed = (changed&~MATRIX)|STANDARD_SHDATA;
305                 }
306
307                 if(state->material && ((changed&MATERIAL_SHDATA) || shprog_changed))
308                 {
309                         state->material->get_shader_data().apply();
310                         changed &= ~MATERIAL_SHDATA;
311                 }
312
313                 if((changed&STANDARD_SHDATA) || shprog_changed)
314                 {
315                         standard_shdata.apply();
316                         changed &= ~STANDARD_SHDATA;
317                 }
318
319                 bool extra_shdata = (shdata_stack.size()>state->shdata_count);
320
321                 if((changed&SHADER_DATA) || shprog_changed || extra_shdata)
322                 {
323                         if(extra_shdata)
324                                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
325                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
326                                 (*i)->apply();
327                         changed &= ~SHADER_DATA;
328                 }
329         }
330         else
331                 Program::unbind();
332
333         if(state->vertex_setup)
334                 state->vertex_setup->bind();
335         else
336                 VertexSetup::unbind();
337
338         if(state->winding_test)
339         {
340                 if(state->reverse_winding)
341                         state->winding_test->get_reverse().bind();
342                 else
343                         state->winding_test->bind();
344         }
345         else
346                 WindingTest::unbind();
347 }
348
349
350 Renderer::State::State():
351         camera(0),
352         texture(0),
353         texturing(0),
354         lowest_effect_texunit(TexUnit::get_n_units()),
355         material(0),
356         lighting(0),
357         clipping(0),
358         shprog(0),
359         shdata_count(0),
360         vertex_setup(0),
361         winding_test(0),
362         reverse_winding(false),
363         object_lod_bias(0)
364 { }
365
366 } // namespace GL
367 } // namespace Msp