]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Use Renderer to bind Lighting
[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 "program.h"
8 #include "programdata.h"
9 #include "renderable.h"
10 #include "renderer.h"
11 #include "texture.h"
12 #include "texturing.h"
13 #include "vertexarray.h"
14 #include "windingtest.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 Renderer::Renderer(const Camera *c):
22         mtx_stack(*this),
23         mtx_changed(false),
24         camera(c),
25         state_stack(1),
26         vertex_array(0),
27         element_buffer(0)
28 {
29         state_stack.reserve(16);
30         shdata_stack.reserve(32);
31         state = &state_stack.back();
32
33         MatrixStack::modelview().push();
34         if(camera)
35         {
36                 MatrixStack::projection().push();
37                 camera->apply();
38                 mtx_stack.load(camera->get_matrix());
39         }
40         else
41                 mtx_stack.load(MatrixStack::modelview().top());
42 }
43
44 Renderer::~Renderer()
45 {
46         if(camera)
47                 MatrixStack::projection().pop();
48         MatrixStack::modelview().pop();
49
50         Texturing::unbind();
51         Texture::unbind_from(0);
52         Material::unbind();
53         Program::unbind();
54         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
55         WindingTest::unbind();
56 }
57
58 void Renderer::set_texture(const Texture *t)
59 {
60         state->texture = t;
61         state->texturing = 0;
62 }
63
64 void Renderer::set_texturing(const Texturing *t)
65 {
66         state->texturing = t;
67         state->texture = 0;
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         lighting_changed = true;
80 }
81
82 void Renderer::set_shader_program(const Program *p, const ProgramData *d)
83 {
84         state->shprog = p;
85         if(p && d)
86                 add_shader_data(*d);
87
88         /* Even if we have no new shdata, the existing ones need to be re-applied
89         to the new program */
90         shdata_changed = true;
91 }
92
93 void Renderer::add_shader_data(const ProgramData &d)
94 {
95         shdata_stack.push_back(&d);
96         state->shdata_count = shdata_stack.size();
97         shdata_changed = true;
98 }
99
100 void Renderer::set_vertex_array(const VertexArray *a)
101 {
102         vertex_array = a;
103 }
104
105 void Renderer::set_element_buffer(const Buffer *b)
106 {
107         element_buffer = b;
108 }
109
110 void Renderer::set_winding_test(const WindingTest *w)
111 {
112         state->winding_test = w;
113 }
114
115 void Renderer::push_state()
116 {
117         state_stack.push_back(state_stack.back());
118         state = &state_stack.back();
119         mtx_stack.push();
120 }
121
122 void Renderer::pop_state()
123 {
124         if(state_stack.size()==1)
125                 throw stack_underflow("Renderer::pop_state");
126
127         state_stack.pop_back();
128         state = &state_stack.back();
129         if(shdata_stack.size()>state->shdata_count)
130                 shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
131         mtx_stack.pop();
132         mtx_changed = true;
133         shdata_changed = true;
134 }
135
136 void Renderer::escape()
137 {
138         apply_state();
139         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
140 }
141
142 void Renderer::exclude(const Renderable &renderable)
143 {
144         excluded.insert(&renderable);
145 }
146
147 void Renderer::include(const Renderable &renderable)
148 {
149         excluded.erase(&renderable);
150 }
151
152 void Renderer::render(const Renderable &renderable, const Tag &tag)
153 {
154         if(!excluded.count(&renderable))
155                 renderable.render(*this, tag);
156 }
157
158 void Renderer::draw(const Batch &batch)
159 {
160         if(!vertex_array)
161                 throw invalid_operation("Renderer::draw");
162
163         apply_state();
164
165         vertex_array->apply();
166
167         if(element_buffer)
168                 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
169         else
170                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
171
172         batch.draw();
173 }
174
175 void Renderer::apply_state()
176 {
177         /* We (mostly) let the objects themselves figure out if the binding has
178         changed */
179
180         if(state->texturing)
181                 state->texturing->bind();
182         else
183         {
184                 Texturing::unbind();
185                 if(state->texture)
186                         state->texture->bind_to(0);
187                 else
188                         Texture::unbind_from(0);
189         }
190
191         if(state->material)
192                 state->material->bind();
193         else
194                 Material::unbind();
195
196         if(lighting_changed)
197         {
198                 if(state->lighting)
199                 {
200                         MatrixStack::modelview() = state->lighting_matrix;
201                         state->lighting->bind();
202                         mtx_changed = true;
203                         lighting_changed = false;
204                 }
205                 else
206                         Lighting::unbind();
207         }
208
209         if(state->shprog)
210         {
211                 state->shprog->bind();
212                 if(shdata_changed)
213                 {
214                         for(vector<const ProgramData *>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
215                                 (*i)->apply();
216                         shdata_changed = false;
217                 }
218         }
219         else
220                 Program::unbind();
221
222         if(state->winding_test)
223                 state->winding_test->bind();
224         else
225                 WindingTest::unbind();
226
227         if(mtx_changed)
228         {
229                 MatrixStack::modelview() = mtx_stack.top();
230                 mtx_changed = false;
231         }
232 }
233
234
235 Renderer::State::State():
236         texture(0),
237         texturing(0),
238         material(0),
239         lighting(0),
240         shprog(0),
241         shdata_count(0),
242         winding_test(0)
243 { }
244
245
246 Renderer::MtxStack::MtxStack(Renderer &r):
247         renderer(r)
248 { }
249
250 void Renderer::MtxStack::update()
251 {
252         renderer.mtx_changed = true;
253 }
254
255 } // namespace GL
256 } // namespace Msp