]> git.tdb.fi Git - libs/gl.git/blobdiff - source/mesh.cpp
Present Mesh's index buffer as current while the Mesh is bound
[libs/gl.git] / source / mesh.cpp
index d474a27f116cc0c3e7175f7a64488120d4e83805..413986f463d60180d50f8b0d37982256e7c43ac5 100644 (file)
@@ -80,16 +80,17 @@ 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);
-                       BufferAlias<ARRAY_BUFFER> vbuf_alias(*vbuf);
-                       Bind bind_vbuf(vbuf_alias);
+                       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);
@@ -108,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;
@@ -169,7 +172,9 @@ void Mesh::draw() const
 
 void Mesh::draw(Renderer &renderer) const
 {
-       vertices.apply();
+       refresh();
+
+       renderer.set_mesh(this);
        renderer.set_element_buffer(ibuf);
        renderer.set_winding_test(winding);
 
@@ -183,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()