Hi!
I'm currently trying to set up a function that renders a textured 2D quad using VBOs. The function changes the vertex data of the VBO every time it's called so that you can stretch it, but the texture data of the VBO doesn't change. In other words I want to be able to change the vertex data without having to change the texture data of the VBO.
When I store my VBO I have a struct that contains 4 GLfloats (VertexData2D), the first two GLfloats represent the vertex data and the last two GLfloats represent the texture data. My problem is that I can't find a way to only change the vertex data without having to change the texture data, since the entire VBO contains both vertex data and texture data.
This the rendering part in the function:
glBindTexture( GL_TEXTURE_2D, get_texture_id() ); //Set texture
//Enable vertex and texture coordinate arrays
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glBindBuffer( GL_ARRAY_BUFFER, VertexDataBuffer ); //Bind vertex data
//Update vertex buffer data
glBufferSubData( GL_ARRAY_BUFFER, 0, 4 * 4 * sizeof(GLfloat), VData );
//Set texture coordinate data
glTexCoordPointer( 2, GL_FLOAT, 4 * sizeof(GLfloat), (GLvoid*)offsetof( VertexData2D, TexCoord ) );
//Set vertex data
glVertexPointer( 2, GL_FLOAT, 4 * sizeof(GLfloat), (GLvoid*)offsetof( VertexData2D, Position ) );
//Draw quad using vertex data and index data
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, IndexBuffer );
glDrawElements( GL_QUADS, 4, GL_UNSIGNED_INT, NULL );
//Disable vertex and texture coordinate arrays
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
glDisableClientState( GL_VERTEX_ARRAY );
I first bind the correct texture and VBO, then update the VBO using
glBufferSubData()
(the VBO is set with GL_DYNAMIC_DRAW). I update the VBO starting at index 0, VData is the VBO that's going to replace the current one. Each vertex has the size
4 * sizeof(GLfloat)
and since I'm rendering a quad, the stride results in
4 * 4 * sizeof(GLfloat)
It's in glBufferSubData() that the problem is, my VBO contains both the vertex data and texture data, but I only want to change the vertex data. glBufferSubData() overwrites the current VBO, but I only want it to overwrite parts of it. This is essentially how I want it to work:
Update memory equal to: 2 * sizeof(GLfloat)
Skip memory equal to: 2 * sizeof(GLfloat)
Repeat 3 more times
What should I do? Is there another function that can do this?
Thank you!