]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.cpp
Minor fixes
[libs/gl.git] / source / vertexarray.cpp
index e453a3f12a569a70f7868288647dee37c6f45b33..867aee7b189b71f348950741e4f2ccda6ae18921 100644 (file)
@@ -3,6 +3,7 @@
 #include "buffer.h"
 #include "error.h"
 #include "gl.h"
+#include "mesh.h"
 #include "vertexarray.h"
 
 using namespace std;
@@ -10,21 +11,17 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-VertexArray::VertexArray(const VertexFormat &f):
-       dirty(false)
+VertexArray::VertexArray(const VertexFormat &f)
 {
        reset(f);
 }
 
 VertexArray::~VertexArray()
 {
+       /* Unbind accesses the current VertexArray, so a call from ~Bindable would
+       try to access destroyed data. */
        if(current()==this)
-       {
-               /* We must deactivate arrays here, or apply() would try to access deleted
-               data on the next invocation. */
-               set_current(0);
-               apply_arrays(0, &arrays, 0, 0);
-       }
+               unbind();
 }
 
 void VertexArray::reset(const VertexFormat &f)
@@ -88,6 +85,7 @@ void VertexArray::reserve(unsigned n)
 float *VertexArray::append()
 {
        data.insert(data.end(), stride, 0.0f);
+       update_offset();
        dirty = true;
        return &*(data.end()-stride);
 }
@@ -103,15 +101,13 @@ unsigned VertexArray::get_data_size() const
        return data.size()*sizeof(float);
 }
 
-void VertexArray::upload_data() const
-{
-       get_buffer()->sub_data(get_offset(), get_data_size(), &data[0]);
-}
-
-void VertexArray::apply() const
+void VertexArray::bind() 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
@@ -122,19 +118,13 @@ void VertexArray::apply() const
                return;
 
        Buffer *vbuf = get_buffer();
-       if(vbuf)
-       {
-               vbuf->bind_to(ARRAY_BUFFER);
-               if(dirty)
-                       update_buffer();
-       }
+       Bind _bind_vbuf(vbuf, ARRAY_BUFFER);
+       if(vbuf && dirty)
+               update_buffer();
 
        const float *base = (vbuf ? reinterpret_cast<float *>(get_offset()) : &data[0]);
        unsigned stride_bytes = stride*sizeof(float);
        apply_arrays(&arrays, (old ? &old->arrays : 0), base, stride_bytes);
-
-       if(vbuf)
-               Buffer::unbind_from(ARRAY_BUFFER);
 }
 
 void VertexArray::apply_arrays(const vector<Array> *arrays, const vector<Array> *old_arrays, const float *base, unsigned stride_bytes)
@@ -218,6 +208,13 @@ void VertexArray::apply_arrays(const vector<Array> *arrays, const vector<Array>
                glClientActiveTexture(GL_TEXTURE0);
 }
 
+void VertexArray::unbind()
+{
+       const VertexArray *old = current();
+       if(set_current(0))
+               apply_arrays(0, &old->arrays, 0, 0);
+}
+
 
 VertexArray::Array::Array():
        component(0),