]> git.tdb.fi Git - libs/gl.git/commitdiff
Deactivate all arrays when deleting the active VertexArray
authorMikko Rasa <tdb@tdb.fi>
Tue, 11 Sep 2012 20:52:13 +0000 (23:52 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 11 Sep 2012 20:53:47 +0000 (23:53 +0300)
source/vertexarray.cpp
source/vertexarray.h

index 79eecf58a2970ea53e850477e81ed11f6a96ce22..9d8e4477b0ab5a4e3008c72b4f048de2b9d213f7 100644 (file)
@@ -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)
 {
@@ -140,14 +148,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 +233,6 @@ void VertexArray::apply() const
 
        if(active_tex)
                glClientActiveTexture(GL_TEXTURE0);
-
-       if(vbuf)
-               Buffer::unbind_from(ARRAY_BUFFER);
 }
 
 
index 1887ad912d52547d89257e48f74af7d10e58db47..82a6b6ac5260cb3bd074404480187022f2b03b37 100644 (file)
@@ -70,6 +70,8 @@ public:
        const float *operator[](unsigned i) const { return &data[0]+i*stride; }
 
        void apply() const;
+private:
+       static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
 };
 
 } // namespace GL