Search code examples
c++opengl

OpenGL Updating VBO data by re-specification


I'm trying to change the contents of a VBO, but it's for a stream draw so I'm trying to understand and follow the concept explained here (and here), but I'm getting an error when I attempt that.

This is what I'm trying to do:

void Model::Draw()
{
    //Changing only the vertexesVBO
    _vertexes[0] += 0.0001f;
    glBindBuffer(GL_ARRAY_BUFFER, _vboVertexes._bufferID);

    //This works
    //glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(float), &_vertexes[0]);

    //But this doesn't
    glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, _vertexCount * sizeof(float), _vertexes);

    //Call vertexVBO, uvVBO, indexIBO, disable them
}

Supposedly, I'm creating a new BufferData of size _vertexCount * sizeof(float) and uninitialized data, and uploading the same _vertexes data to it to be used in the next draw call. But this is throwing the error of access violation reading.

How do I get this working? And if I understood correctly, with this I shouldn't need to implement a double buffer VBO for animation, right?


Solution

  • Supposedly, I'm creating a new BufferData of size _vertexCount * sizeof(float) and uninitialized data, and uploading the same _vertexes data to it to be used in the next draw call.

    No. You just create a new buffer storage of size 0. And the update with glBufferSubData() will fail as it is accessing the buffer out of bounds.