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