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