]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Add WindingTest and support for it in Mesh and Renderer
[libs/gl.git] / source / renderer.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/except.h>
9 #include "batch.h"
10 #include "buffer.h"
11 #include "camera.h"
12 #include "material.h"
13 #include "program.h"
14 #include "programdata.h"
15 #include "renderer.h"
16 #include "texture.h"
17 #include "texturing.h"
18 #include "vertexarray.h"
19 #include "windingtest.h"
20
21 using namespace std;
22
23 namespace Msp {
24 namespace GL {
25
26 Renderer::Renderer(const Camera *c):
27         mtx_stack(*this),
28         mtx_changed(false),
29         camera(c),
30         state_stack(1),
31         state(&state_stack.back()),
32         vertex_array(0),
33         vertex_array_changed(false),
34         element_buffer(0),
35         winding_test(0)
36 {
37         MatrixStack::modelview().push();
38         if(camera)
39         {
40                 MatrixStack::projection().push();
41                 camera->apply();
42                 mtx_stack.load(camera->get_matrix());
43         }
44         else
45                 mtx_stack.load(MatrixStack::modelview().top());
46 }
47
48 Renderer::~Renderer()
49 {
50         if(camera)
51                 MatrixStack::projection().pop();
52         MatrixStack::modelview().pop();
53
54         Texturing::unbind();
55         Texture::unbind_from(0);
56         Material::unbind();
57         Program::unbind();
58         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
59 }
60
61 void Renderer::set_texture(const Texture *t)
62 {
63         state->texture = t;
64         state->texturing = 0;
65 }
66
67 void Renderer::set_texturing(const Texturing *t)
68 {
69         state->texturing = t;
70         state->texture = 0;
71 }
72
73 void Renderer::set_material(const Material *m)
74 {
75         state->material = m;
76 }
77
78 void Renderer::set_shader(const Program *p, const ProgramData *d)
79 {
80         state->shprog = p;
81         if(d)
82                 state->shdata.assign(1, d);
83         else
84                 state->shdata.clear();
85 }
86
87 void Renderer::add_shader_data(const ProgramData *d)
88 {
89         if(!state->shprog)
90                 throw InvalidState("No shader program");
91
92         state->shdata.push_back(d);
93 }
94
95 void Renderer::set_vertex_array(const VertexArray *a)
96 {
97         vertex_array_changed = (a!=vertex_array);
98         vertex_array = a;
99 }
100
101 void Renderer::set_element_buffer(const Buffer *b)
102 {
103         element_buffer = b;
104 }
105
106 void Renderer::set_winding_test(const WindingTest *w)
107 {
108         winding_test = w;
109 }
110
111 void Renderer::push_state()
112 {
113         state_stack.push_back(state_stack.back());
114         state = &state_stack.back();
115         mtx_stack.push();
116 }
117
118 void Renderer::pop_state()
119 {
120         if(state_stack.size()==1)
121                 throw InvalidState("Can't pop the last state");
122
123         state_stack.pop_back();
124         state = &state_stack.back();
125         mtx_stack.pop();
126         mtx_changed = true;
127 }
128
129 void Renderer::escape()
130 {
131         apply_state();
132         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
133         WindingTest::unbind();
134 }
135
136 void Renderer::draw(const Batch &batch)
137 {
138         if(!vertex_array)
139                 throw InvalidState("Can't draw without a vertex array");
140
141         apply_state();
142
143         // Until VertexArray acquires VAO support and becomes Bindable
144         if(vertex_array_changed)
145                 vertex_array->apply();
146
147         if(element_buffer)
148                 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
149         else
150                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
151
152         if(winding_test)
153                 winding_test->bind();
154         else
155                 WindingTest::unbind();
156
157         batch.draw();
158 }
159
160 void Renderer::apply_state()
161 {
162         // We let the objects themselves figure out if the binding has changed
163
164         if(state->texturing)
165                 state->texturing->bind();
166         else
167         {
168                 Texturing::unbind();
169                 if(state->texture)
170                         state->texture->bind_to(0);
171                 else
172                         Texture::unbind_from(0);
173         }
174
175         if(state->material)
176                 state->material->bind();
177         else
178                 Material::unbind();
179
180         if(state->shprog)
181         {
182                 state->shprog->bind();
183                 for(vector<const ProgramData *>::const_iterator i=state->shdata.begin(); i!=state->shdata.end(); ++i)
184                         (*i)->apply();
185         }
186         else
187                 Program::unbind();
188
189         if(mtx_changed)
190         {
191                 MatrixStack::modelview() = mtx_stack.top();
192                 mtx_changed = false;
193         }
194 }
195
196
197 Renderer::State::State():
198         texture(0),
199         texturing(0),
200         material(0),
201         shprog(0)
202 { }
203
204
205 Renderer::MtxStack::MtxStack(Renderer &r):
206         renderer(r)
207 { }
208
209 void Renderer::MtxStack::update()
210 {
211         renderer.mtx_changed = true;
212 }
213
214 } // namespace GL
215 } // namespace Msp