I have an odd problem. My render() code does this
render() {
...
if (rebuildMesh == true) {
buildMesh();
}
...
}
buildMesh() {
....
if (this.vboVertexHandle == 0) {
this.vboVertexHandle = glGenBuffers();
}
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, this.vbuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
...
}
It creates a buffer if one doesn't exist, binds it, and buffers the data.
The problem comes later when another render() call decides to update the mesh. It creates the vertex buffer correctly and then tries to upload the data into the existing vboVertexHandle. This seems to result in a corrupted VBO with fragments of the original vertex data somehow "transposed" onto the existing buffer.
I was under the impression that glBufferData(GL_ARRAY_BUFFER, this.vbuffer, GL_STATIC_DRAW) would replace the entire contents of the buffer, but it doesn't appear to. It seems to prepend the new data onto the buffer?
I need to flush and upload all new data to the VBO. The VBO holds a mesh that changes infrequently.
This code works:
buildMesh2() {
....
if (this.vboVertexHandle == 0) {
this.vboVertexHandle = GL15.glGenBuffers();
} else {
GL15.glDeleteBuffers(vboVertexHandle);
this.vboVertexHandle = GL15.glGenBuffers();
}
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, this.vbuffer, GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
...
}
But that seems wasteful to delete and gen a whole new buffer..