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