Hi,
I started using OpenGL about a year ago and did some tests with immediate mode rendering. After a few months I started to work with VBO’s. Now I wanted to see how much performance I gained by switching from immediate mode to VBO rendering, but to my surprise the performance didnt increase at all. In fact, most of the time it was slightly worse?!
For my test I create only a single VBO and I want to render it 20000 times per frame (with some different translation on the modelview-matrix).
My guess is that I probably did something wrong with setting up the VBO’s, here are the important parts of the code:
(Please notoe, I use Java and LWJGL to access the native OpenGL functions)
Initializing the VBO:
private void load_vbo() {
int FLOAT_BYTE_SIZE = 4;
/*
* Define vertices and indices used by the VBO:
*/
float[] vertices = new float[] {
0, 0, 0,
0, 1, 0,
1, 1, 0,
1, 0, 0
};
byte[] indices = new byte[] {
0, 1, 2, 2, 3, 0
};
/*
* Create the vertex buffer; upload vertices into buffer.
*/
vbo_id = GL15.glGenBuffers();
FloatBuffer vbo_buf = ByteBuffer.allocateDirect(vertices.length * FLOAT_BYTE_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
vbo_buf.put(vertices);
vbo_buf.flip();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo_id);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vbo_buf, GL15.GL_STATIC_DRAW);
/*
* Create the index buffer; upload indices into buffer.
*/
ibo_id = GL15.glGenBuffers();
ByteBuffer ibo_buf = ByteBuffer.allocateDirect(indices.length).order(ByteOrder.nativeOrder());
ibo_buf.put(indices);
ibo_buf.flip();
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo_id);
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo_buf, GL15.GL_STATIC_DRAW);
/*
* Make vbo ready for drawing:
*/
int POS_COUNT = 3;
int TEXCOORD_COUNT = 0;
int stride = (POS_COUNT + TEXCOORD_COUNT) * FLOAT_BYTE_SIZE;
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
//GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glVertexPointer(POS_COUNT, GL11.GL_FLOAT, stride, 0);
//GL11.glTexCoordPointer(TEXCOORD_COUNT, GL11.GL_FLOAT, stride, POS_COUNT * FLOAT_BYTE_SIZE);
}
For drawing the VBO:
GL11.glMatrixMode(GL11.GL_MODELVIEW);
for (int i = 0; i < IMAGE_COUNT; i++) {
GL11.glPushMatrix();
GL11.glTranslatef(someX, someY, 0);
GL11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_BYTE, 0);
GL11.glPopMatrix();
}
For drawing Immediate-Mode:
GL11.glBegin(GL11.GL_QUADS);
for (int i = 0; i < IMAGE_COUNT; i++) {
GL11.glVertex3f(someX, someY, 0);
GL11.glVertex3f(someX, someY + 1, 0);
GL11.glVertex3f(someX + 1, someY + 1, 0);
GL11.glVertex3f(someX + 1, someY, 0);
}
GL11.glEnd();
Everything else works, its just the performance which isnt that good.
Thanks for your help.