]> git.tdb.fi Git - libs/gl.git/commitdiff
Add vertex array object support to Mesh
authorMikko Rasa <tdb@tdb.fi>
Sun, 15 Dec 2013 23:03:26 +0000 (01:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 16 Dec 2013 18:28:44 +0000 (20:28 +0200)
Renderer integration to be added soon.

extensions/arb_vertex_array_object.glext [new file with mode: 0644]
source/batch.cpp
source/buffer.cpp
source/mesh.cpp
source/mesh.h
source/vertexarray.cpp

diff --git a/extensions/arb_vertex_array_object.glext b/extensions/arb_vertex_array_object.glext
new file mode 100644 (file)
index 0000000..4db9f5d
--- /dev/null
@@ -0,0 +1 @@
+extension ARB_vertex_array_object
index 09a6a6111314262d57b7607939da42a74ecc3f89..bca33484ce976cc585d8da88187c8ba568baed23 100644 (file)
@@ -4,6 +4,7 @@
 #include "bindable.h"
 #include "buffer.h"
 #include "error.h"
+#include "mesh.h"
 #include "vertexarray.h"
 
 using namespace std;
@@ -247,13 +248,25 @@ void Batch::draw() const
 
        if(Buffer *ibuf = get_buffer())
        {
-               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
-               Bind bind_ibuf(alias, true);
+               bool have_vao = Mesh::current();
+               const Buffer *old_ibuf = 0;
+               if(!have_vao)
+               {
+                       old_ibuf = Buffer::current(ELEMENT_ARRAY_BUFFER);
+                       ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
+               }
 
                if(dirty)
                        update_buffer();
 
                glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
+               if(!have_vao)
+               {
+                       if(old_ibuf)
+                               old_ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
+                       else
+                               Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
+               }
        }
        else
                glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
index a502190f82058c5b67aa4b53dada321bbec95cbf..d8bb28028ee686380e48b77613c039906df40a35 100644 (file)
@@ -3,6 +3,8 @@
 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
 #include "buffer.h"
+#include "error.h"
+#include "mesh.h"
 #include "misc.h"
 
 using namespace std;
@@ -67,12 +69,17 @@ void Buffer::bind_to(BufferType t) const
 {
        if(t!=type)
                require_buffer_type(t);
+       // Don't change the binding in a mesh's vertex array object
+       if(t==ELEMENT_ARRAY_BUFFER && Mesh::current())
+               throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
        if(set_current(t, this))
                glBindBuffer(t, id);
 }
 
 void Buffer::unbind_from(BufferType type)
 {
+       if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
+               throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
        if(set_current(type, 0))
                glBindBuffer(type, 0);
 }
index f23c56c8b80c54e30b645984cc3d0b8d7747f7e5..d474a27f116cc0c3e7175f7a64488120d4e83805 100644 (file)
@@ -1,3 +1,5 @@
+#include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
 #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()
@@ -60,6 +68,51 @@ 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::refresh() const
+{
+       vertices.refresh();
+       if(dirty)
+       {
+               if(dirty&1)
+               {
+                       unbind();
+                       for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
+                               i->refresh();
+               }
+
+               if(dirty&2)
+               {
+                       glBindVertexArray(vao_id);
+                       BufferAlias<ARRAY_BUFFER> 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<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
                i->draw();
 
-       if(ibuf)
+       if(!current() && ibuf)
                Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
 }
 
@@ -118,6 +177,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();
+       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);
+}
+
 
 Mesh::Loader::Loader(Mesh &m):
        DataFile::ObjectLoader<Mesh>(m)
@@ -136,6 +219,7 @@ void Mesh::Loader::vertices(const vector<VertexComponent> &c)
        for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
                fmt = (fmt, *i);
        obj.vertices.reset(fmt);
+       obj.dirty |= 2;
        load_sub(obj.vertices);
 }
 
index 6181d5fb9bc1b9126248d2e786f826a77c7c92bd..2aba273e2b2539cc567a66a25f8525f519c446c9 100644 (file)
@@ -17,7 +17,7 @@ Raw mesh data, consisting of a VertexArray and one or more Batches.  Though a
 Mesh can draw itself, it's usually used as part of Renderables rather than on
 its own.
 */
-class Mesh
+class Mesh: public Bindable<Mesh>
 {
        friend class MeshBuilder;
 
@@ -37,7 +37,9 @@ private:
        std::list<Batch> batches;
        Buffer *vbuf;
        Buffer *ibuf;
+       unsigned vao_id;
        bool defer_buffers;
+       mutable unsigned char dirty;
        const WindingTest *winding;
 
 public:
@@ -49,6 +51,7 @@ public:
        void use_buffers(bool);
 private:
        void create_buffers();
+       void refresh() const;
 
 public:
        const VertexArray &get_vertices() const { return vertices; }
@@ -62,6 +65,10 @@ public:
 
        void draw() const;
        void draw(Renderer &) const;
+
+       void bind() const;
+
+       static void unbind();
 };
 
 } // namespace GL
index e453a3f12a569a70f7868288647dee37c6f45b33..996871c4094ecc659566cd460175392c6b69600a 100644 (file)
@@ -3,6 +3,7 @@
 #include "buffer.h"
 #include "error.h"
 #include "gl.h"
+#include "mesh.h"
 #include "vertexarray.h"
 
 using namespace std;
@@ -112,6 +113,9 @@ void VertexArray::apply() const
 {
        if(format.empty())
                throw invalid_operation("VertexArray::apply");
+       // Don't mess up the vertex array object of a mesh
+       if(Mesh::current())
+               throw invalid_operation("VertexArray::apply");
 
        const VertexArray *old = current();
        /* If the array has been modified, apply it even if it was the last one to