From 2e9f905d2a2b0abdb7902aa3d3416a97666c489a Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 11 Sep 2012 23:52:13 +0300 Subject: [PATCH] Deactivate all arrays when deleting the active VertexArray --- source/vertexarray.cpp | 31 ++++++++++++++++++++++--------- source/vertexarray.h | 2 ++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 79eecf58..9d8e4477 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -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 *arrays, const vector *old_arrays, const float *base, unsigned stride_bytes) +{ unsigned active_tex = 0; - unsigned n_arrays = arrays.size(); - if(old) - n_arrays = max(n_arrays, old->arrays.size()); + unsigned n_arrays = arrays ? arrays->size() : 0; + if(old_arrays) + n_arrays = max(n_arrays, old_arrays->size()); for(unsigned i=0; iarrays.size() && old->arrays[i].component) ? &old->arrays[i] : 0); + const Array *arr = ((arrays && isize() && (*arrays)[i].component) ? &(*arrays)[i] : 0); + const Array *old_arr = ((old_arrays && isize() && (*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); } diff --git a/source/vertexarray.h b/source/vertexarray.h index 1887ad91..82a6b6ac 100644 --- a/source/vertexarray.h +++ b/source/vertexarray.h @@ -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 *, const std::vector *, const float *, unsigned); }; } // namespace GL -- 2.45.2