]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.cpp
Restore old buffer binding when setting data
[libs/gl.git] / source / vertexarray.cpp
index 395f217a97ecff6c9c76c3a6985fdfa97f163978..54de0c0d1d01beadef6228a8bb29fd7ab7a5e66f 100644 (file)
@@ -6,50 +6,46 @@ Distributed under the LGPL
 */
 
 #include "arb_vertex_program.h"
+#include "buffer.h"
 #include "extension.h"
 #include "gl.h"
 #include "version_1_2.h"
 #include "version_1_3.h"
 #include "vertexarray.h"
-#include "vertexbuffer.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
+VertexArray::ArrayMask VertexArray::enabled_arrays;
+
 VertexArray::VertexArray(const VertexFormat &f):
-       vbuf(0),
-       own_vbuf(false)
+       defer_vbuf(true),
+       dirty(false)
 {
        reset(f);
 }
 
 VertexArray::~VertexArray()
-{
-       if(own_vbuf)
-               delete vbuf;
-}
+{ }
 
 void VertexArray::use_vertex_buffer()
 {
-       if(vbuf && own_vbuf)
+       if(vbuf)
                return;
 
        vbuf = new Buffer(ARRAY_BUFFER);
-       own_vbuf = true;
-
-       update_data();
+       defer_vbuf = false;
+       dirty = true;
 }
 
 void VertexArray::use_vertex_buffer(Buffer *b)
 {
-       if(own_vbuf)
-               delete vbuf;
        vbuf = b;
-       own_vbuf = false;
-
-       update_data();
+       vbuf.keep();
+       defer_vbuf = false;
+       dirty = true;
 }
 
 void VertexArray::reserve(unsigned n)
@@ -89,9 +85,16 @@ void VertexArray::apply() const
                throw InvalidState("Trying to apply a vertex array with no data");
 
        if(vbuf)
-               vbuf->bind();
+       {
+               vbuf->bind_to(ARRAY_BUFFER);
+               if(dirty)
+               {
+                       vbuf->data(data.size()*sizeof(float), &data[0]);
+                       dirty = false;
+               }
+       }
 
-       const float *base = vbuf?0:&data[0];
+       const float *base = (vbuf ? 0 : &data[0]);
        unsigned offset = 0;
        ArrayMask found;
        unsigned bpv = stride*sizeof(float);
@@ -170,28 +173,31 @@ void VertexArray::apply() const
                glClientActiveTexture(GL_TEXTURE0);
 
        if(vbuf)
-               vbuf->unbind();
+               Buffer::unbind_from(ARRAY_BUFFER);
 }
 
-/**
-Updates the VertexArray data to the VertexBuffer tied to the array, if any.
-*/
-void VertexArray::update_data()
+float *VertexArray::append()
 {
-       if(vbuf)
-       {
-               vbuf->data(data.size()*sizeof(float), &data[0]);
-               vbuf->unbind();
-       }
+       data.insert(data.end(), stride, 0.0f);
+       set_dirty();
+       return &*(data.end()-stride);
 }
 
-float *VertexArray::append()
+float *VertexArray::modify(unsigned i)
 {
-       data.insert(data.end(), stride, 0.0f);
-       return &*data.end()-stride;
+       set_dirty();
+       return &data[0]+i*stride;
 }
 
-VertexArray::ArrayMask VertexArray::enabled_arrays;
+void VertexArray::set_dirty()
+{
+       dirty = true;
+       if(defer_vbuf)
+       {
+               vbuf = new Buffer(ARRAY_BUFFER);
+               defer_vbuf = false;
+       }
+}
 
 
 VertexArray::ArrayMask::ArrayMask()