]> git.tdb.fi Git - libs/gl.git/blob - source/mesh.cpp
Some fixes to VAO management
[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(true),
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(true),
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::setup_vao() const
78 {
79         Bind bind_vbuf(vbuf, ARRAY_BUFFER);
80
81         const VertexFormat &fmt = vertices.get_format();
82         unsigned stride = get_stride(fmt)*sizeof(float);
83         float *ptr = 0;
84         for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
85         {
86                 unsigned t = get_component_type(*c);
87                 if(t>=get_component_type(ATTRIB1))
88                         t -= get_component_type(ATTRIB1);
89                 unsigned sz = get_component_size(*c);
90                 if(*c==COLOR4_UBYTE)
91                         glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
92                 else
93                         glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
94                 glEnableVertexAttribArray(t);
95                 ptr += sz;
96         }
97         glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
98
99         dirty = false;
100 }
101
102 unsigned Mesh::get_n_vertices() const
103 {
104         return vertices.size();
105 }
106
107 float *Mesh::modify_vertex(unsigned i)
108 {
109         return vertices.modify(i);
110 }
111
112 void Mesh::add_batch(const Batch &b)
113 {
114         if(defer_buffers)
115                 create_buffers();
116
117         if(!batches.empty() && batches.back().can_append(b.get_type()))
118                 batches.back().append(b);
119         else
120         {
121                 Batch *prev = (batches.empty() ? 0 : &batches.back());
122                 batches.push_back(b);
123                 if(ibuf)
124                         batches.back().use_buffer(ibuf, prev);
125         }
126 }
127
128 void Mesh::set_winding(const WindingTest *w)
129 {
130         winding = w;
131 }
132
133 void Mesh::draw() const
134 {
135         const Mesh *cur = current();
136         if(cur && cur!=this)
137                 throw invalid_operation("Mesh::draw");
138
139         if(!current())
140                 vertices.apply();
141         Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
142         Bind bind_winding(winding);
143
144         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
145                 i->draw();
146 }
147
148 void Mesh::draw(Renderer &renderer) const
149 {
150         renderer.set_mesh(this);
151         renderer.set_element_buffer(ibuf);
152         renderer.set_winding_test(winding);
153
154         for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
155                 renderer.draw(*i);
156 }
157
158 void Mesh::bind() const
159 {
160         /* If VAOs are not supported, vao_id is zero and set_current won't get
161         called.  Thus unbind won't try to call a null function either. */
162         if(!vao_id)
163         {
164                 unbind();
165                 vertices.apply();
166         }
167         else if(set_current(this))
168         {
169                 glBindVertexArray(vao_id);
170                 vertices.refresh();
171                 if(dirty)
172                         setup_vao();
173         }
174 }
175
176 void Mesh::unbind()
177 {
178         if(set_current(0))
179                 glBindVertexArray(0);
180 }
181
182
183 Mesh::Loader::Loader(Mesh &m):
184         DataFile::ObjectLoader<Mesh>(m)
185 {
186         add("batch",    &Loader::batch);
187         add("vertices", &Loader::vertices);
188         add("winding",  &Loader::winding);
189 }
190
191 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
192 {
193         if(c.empty())
194                 throw invalid_argument("No vertex components");
195
196         VertexFormat fmt;
197         for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
198                 fmt = (fmt, *i);
199         obj.vertices.reset(fmt);
200         obj.dirty = true;
201         load_sub(obj.vertices);
202 }
203
204 void Mesh::Loader::batch(PrimitiveType p)
205 {
206         Batch btc(p);
207         load_sub(btc);
208         obj.add_batch(btc);
209 }
210
211 void Mesh::Loader::winding(FaceWinding w)
212 {
213         if(w==CLOCKWISE)
214                 obj.winding = &WindingTest::clockwise();
215         else if(w==COUNTERCLOCKWISE)
216                 obj.winding = &WindingTest::counterclockwise();
217 }
218
219 } // namespace GL
220 } // namespace Msp