VBO demo

Hi,

I’m trying to make a demo of VBO and I have some problems and questions.

1.- Can I use glDrawArrays or I must use glDrawElements?

2.- This is my initialization code:
void initVBO()
{
glEnableClientState(GL_VERTEX_ARRAY);

glGenBuffersARB(1, &vbo);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
    	glBufferDataARB(GL_ARRAY_BUFFER_ARB, 9*sizeof(GLfloat), data, GL_STATIC_DRAW_ARB);

}

// Where data is an array of 9 elements containing de coordinates of a triangle

3.- And this is my display code:
void display()
{
// other display code
glBindBufferARB(GL_ARRAY_BUFFER_ARB, kk);
glVertexPointer(3,GL_FLOAT, 0, data);
glDrawArrays(GL_TRIANGLES, 0, 1);
glutSwapBuffers();
}

And it doesn’t work. What I’m doing bad?

Thank you very much.

Try
glVertexPointer(3,GL_FLOAT, 0, 0);

The last parameter of glVertexArray is the pointer to the array of vertices, isn’t it?

However, glVertexPointer(3,GL_FLOAT, 0, 0); doesn’t work.

Thank you very much.

I haven’t followed VBO too closely yet, but the last param changes roles once you enable a VBO.

Instead of being a direct pointer to data (which you just don’t have! it’s somewhere in graphics memory after all), it specifies a byte offset into the VBO.
That’s why I suggested that. There may be further steps required to truly ‘enable’ the VBO, someone else will probably know better than I do.

Oh, your glDrawArrays call seems to be wrong.
DrawArrays counts vertices, not primitives, so the last param should be changed to 3.

You are right! The last param must be 3.

Likewise, it doesn’t work… :’(

This may sound silly or patronising, but did you actually put your vertex data in “data” before doing the glBufferDataARB call? That call, much like glTexImage2D, transfers data out of your memory and into the card.

Yes, I put my vertex data in the array before the call.

If I comment the line
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 9*sizeof(GLfloat), data, GL_STATIC_DRAW_ARB);

the applications works, using the “normal” vertex array, but if I try to use VBO, the application doesn’t work.

glBindBufferARB(GL_ARRAY_BUFFER_ARB, kk);

Shouldn’t that be ‘vbo’, not ‘kk’? What is ‘kk’, anyway?

If I comment the line:
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 9*sizeof(GLfloat), data, GL_STATIC_DRAW_ARB);

the applications works, using the “normal” vertex array, but if I try to use VBO, the application doesn’t work.

As it has been pointed out in this thread, and as the spec mentions on several occasions, the call to glVertexPointer should not have a pointer to the vertex array; that is implicit by the binding of the buffer. Instead, it should have a byte offset from the beginning of the array to the beginning of your vertex data. In this case, 0 should do. Or, more specifically, (void*)0.

Because of this, simply commenting out that line would not cause a functioning vbo program to work as a non-vbo program. That program would start to trash memory/cause a GPF/segfault if it didn’t swap the (void*)0 for ‘data’.

Perfect!!!

Thank you very much!