So I’m writing a voxel game. So, if a block changes in a chunk, I need to rerender that chunk’s VBO with the new data. How do I do this?
Not really a direct answer to your question, but I certainly do suggest you take a peek at Thinmatrix’s tutorials where he explains the use of VBOs and writes you basically the sample code needed. :point:
I appreciate the tutorials and am sure they’ll be helpful, but I still need to know how to rerender a VBO…
glBufferData() reallocates the VBO.
Ok and a quick related question: when I want to draw my VBO I am doing glEnableClientState, drawing it, and doing glDisableClientState. Is there any reason to do it this way or can I just do glEnableClientState at the start of my program and leave it enabled?
glEnableClientState() makes OpenGL attempt to read data from a VBO. If you at any point draw something with immediate mode, or don’t have a valid buffer for an attribute anymore or read out of bounds, you can crash the entire program. Example:
- Draw model 1 with 100 vertices, each with 3D positions, texture coordinates and normals. glDisableClientState() is not called afterwards.
- Draw model 2 with 5000 vertices, each with only 3D positions and texture coordinates. Potential for a crash here as the previous glNormalPointer() points to the buffer of model 1 and it’s left enabled, meaning that the driver will read out of bounds of the VBO, potentially randomly crashing the JVM.
Making sure to always disable state you’re not using anymore is a good idea.
The best solution is to keep track of the state of everything and only change it when it actually needs to be changed, as OpenGL commands have a lot of overhead. In the example above, you’d want to avoid disabling and enabling attributes inbetween drawing model 1 and 2. You’d realize that position and texture coordinates are already enabled and only disable normals (1 call), instead of disabling all 3 of them and enabling position and texcoords again (5 calls). This is probably overkill though unless you’re drawing a crapload of models each frame.