]> git.tdb.fi Git - libs/gl.git/commitdiff
Present Mesh's index buffer as current while the Mesh is bound
authorMikko Rasa <tdb@tdb.fi>
Sat, 21 Dec 2013 11:30:02 +0000 (13:30 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 21 Dec 2013 11:30:02 +0000 (13:30 +0200)
This simplifies the internal update operations.

source/batch.cpp
source/buffer.cpp
source/buffer.h
source/mesh.cpp
source/mesh.h

index bca33484ce976cc585d8da88187c8ba568baed23..7f9adff7aad42ca8a9cb8e3bd4758ff2048acb75 100644 (file)
@@ -248,25 +248,12 @@ void Batch::draw() const
 
        if(Buffer *ibuf = get_buffer())
        {
-               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);
-               }
+               BindRestore _bind_ibuf(ibuf, 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 61670cf9d378245f9a96ddead7f0fcea32ffb584..9fa03c1242024184585618ff841b71d802fc1a46 100644 (file)
@@ -65,13 +65,26 @@ 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(t==ELEMENT_ARRAY_BUFFER)
+               if(const Mesh *m = Mesh::current())
+               {
+                       // Don't change the binding in a mesh's vertex array object
+                       if(this==m->get_index_buffer())
+                               return;
+                       throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
+               }
        if(set_current(t, this))
                glBindBuffer(t, id);
 }
 
+const Buffer *Buffer::current(BufferType t)
+{
+       if(t==ELEMENT_ARRAY_BUFFER)
+               if(const Mesh *m = Mesh::current())
+                       return m->get_index_buffer();
+       return binding(t);
+}
+
 void Buffer::unbind_from(BufferType type)
 {
        if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
index bfbfd14891ff024a7d509198710deffa439e73f7..a7d4c2ba4314f8cbbc9db9fb6c2422cf0e85da2c 100644 (file)
@@ -89,7 +89,7 @@ public:
        /** Unbinds the buffer from its default slot. */
        void unbind() const { unbind_from(type); }
 
-       static const Buffer *current(BufferType t) { return binding(t); }
+       static const Buffer *current(BufferType);
        static void unbind_from(BufferType);
 private:
        static const Buffer *&binding(BufferType);
index c9819ae91c0e60dcec38f18febe5ffeb1747f94f..413986f463d60180d50f8b0d37982256e7c43ac5 100644 (file)
@@ -80,16 +80,18 @@ void Mesh::refresh() const
        {
                if(dirty&1)
                {
-                       unbind();
                        for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
                                i->refresh();
                }
 
                if(dirty&2)
                {
-                       glBindVertexArray(vao_id);
                        Bind bind_vbuf(vbuf, ARRAY_BUFFER);
 
+                       bool bind_here = !current();
+                       if(bind_here)
+                               glBindVertexArray(vao_id);
+
                        const VertexFormat &fmt = vertices.get_format();
                        unsigned stride = get_stride(fmt)*sizeof(float);
                        float *ptr = 0;
@@ -107,7 +109,9 @@ void Mesh::refresh() const
                                ptr += sz;
                        }
                        glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
-                       glBindVertexArray(0);
+
+                       if(bind_here)
+                               glBindVertexArray(0);
                }
 
                dirty = 0;
@@ -184,16 +188,8 @@ void Mesh::bind() const
        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);
-       }
+       else if(set_current(this))
+               glBindVertexArray(vao_id);
 }
 
 void Mesh::unbind()
index 2aba273e2b2539cc567a66a25f8525f519c446c9..ba21f256a6370abecb45d17feef9d40aa5818051 100644 (file)
@@ -55,6 +55,7 @@ private:
 
 public:
        const VertexArray &get_vertices() const { return vertices; }
+       const Buffer *get_index_buffer() const { return ibuf; }
        unsigned get_n_vertices() const;
        float *modify_vertex(unsigned);