From ec98c7fe03e699364fbb745cbe0c6a591264e4a9 Mon Sep 17 00:00:00 2001 From: manuelk Date: Thu, 30 Jan 2014 17:29:14 -0800 Subject: [PATCH] Move VBO buffer allocation out of allocate() and into BindVBO() fixes #256 --- opensubdiv/osd/cpuGLVertexBuffer.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/opensubdiv/osd/cpuGLVertexBuffer.cpp b/opensubdiv/osd/cpuGLVertexBuffer.cpp index 2b1dfd87..98369dd3 100644 --- a/opensubdiv/osd/cpuGLVertexBuffer.cpp +++ b/opensubdiv/osd/cpuGLVertexBuffer.cpp @@ -39,7 +39,10 @@ OsdCpuGLVertexBuffer::OsdCpuGLVertexBuffer(int numElements, int numVertices) OsdCpuGLVertexBuffer::~OsdCpuGLVertexBuffer() { delete[] _cpuBuffer; - glDeleteBuffers(1, &_vbo); + + if (_vbo) { + glDeleteBuffers(1, &_vbo); + } } OsdCpuGLVertexBuffer * @@ -72,17 +75,23 @@ OsdCpuGLVertexBuffer::GetNumVertices() const { float* OsdCpuGLVertexBuffer::BindCpuBuffer() { + _dataDirty = true; // caller might modify data return _cpuBuffer; } GLuint OsdCpuGLVertexBuffer::BindVBO() { + if (not _dataDirty) return _vbo; int size = GetNumElements() * GetNumVertices() * sizeof(float); + if (not _vbo) { + glGenBuffers(1, &_vbo); + } + glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, size, _cpuBuffer, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -93,15 +102,9 @@ OsdCpuGLVertexBuffer::BindVBO() { bool OsdCpuGLVertexBuffer::allocate() { + _cpuBuffer = new float[GetNumElements() * GetNumVertices()]; _dataDirty = true; - int size = GetNumElements() * GetNumVertices() * sizeof(float); - - glGenBuffers(1, &_vbo); - glBindBuffer(GL_ARRAY_BUFFER, _vbo); - glBufferData(GL_ARRAY_BUFFER, size, 0, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - return true; }