PDA

View Full Version : VBO demo



vgarcia
04-30-2003, 12:54 AM
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.

zeckensack
04-30-2003, 01:00 AM
Try
glVertexPointer(3,GL_FLOAT, 0, 0);

vgarcia
04-30-2003, 02:19 AM
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.

zeckensack
04-30-2003, 03:38 AM
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.

zeckensack
04-30-2003, 03:43 AM
Oh, your glDrawArrays call seems to be wrong.
DrawArrays counts vertices, not primitives, so the last param should be changed to 3.

vgarcia
05-02-2003, 12:27 AM
You are right! The last param must be 3.

Likewise, it doesn't work... :'(

Korval
05-02-2003, 01:00 AM
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.

vgarcia
05-02-2003, 01:18 AM
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.

Korval
05-02-2003, 01:52 AM
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'.

vgarcia
05-02-2003, 02:16 AM
Perfect!!!

Thank you very much!