]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.cpp
Throw an exception if trying to draw a Mesh with a different Mesh bound
[libs/gl.git] / source / mesh.cpp
1 #include <msp/gl/extensions/arb_vertex_array_object.h>
2 #include <msp/gl/extensions/arb_vertex_shader.h>
3 #include "buffer.h"
4 #include "error.h"
5 #include "mesh.h"
6 #include "renderer.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 Mesh::Mesh():
14         vertices(VERTEX3),
15         vbuf(0),
16         ibuf(0),
17         vao_id(0),
18         defer_buffers(true),
19         dirty(0),
20         winding(0)
21 { }
22
23 Mesh::Mesh(const VertexFormat &f):
24         vertices(f),
25         vbuf(0),
26         ibuf(0),
27         vao_id(0),
28         defer_buffers(true),
29         dirty(2),
30         winding(0)
31 { }
32
33 Mesh::~Mesh()
34 {
35         delete vbuf;
36         delete ibuf;
37         if(vao_id)
38                 glDeleteVertexArrays(1, &vao_id);
39 }
40
41 void Mesh::clear()
42 {
43         vertices.clear();
44         batches.clear();
45 }
46
47 void Mesh::use_buffers(bool b)
48 {
49         defer_buffers = false;
50         if(b)
51                 create_buffers();
52         else
53         {
54                 vertices.use_buffer(0);
55                 delete vbuf;
56                 vbuf = 0;
57                 delete ibuf;
58                 ibuf = 0;
59         }
60 }
61
62 void Mesh::create_buffers()
63 {
64         defer_buffers = false;
65
66         if(!vbuf)
67                 vbuf = new Buffer(ARRAY_BUFFER);
68         vertices.use_buffer(vbuf);
69
70         if(!ibuf)
71                 ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
72
73         if(ARB_vertex_array_object && !vao_id)
74                 glGenVertexArrays(1, &vao_id);
75 }
76
77 void Mesh::refresh() const
78 {
79         vertices.refresh();
80         if(dirty)
81         {
82                 if(dirty&1)
83                 {
84                         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
85                                 i->refresh();
86                 }
87
88                 if(dirty&2)
89                 {
90                         Bind bind_vbuf(vbuf, ARRAY_BUFFER);
91
92                         bool bind_here = !current();
93                         if(bind_here)
94                                 glBindVertexArray(vao_id);
95
96                         const VertexFormat &fmt = vertices.get_format();
97                         unsigned stride = get_stride(fmt)*sizeof(float);
98                         float *ptr = 0;
99                         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
100                         {
101                                 unsigned t = get_component_type(*c);
102                                 if(t>=get_component_type(ATTRIB1))
103                                         t -= get_component_type(ATTRIB1);
104                                 unsigned sz = get_component_size(*c);
105                                 if(*c==COLOR4_UBYTE)
106                                         glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
107                                 else
108                                         glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
109                                 glEnableVertexAttribArray(t);
110                                 ptr += sz;
111                         }
112                         glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
113
114                         if(bind_here)
115                                 glBindVertexArray(0);
116                 }
117
118                 dirty = 0;
119         }
120 }
121
122 unsigned Mesh::get_n_vertices() const
123 {
124         return vertices.size();
125 }
126
127 float *Mesh::modify_vertex(unsigned i)
128 {
129         return vertices.modify(i);
130 }
131
132 void Mesh::add_batch(const Batch &b)
133 {
134         if(defer_buffers)
135                 create_buffers();
136
137         dirty |= 1;
138         if(!batches.empty() && batches.back().can_append(b.get_type()))
139                 batches.back().append(b);
140         else
141         {
142                 Batch *prev = (batches.empty() ? 0 : &batches.back());
143                 batches.push_back(b);
144                 if(ibuf)
145                         batches.back().use_buffer(ibuf, prev);
146         }
147 }
148
149 void Mesh::set_winding(const WindingTest *w)
150 {
151         winding = w;
152 }
153
154 void Mesh::draw() const
155 {
156         const Mesh *cur = current();
157         if(cur && cur!=this)
158                 throw invalid_operation("Mesh::draw");
159
160         refresh();
161
162         if(!current())
163                 vertices.apply();
164         Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
165         Bind bind_winding(winding);
166
167         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
168                 i->draw();
169 }
170
171 void Mesh::draw(Renderer &renderer) const
172 {
173         refresh();
174
175         renderer.set_mesh(this);
176         renderer.set_element_buffer(ibuf);
177         renderer.set_winding_test(winding);
178
179         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
180                 renderer.draw(*i);
181 }
182
183 void Mesh::bind() const
184 {
185         /* If VAOs are not supported, vao_id is zero and set_current won't get
186         called.  Thus unbind won't try to call a null function either. */
187         if(!vao_id)
188                 unbind();
189         else if(set_current(this))
190                 glBindVertexArray(vao_id);
191 }
192
193 void Mesh::unbind()
194 {
195         if(set_current(0))
196                 glBindVertexArray(0);
197 }
198
199
200 Mesh::Loader::Loader(Mesh &m):
201         DataFile::ObjectLoader<Mesh>(m)
202 {
203         add("batch",    &Loader::batch);
204         add("vertices", &Loader::vertices);
205         add("winding",  &Loader::winding);
206 }
207
208 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
209 {
210         if(c.empty())
211                 throw invalid_argument("No vertex components");
212
213         VertexFormat fmt;
214         for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
215                 fmt = (fmt, *i);
216         obj.vertices.reset(fmt);
217         obj.dirty |= 2;
218         load_sub(obj.vertices);
219 }
220
221 void Mesh::Loader::batch(PrimitiveType p)
222 {
223         Batch btc(p);
224         load_sub(btc);
225         obj.add_batch(btc);
226 }
227
228 void Mesh::Loader::winding(FaceWinding w)
229 {
230         if(w==CLOCKWISE)
231                 obj.winding = &WindingTest::clockwise();
232         else if(w==COUNTERCLOCKWISE)
233                 obj.winding = &WindingTest::counterclockwise();
234 }
235
236 } // namespace GL
237 } // namespace Msp