]> git.tdb.fi Git - libs/gl.git/commitdiff
Move vertex buffer management from VertexArray to Mesh
authorMikko Rasa <tdb@tdb.fi>
Thu, 28 Nov 2013 20:21:59 +0000 (22:21 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 28 Nov 2013 20:35:52 +0000 (22:35 +0200)
It was getting in the way of implementing vertex array objects.

source/mesh.cpp
source/mesh.h
source/vertexarray.cpp
source/vertexarray.h

index 572ac6195502afc97259a45857d94998c666fa4d..281bb7c2c9c67a01c2507e63565779e11701bfa4 100644 (file)
@@ -9,20 +9,23 @@ namespace GL {
 
 Mesh::Mesh():
        vertices(VERTEX3),
+       vbuf(0),
        ibuf(0),
-       defer_ibuf(true),
+       defer_buffers(true),
        winding(0)
 { }
 
 Mesh::Mesh(const VertexFormat &f):
        vertices(f),
+       vbuf(0),
        ibuf(0),
-       defer_ibuf(true),
+       defer_buffers(true),
        winding(0)
 { }
 
 Mesh::~Mesh()
 {
+       delete vbuf;
        delete ibuf;
 }
 
@@ -34,22 +37,31 @@ void Mesh::clear()
 
 void Mesh::use_buffers(bool b)
 {
+       defer_buffers = false;
        if(b)
-       {
-               vertices.use_vertex_buffer();
-               if(!ibuf)
-                       ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
-               defer_ibuf = false;
-       }
+               create_buffers();
        else
        {
                vertices.use_vertex_buffer(0);
+               delete vbuf;
+               vbuf = 0;
                delete ibuf;
                ibuf = 0;
-               defer_ibuf = false;
        }
 }
 
+void Mesh::create_buffers()
+{
+       defer_buffers = false;
+
+       if(!vbuf)
+               vbuf = new Buffer(ARRAY_BUFFER);
+       vertices.use_vertex_buffer(vbuf);
+
+       if(!ibuf)
+               ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
+}
+
 unsigned Mesh::get_n_vertices() const
 {
        return vertices.size();
@@ -62,11 +74,8 @@ float *Mesh::modify_vertex(unsigned i)
 
 void Mesh::add_batch(const Batch &b)
 {
-       if(defer_ibuf)
-       {
-               ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
-               defer_ibuf = false;
-       }
+       if(defer_buffers)
+               create_buffers();
 
        if(!batches.empty() && batches.back().can_append(b.get_type()))
                batches.back().append(b);
index 83f2fb0734d3bf31b16e2756977bb53a78d21214..6181d5fb9bc1b9126248d2e786f826a77c7c92bd 100644 (file)
@@ -35,8 +35,9 @@ public:
 private:
        VertexArray vertices;
        std::list<Batch> batches;
+       Buffer *vbuf;
        Buffer *ibuf;
-       bool defer_ibuf;
+       bool defer_buffers;
        const WindingTest *winding;
 
 public:
@@ -46,7 +47,10 @@ public:
 
        void clear();
        void use_buffers(bool);
+private:
+       void create_buffers();
 
+public:
        const VertexArray &get_vertices() const { return vertices; }
        unsigned get_n_vertices() const;
        float *modify_vertex(unsigned);
index 5065f305c3575920411b6d8ebfc707f61b6aabb3..3fca527d75aee2dba990a176a2415f514a537f92 100644 (file)
@@ -11,7 +11,6 @@ namespace Msp {
 namespace GL {
 
 VertexArray::VertexArray(const VertexFormat &f):
-       defer_vbuf(true),
        dirty(false)
 {
        reset(f);
@@ -76,21 +75,9 @@ unsigned VertexArray::get_array_slot(unsigned char comp)
        }
 }
 
-void VertexArray::use_vertex_buffer()
-{
-       if(vbuf)
-               return;
-
-       vbuf = new Buffer(ARRAY_BUFFER);
-       defer_vbuf = false;
-       dirty = true;
-}
-
 void VertexArray::use_vertex_buffer(Buffer *b)
 {
        vbuf = b;
-       vbuf.keep();
-       defer_vbuf = false;
        dirty = true;
 }
 
@@ -120,11 +107,6 @@ float *VertexArray::modify(unsigned i)
 void VertexArray::set_dirty()
 {
        dirty = true;
-       if(defer_vbuf)
-       {
-               vbuf = new Buffer(ARRAY_BUFFER);
-               defer_vbuf = false;
-       }
 }
 
 void VertexArray::apply() const
index 82a6b6ac5260cb3bd074404480187022f2b03b37..af1817b18461893591bc72c001db80c1dd72c0ce 100644 (file)
@@ -38,8 +38,7 @@ private:
        std::vector<float> data;
        unsigned stride;
        std::vector<Array> arrays;
-       RefPtr<Buffer> vbuf;
-       bool defer_vbuf;
+       Buffer *vbuf;
        mutable bool dirty;
 
        VertexArray(const VertexArray &);
@@ -54,7 +53,6 @@ private:
        static unsigned get_array_slot(unsigned char);
 
 public:
-       void use_vertex_buffer();
        void use_vertex_buffer(Buffer *);
 
        void clear();