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