]> git.tdb.fi Git - libs/gl.git/blob - source/renderer.cpp
Update viewer for compatibility with library changes
[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(const Program *p, const ProgramData *d)
72 {
73         state->shprog = p;
74         if(d)
75                 state->shdata.assign(1, d);
76         else
77                 state->shdata.clear();
78 }
79
80 void Renderer::add_shader_data(const ProgramData *d)
81 {
82         if(!state->shprog)
83                 throw invalid_operation("Renderer::add_shader_data");
84
85         state->shdata.push_back(d);
86 }
87
88 void Renderer::set_vertex_array(const VertexArray *a)
89 {
90         vertex_array_changed = (a!=vertex_array);
91         vertex_array = a;
92 }
93
94 void Renderer::set_element_buffer(const Buffer *b)
95 {
96         element_buffer = b;
97 }
98
99 void Renderer::set_winding_test(const WindingTest *w)
100 {
101         state->winding_test = w;
102 }
103
104 void Renderer::push_state()
105 {
106         state_stack.push_back(state_stack.back());
107         state = &state_stack.back();
108         mtx_stack.push();
109 }
110
111 void Renderer::pop_state()
112 {
113         if(state_stack.size()==1)
114                 throw stack_underflow("Renderer::pop_state");
115
116         state_stack.pop_back();
117         state = &state_stack.back();
118         mtx_stack.pop();
119         mtx_changed = true;
120 }
121
122 void Renderer::escape()
123 {
124         apply_state();
125         Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
126 }
127
128 void Renderer::draw(const Batch &batch)
129 {
130         if(!vertex_array)
131                 throw invalid_operation("Renderer::draw");
132
133         apply_state();
134
135         // Until VertexArray acquires VAO support and becomes Bindable
136         if(vertex_array_changed)
137         {
138                 vertex_array->apply();
139                 vertex_array_changed = false;
140         }
141
142         if(element_buffer)
143                 element_buffer->bind_to(ELEMENT_ARRAY_BUFFER);
144         else
145                 Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
146
147         batch.draw();
148 }
149
150 void Renderer::apply_state()
151 {
152         // We let the objects themselves figure out if the binding has changed
153
154         if(state->texturing)
155                 state->texturing->bind();
156         else
157         {
158                 Texturing::unbind();
159                 if(state->texture)
160                         state->texture->bind_to(0);
161                 else
162                         Texture::unbind_from(0);
163         }
164
165         if(state->material)
166                 state->material->bind();
167         else
168                 Material::unbind();
169
170         if(state->shprog)
171         {
172                 state->shprog->bind();
173                 for(vector<const ProgramData *>::const_iterator i=state->shdata.begin(); i!=state->shdata.end(); ++i)
174                         (*i)->apply();
175         }
176         else
177                 Program::unbind();
178
179         if(state->winding_test)
180                 state->winding_test->bind();
181         else
182                 WindingTest::unbind();
183
184         if(mtx_changed)
185         {
186                 MatrixStack::modelview() = mtx_stack.top();
187                 mtx_changed = false;
188         }
189 }
190
191
192 Renderer::State::State():
193         texture(0),
194         texturing(0),
195         material(0),
196         shprog(0),
197         winding_test(0)
198 { }
199
200
201 Renderer::MtxStack::MtxStack(Renderer &r):
202         renderer(r)
203 { }
204
205 void Renderer::MtxStack::update()
206 {
207         renderer.mtx_changed = true;
208 }
209
210 } // namespace GL
211 } // namespace Msp