1 #include <msp/gl/extensions/arb_vertex_array_object.h>
2 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
3 #include <msp/gl/extensions/arb_vertex_shader.h>
8 #include "resourcemanager.h"
9 #include "vertexsetup.h"
16 Mesh::Mesh(ResourceManager *rm):
22 Mesh::Mesh(const VertexFormat &f, ResourceManager *rm):
28 void Mesh::init(ResourceManager *rm)
33 disallow_rendering = false;
53 void Mesh::check_buffers(unsigned mask)
55 if(mask&VERTEX_BUFFER)
57 unsigned req_size = vertices.get_required_buffer_size();
58 if(!vbuf || (vbuf->get_size()>0 && vbuf->get_size()<req_size))
61 vbuf = new Buffer(ARRAY_BUFFER);
62 vertices.use_buffer(vbuf);
63 vtx_setup.set_vertex_array(vertices);
64 dirty |= VERTEX_BUFFER;
70 unsigned req_size = (batches.empty() ? 0 : batches.front().get_required_buffer_size());
71 if(!ibuf || (ibuf->get_size()>0 && ibuf->get_size()<req_size))
74 ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
76 batches.front().change_buffer(ibuf);
77 vtx_setup.set_index_buffer(*ibuf);
78 dirty |= INDEX_BUFFER;
83 unsigned Mesh::get_n_vertices() const
85 return vertices.size();
88 float *Mesh::modify_vertex(unsigned i)
90 return vertices.modify(i);
93 void Mesh::add_batch(const Batch &b)
99 batches.back().use_buffer(ibuf);
101 else if(batches.back().can_append(b.get_type()))
102 batches.back().append(b);
105 bool reallocate = (batches.size()==batches.capacity());
108 for(vector<Batch>::iterator i=batches.end(); i!=batches.begin(); )
109 (--i)->use_buffer(0);
112 Batch *prev = &batches.back();
113 batches.push_back(b);
117 for(vector<Batch>::iterator i=batches.begin(); i!=batches.end(); ++i)
119 i->use_buffer(ibuf, prev);
124 batches.back().use_buffer(ibuf, prev);
127 check_buffers(INDEX_BUFFER);
130 void Mesh::set_winding(const WindingTest *w)
135 void Mesh::draw(Renderer &renderer) const
137 draw(renderer, 0, 0);
140 void Mesh::draw_instanced(Renderer &renderer, const VertexSetup &vs, unsigned count) const
142 if(vs.get_vertex_array()!=&vertices)
143 throw invalid_argument("Mesh::draw_instanced");
145 draw(renderer, &vs, count);
148 void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const
152 manager->resource_used(*this);
153 if(disallow_rendering)
160 renderer.set_vertex_setup(vs ? vs : &vtx_setup);
161 renderer.set_winding_test(winding);
165 for(vector<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
170 for(vector<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
171 renderer.draw_instanced(*i, count);
175 void Mesh::resize_buffers() const
177 if(dirty&VERTEX_BUFFER)
178 vbuf->storage(vertices.get_required_buffer_size());
179 if(dirty&INDEX_BUFFER)
180 ibuf->storage(batches.front().get_required_buffer_size());
184 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
186 return new AsyncLoader(*this, io);
189 UInt64 Mesh::get_data_size() const
193 size += vbuf->get_size();
195 size += ibuf->get_size();
202 vertices.use_buffer(0);
211 Mesh::Loader::Loader(Mesh &m, bool g):
212 DataFile::ObjectLoader<Mesh>(m),
215 add("batch", &Loader::batch);
216 add("vertices", &Loader::vertices);
217 add("winding", &Loader::winding);
220 void Mesh::Loader::vertices(const vector<VertexComponent> &c)
223 throw invalid_argument("No vertex components");
226 for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
228 obj.vertices.reset(fmt);
229 load_sub(obj.vertices);
232 obj.check_buffers(VERTEX_BUFFER);
233 obj.vtx_setup.refresh();
237 void Mesh::Loader::batch(PrimitiveType p)
244 void Mesh::Loader::winding(FaceWinding w)
247 obj.winding = &WindingTest::clockwise();
248 else if(w==COUNTERCLOCKWISE)
249 obj.winding = &WindingTest::counterclockwise();
253 Mesh::AsyncLoader::AsyncLoader(Mesh &m, IO::Seekable &i):
260 mesh.disallow_rendering = true;
261 mesh.check_buffers(VERTEX_BUFFER|INDEX_BUFFER);
264 Mesh::AsyncLoader::~AsyncLoader()
266 mesh.disallow_rendering = false;
267 delete vertex_updater;
268 delete index_updater;
271 bool Mesh::AsyncLoader::needs_sync() const
276 bool Mesh::AsyncLoader::process()
280 // TODO use correct filename
281 DataFile::Parser parser(io, "async");
282 Loader loader(mesh, false);
287 mesh.resize_buffers();
288 mesh.vtx_setup.refresh();
289 vertex_updater = mesh.vertices.refresh_async();
290 if(!mesh.batches.empty())
291 index_updater = mesh.batches.front().refresh_async();
296 vertex_updater->upload_data();
298 index_updater->upload_data();
302 delete vertex_updater;
304 delete index_updater;
309 if(phase==1 && !mesh.vbuf && !mesh.ibuf)