]> git.tdb.fi Git - libs/gl.git/blobdiff - source/vertexarray.cpp
Do not store generated files in the repository
[libs/gl.git] / source / vertexarray.cpp
index 79eecf58a2970ea53e850477e81ed11f6a96ce22..5065f305c3575920411b6d8ebfc707f61b6aabb3 100644 (file)
@@ -1,5 +1,5 @@
-#include "arb_multitexture.h"
-#include "arb_vertex_shader.h"
+#include <msp/gl/extensions/arb_multitexture.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
 #include "buffer.h"
 #include "error.h"
 #include "gl.h"
@@ -18,7 +18,15 @@ VertexArray::VertexArray(const VertexFormat &f):
 }
 
 VertexArray::~VertexArray()
-{ }
+{
+       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);
+       }
+}
 
 void VertexArray::reset(const VertexFormat &f)
 {
@@ -125,7 +133,11 @@ void VertexArray::apply() const
                throw invalid_operation("VertexArray::apply");
 
        const VertexArray *old = current();
-       if(!set_current(this))
+       /* If the array has been modified, apply it even if it was the last one to
+       be applied.  This is necessary to get the data updated to vertex buffer, and
+       to resync things after a format change.  Radeon drivers also have some
+       problems with modifying vertex arrays without re-setting the pointers. */
+       if(!set_current(this) && !dirty)
                return;
 
        if(vbuf)
@@ -140,14 +152,22 @@ void VertexArray::apply() const
 
        const float *base = (vbuf ? 0 : &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)
+{
        unsigned active_tex = 0;
-       unsigned n_arrays = arrays.size();
-       if(old)
-               n_arrays = max<unsigned>(n_arrays, old->arrays.size());
+       unsigned n_arrays = arrays ? arrays->size() : 0;
+       if(old_arrays)
+               n_arrays = max<unsigned>(n_arrays, old_arrays->size());
        for(unsigned i=0; i<n_arrays; ++i)
        {
-               const Array *arr = ((i<arrays.size() && arrays[i].component) ? &arrays[i] : 0);
-               const Array *old_arr = ((old && i<old->arrays.size() && old->arrays[i].component) ? &old->arrays[i] : 0);
+               const Array *arr = ((arrays && i<arrays->size() && (*arrays)[i].component) ? &(*arrays)[i] : 0);
+               const Array *old_arr = ((old_arrays && i<old_arrays->size() && (*old_arrays)[i].component) ? &(*old_arrays)[i] : 0);
                if(!arr && !old_arr)
                        continue;
 
@@ -217,9 +237,6 @@ void VertexArray::apply() const
 
        if(active_tex)
                glClientActiveTexture(GL_TEXTURE0);
-
-       if(vbuf)
-               Buffer::unbind_from(ARRAY_BUFFER);
 }