X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmesh.cpp;h=d43e0d0efdd421b43b209c0e8f4f280d2529c895;hb=76e338af116120d93d082ad247591ec9adad9233;hp=4e671e037e6352e3617c1c474b7e8ecb02eac454;hpb=3f285d3f4fd0a6790bf1efa780284dc7ba2287a2;p=libs%2Fgl.git diff --git a/source/mesh.cpp b/source/mesh.cpp index 4e671e03..d43e0d0e 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,16 +14,87 @@ namespace Msp { namespace GL { Mesh::Mesh(): - vertices(NODATA) + vertices(VERTEX3), + ibuf(0) +{ } + +Mesh::Mesh(const VertexFormat &f): + vertices(f), + ibuf(0) +{ } + +void Mesh::use_vertex_buffer(bool b) +{ + 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; + } +} + +float *Mesh::get_vertex(unsigned i) { - vertices.use_vertex_buffer(); + 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(); }