X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fmesh.cpp;h=d421769abc257a775d53a4b0c73909cd92c36abb;hb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;hp=cba1bf57fdbcd8737183c90d06fb1d9c7a511d51;hpb=e92458a4a0e6191bff549a8b316dbbbd7c56e90f;p=libs%2Fgl.git diff --git a/source/mesh.cpp b/source/mesh.cpp index cba1bf57..d421769a 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include "buffer.h" #include "mesh.h" using namespace std; @@ -13,37 +14,92 @@ namespace Msp { namespace GL { Mesh::Mesh(): - vertices(NODATA) -{ - vertices.use_vertex_buffer(); -} + vertices(VERTEX3), + ibuf(0) +{ } + +Mesh::Mesh(const VertexFormat &f): + vertices(f), + ibuf(0) +{ } -Mesh::Mesh(VertexFormat f): - vertices(f) +void Mesh::use_vertex_buffer(bool b) { - vertices.use_vertex_buffer(); + if(b) + { + vertices.use_vertex_buffer(); + if(!ibuf) + ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); + update_index_buffer(); + } + else + { + vertices.use_vertex_buffer(0); + delete ibuf; + ibuf = 0; + } } -RefPtr Mesh::modify() +float *Mesh::get_vertex(unsigned i) { - return new MeshBuilder(*this); + return vertices[i]; } void Mesh::add_batch(const Batch &b) { batches.push_back(b); + update_index_buffer(); +} + +void Mesh::clear() +{ + vertices.clear(); + batches.clear(); } void Mesh::draw() const { vertices.apply(); + if(ibuf) + { + ibuf->bind(); + unsigned offset = 0; + for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + { + i->draw_with_buffer(offset); + offset += i->size(); + } + ibuf->unbind(); + } + else + { + for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + i->draw(); + } +} + +void Mesh::update_index_buffer() +{ + if(!ibuf) + return; + + unsigned total = 0; + for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + total += i->size(); + + ibuf->data(total*sizeof(unsigned), 0); + unsigned offset = 0; for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) - i->draw(); + { + ibuf->sub_data(offset*sizeof(unsigned), i->size()*sizeof(unsigned), &i->get_indices()[0]); + offset += i->size(); + } + ibuf->unbind(); } Mesh::Loader::Loader(Mesh &m): - mesh(m) + DataFile::ObjectLoader(m) { add("vertices", &Loader::vertices); add("batch", &Loader::batch); @@ -51,14 +107,14 @@ Mesh::Loader::Loader(Mesh &m): void Mesh::Loader::vertices(VertexFormat f) { - mesh.vertices.reset(f); - load_sub(mesh.vertices); + obj.vertices.reset(f); + load_sub(obj.vertices); } void Mesh::Loader::batch(PrimitiveType p) { - mesh.batches.push_back(Batch(p)); - load_sub(mesh.batches.back()); + obj.batches.push_back(Batch(p)); + load_sub(obj.batches.back()); } } // namespace GL