Is this the best way to work VBOs?


public class RenderEngine {
	private FloatBuffer fb;
	int vch = 1;

public RenderEngine() {
fb = BufferUtils.createFloatBuffer(100 * 10 * 10 * 6 * 4 * 6);

//place stuff in fb

 fb.flip();
}
public void render() {
		if(!bFlipped) {
			fb.flip();
			bFlipped = true;
		}

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_COLOR_ARRAY);

		glBindBufferARB(GL_ARRAY_BUFFER_ARB, vch);
		glBufferDataARB(GL_ARRAY_BUFFER_ARB, fb, GL_STATIC_DRAW_ARB);
		glVertexPointer(3, GL_FLOAT, (3 * 2) << 2, 0);
		glColorPointer(3, GL_FLOAT, (3 * 2) * 4, 3 * 4);

		glDrawArrays(GL_QUADS, 0, fb.capacity() / 4);

		glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

		glDisableClientState(GL_COLOR_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);
	}
}

This code seems somewhat inefficient to me for a number of reasons:

  • The vch variable is defaulted to 1 and I’m not really 100% sure why this works, I imagine this is used for rendering multiple VBOs
  • Every time I go to render the VBO, i redo the glBufferData() call, I have seen examples where people call this once when they initialise the VBO and not every render cycle

If anyone could give me any pointers on mistakes I have made please let me know.
(please note, this code does work, I just don’t see it as very efficient which is why I’m asking)

You should use glGenBuffers to get the VBO handle.

glBufferData uploads the float buffer data to your VBO. You don’t need to call it every frame unless your data is changing every frame. In your render function, you just need to enable client states, setup vertex pointers, and draw arrays.

VBOs is core as of GL 1.5 so you really should not be using ARB unless you are targeting computers from the 90s. :stuck_out_tongue:

Also I’m not sure why you are flipping buffers like that. You can read more about buffers here:

Ok, thanks I’ve made some changes now. Thanks for the info about ARB buffers aswell, i thought they were the more modern version of OpenGL.

Thanks a bunch ;D