X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmesh.cpp;h=d474a27f116cc0c3e7175f7a64488120d4e83805;hb=c233a90fd05f1f5424d7fca94bb6b999f3061271;hp=07eaa68962a8952da9c4fa3f3e482a85c55d90dc;hpb=8aea0f65f9ab7d603e47cf4479388af246cc8400;p=libs%2Fgl.git diff --git a/source/mesh.cpp b/source/mesh.cpp index 07eaa689..d474a27f 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -1,3 +1,5 @@ +#include +#include #include "buffer.h" #include "mesh.h" #include "renderer.h" @@ -11,7 +13,9 @@ Mesh::Mesh(): vertices(VERTEX3), vbuf(0), ibuf(0), + vao_id(0), defer_buffers(true), + dirty(0), winding(0) { } @@ -19,7 +23,9 @@ Mesh::Mesh(const VertexFormat &f): vertices(f), vbuf(0), ibuf(0), + vao_id(0), defer_buffers(true), + dirty(2), winding(0) { } @@ -27,6 +33,8 @@ Mesh::~Mesh() { delete vbuf; delete ibuf; + if(vao_id) + glDeleteVertexArrays(1, &vao_id); } void Mesh::clear() @@ -42,7 +50,7 @@ void Mesh::use_buffers(bool b) create_buffers(); else { - vertices.use_vertex_buffer(0); + vertices.use_buffer(0); delete vbuf; vbuf = 0; delete ibuf; @@ -56,10 +64,55 @@ void Mesh::create_buffers() if(!vbuf) vbuf = new Buffer(ARRAY_BUFFER); - vertices.use_vertex_buffer(vbuf); + vertices.use_buffer(vbuf); if(!ibuf) ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); + + if(ARB_vertex_array_object && !vao_id) + glGenVertexArrays(1, &vao_id); +} + +void Mesh::refresh() const +{ + vertices.refresh(); + if(dirty) + { + if(dirty&1) + { + unbind(); + for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + i->refresh(); + } + + if(dirty&2) + { + glBindVertexArray(vao_id); + BufferAlias vbuf_alias(*vbuf); + Bind bind_vbuf(vbuf_alias); + + const VertexFormat &fmt = vertices.get_format(); + unsigned stride = get_stride(fmt)*sizeof(float); + float *ptr = 0; + for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + { + unsigned t = get_component_type(*c); + if(t>=get_component_type(ATTRIB1)) + t -= get_component_type(ATTRIB1); + unsigned sz = get_component_size(*c); + if(*c==COLOR4_UBYTE) + glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr); + else + glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr); + glEnableVertexAttribArray(t); + ptr += sz; + } + glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id()); + glBindVertexArray(0); + } + + dirty = 0; + } } unsigned Mesh::get_n_vertices() const @@ -77,6 +130,7 @@ void Mesh::add_batch(const Batch &b) if(defer_buffers) create_buffers(); + dirty |= 1; if(!batches.empty() && batches.back().can_append(b.get_type())) batches.back().append(b); else @@ -95,16 +149,21 @@ void Mesh::set_winding(const WindingTest *w) void Mesh::draw() const { - vertices.apply(); + refresh(); + + if(!current()) + { + vertices.apply(); + if(ibuf) + ibuf->bind_to(ELEMENT_ARRAY_BUFFER); + } - if(ibuf) - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); Bind bind_winding(winding); for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) i->draw(); - if(ibuf) + if(!current() && ibuf) Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); } @@ -116,8 +175,30 @@ void Mesh::draw(Renderer &renderer) const for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) renderer.draw(*i); +} - renderer.set_winding_test(0); +void Mesh::bind() const +{ + /* If VAOs are not supported, vao_id is zero and set_current won't get + called. Thus unbind won't try to call a null function either. */ + if(!vao_id) + unbind(); + else + { + if(Buffer::current(ELEMENT_ARRAY_BUFFER)) + { + unbind(); + Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); + } + if(set_current(this)) + glBindVertexArray(vao_id); + } +} + +void Mesh::unbind() +{ + if(set_current(0)) + glBindVertexArray(0); } @@ -138,6 +219,7 @@ void Mesh::Loader::vertices(const vector &c) for(vector::const_iterator i=c.begin(); i!=c.end(); ++i) fmt = (fmt, *i); obj.vertices.reset(fmt); + obj.dirty |= 2; load_sub(obj.vertices); }