X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmesh.cpp;h=150822ce85e1771a3826332cb19c12922c15a044;hb=910b5be9d486e36fbee15fbd89030202949f73b0;hp=f23c56c8b80c54e30b645984cc3d0b8d7747f7e5;hpb=cfd7b5bc0c69d0b8cc7c8b3ec81a1a9462d04abb;p=libs%2Fgl.git diff --git a/source/mesh.cpp b/source/mesh.cpp index f23c56c8..150822ce 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -1,4 +1,7 @@ +#include +#include #include "buffer.h" +#include "error.h" #include "mesh.h" #include "renderer.h" @@ -11,7 +14,9 @@ Mesh::Mesh(): vertices(VERTEX3), vbuf(0), ibuf(0), + vao_id(0), defer_buffers(true), + dirty(true), winding(0) { } @@ -19,7 +24,9 @@ Mesh::Mesh(const VertexFormat &f): vertices(f), vbuf(0), ibuf(0), + vao_id(0), defer_buffers(true), + dirty(true), winding(0) { } @@ -27,6 +34,8 @@ Mesh::~Mesh() { delete vbuf; delete ibuf; + if(vao_id) + glDeleteVertexArrays(1, &vao_id); } void Mesh::clear() @@ -60,6 +69,34 @@ void Mesh::create_buffers() if(!ibuf) ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); + + if(ARB_vertex_array_object && !vao_id) + glGenVertexArrays(1, &vao_id); +} + +void Mesh::setup_vao() const +{ + Bind bind_vbuf(vbuf, ARRAY_BUFFER); + + 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()); + + dirty = false; } unsigned Mesh::get_n_vertices() const @@ -95,22 +132,22 @@ void Mesh::set_winding(const WindingTest *w) void Mesh::draw() const { - vertices.apply(); + const Mesh *cur = current(); + if(cur && cur!=this) + throw invalid_operation("Mesh::draw"); - if(ibuf) - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); + if(!current()) + vertices.apply(); + Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER); Bind bind_winding(winding); for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) i->draw(); - - if(ibuf) - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); } void Mesh::draw(Renderer &renderer) const { - vertices.apply(); + renderer.set_mesh(this); renderer.set_element_buffer(ibuf); renderer.set_winding_test(winding); @@ -118,6 +155,30 @@ void Mesh::draw(Renderer &renderer) const renderer.draw(*i); } +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(); + vertices.apply(); + } + else if(set_current(this)) + { + glBindVertexArray(vao_id); + vertices.refresh(); + if(dirty) + setup_vao(); + } +} + +void Mesh::unbind() +{ + if(set_current(0)) + glBindVertexArray(0); +} + Mesh::Loader::Loader(Mesh &m): DataFile::ObjectLoader(m) @@ -136,6 +197,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 = true; load_sub(obj.vertices); }