]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Automatically determine texture units for effects
[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                 /* XXX I'm not happy with this, but can't come up with anything better
94                 right now. */
95                 const_cast<Lighting *>(l)->update_shader_data(mtx_stack.top());
96         lighting_changed = true;
97 }
98
99 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
100 {
101         state->shprog = p;
102         if(p && d)
103                 add_shader_data(*d);
104
105         /* Even if we have no new shdata, the existing ones need to be re-applied
106         to the new program */
107         shdata_changed = true;
108 }
109
110 void Renderer::add_shader_data(const ProgramData &d)
111 {
112         shdata_stack.push_back(&d);
113         state->shdata_count = shdata_stack.size();
114         shdata_changed = true;
115 }
116
117 void Renderer::set_mesh(const Mesh *m)
118 {
119         state->mesh = m;
120 }
121
122 void Renderer::set_element_buffer(const Buffer *b)
123 {
124         element_buffer = b;
125 }
126
127 void Renderer::set_winding_test(const WindingTest *w)
128 {
129         state->winding_test = w;
130 }
131
132 void Renderer::set_reverse_winding(bool r)
133 {
134         state->reverse_winding = r;
135 }
136
137 void Renderer::push_state()
138 {
139         state_stack.push_back(state_stack.back());
140         state = &state_stack.back();
141         mtx_stack.push();
142 }
143
144 void Renderer::pop_state()
145 {
146         if(state_stack.size()==1)
147                 throw stack_underflow("Renderer::pop_state");
148
149         state_stack.pop_back();
150         state = &state_stack.back();
151         if(shdata_stack.size()>state->shdata_count)
152                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
153         mtx_stack.pop();
154         mtx_changed = true;
155         shdata_changed = true;
156 }
157
158 void Renderer::escape()
159 {
160         apply_state();
161         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
162 }
163
164 void Renderer::exclude(const Renderable &renderable)
165 {
166         excluded.insert(&renderable);
167 }
168
169 void Renderer::include(const Renderable &renderable)
170 {
171         excluded.erase(&renderable);
172 }
173
174 void Renderer::render(const Renderable &renderable, const Tag &tag)
175 {
176         if(!excluded.count(&renderable))
177                 renderable.render(*this, tag);
178 }
179
180 void Renderer::draw(const Batch &batch)
181 {
182         apply_state();
183
184         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
185         if(legacy_bindings)
186         {
187                 if(element_buffer)
188                         element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
189                 else
190                         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
191         }
192
193         batch.draw();
194 }
195
196 void Renderer::apply_state()
197 {
198         /* We (mostly) let the objects themselves figure out if the binding has
199         changed */
200
201         bool legacy_bindings = (!state->shprog || state->shprog->uses_legacy_variables());
202
203         if(state->texturing)
204                 state->texturing->bind();
205         else
206         {
207                 Texturing::unbind();
208                 if(state->texture)
209                         state->texture->bind_to(0);
210                 else
211                         Texture::unbind_from(0);
212         }
213
214         if(legacy_bindings)
215         {
216                 if(state->material)
217                         state->material->bind();
218                 else
219                         Material::unbind();
220
221                 if(lighting_changed)
222                 {
223                         if(state->lighting)
224                         {
225                                 MatrixStack::modelview() = state->lighting_matrix;
226                                 state->lighting->bind();
227                                 mtx_changed = true;
228                                 lighting_changed = false;
229                         }
230                         else
231                                 Lighting::unbind();
232                 }
233         }
234
235         if(state->shprog)
236         {
237                 state->shprog->bind();
238
239                 if(!legacy_bindings)
240                 {
241                         const Matrix &m = mtx_stack.top();
242                         standard_shdata.uniform("eye_obj_matrix", mtx_stack.top());
243                         LinAl::SquareMatrix<float, 3> nm;
244                         for(unsigned i=0; i<3; ++i)
245                                 for(unsigned j=0; j<3; ++j)
246                                         nm(i, j) = m(i, j);
247                         nm = transpose(invert(nm));
248                         standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0));
249                         if(state->lighting)
250                                 state->lighting->get_shader_data().apply();
251                         if(state->material)
252                                 state->material->get_shader_data().apply();
253                         standard_shdata.apply();
254                 }
255
256                 if(shdata_changed)
257                 {
258                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
259                                 (*i)->apply();
260                         shdata_changed = false;
261                 }
262         }
263         else
264                 Program::unbind();
265
266         if(state->mesh)
267         {
268                 if(legacy_bindings)
269                 {
270                         Mesh::unbind();
271                         state->mesh->get_vertices().apply();
272                 }
273                 else
274                         state->mesh->bind();
275         }
276         else
277                 Mesh::unbind();
278
279         if(state->winding_test)
280         {
281                 if(state->reverse_winding)
282                         state->winding_test->get_reverse().bind();
283                 else
284                         state->winding_test->bind();
285         }
286         else
287                 WindingTest::unbind();
288
289         if(legacy_bindings && mtx_changed)
290         {
291                 MatrixStack::modelview() = mtx_stack.top();
292                 mtx_changed = false;
293         }
294 }
295
296
297 Renderer::State::State():
298         texture(0),
299         texturing(0),
300         lowest_effect_texunit(TexUnit::get_n_units()),
301         material(0),
302         lighting(0),
303         shprog(0),
304         shdata_count(0),
305         mesh(0),
306         winding_test(0),
307         reverse_winding(false)
308 { }
309
310
311 Renderer::MtxStack::MtxStack(Renderer &r):
312         renderer(r)
313 { }
314
315 void Renderer::MtxStack::update()
316 {
317         renderer.mtx_changed = true;
318 }
319
320 } // namespace GL
321 } // namespace Msp