PDA

View Full Version : VBO code problems



wadesworld
09-15-2009, 09:55 PM
Anyone see any errors in this very simple test VBO code? It looks correct to me, but I'm not seeing any evidence of drawing. My immediate mode code does draw however.



struct vert3d_struct
{
GLfloat x;
GLfloat y;
GLfloat z;
};

typedef struct vert3d_struct vert3d;


vert3d *vertices;
GLushort *indices;
GLuint vboBuffers[2];

vertices[0].x = -20.0;
vertices[0].y = -20.0;
vertices[0].z = 0;

vertices[1].x = 20.0;
vertices[1].y = -20.0;
vertices[1].z = 0;

vertices[2].x = 20.0;
vertices[2].y = 20.0;
vertices[2].z = 0;

vertices[3].x = -20.0;
vertices[3].y = 20.0;
vertices[3].z = 0;


verticesCount = 4;

indices[0]=0;
indices[1]=1;
indices[2]=2;
indices[3]=3;

vertices = (vert3d *) malloc ( 4 * (sizeof(vert3d)));

indices = (GLushort *) malloc( 4 * sizeof(GLushort) );

glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboBuffers[0]); //vertex data
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboBuffers[1]); //indicies

glBufferDataARB(GL_ARRAY_BUFFER_ARB, 4 * (sizeof(vert3d)), vertices, GL_STATIC_DRAW_ARB);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 4 * sizeof(GLushort), indices, GL_STATIC_DRAW_ARB);

glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(0.0, 0.0, 1.0);
glDrawElements(GL_QUADS, verticesCount, GL_UNSIGNED_SHORT, 0);
glDisableClientState(GL_VERTEX_ARRAY);



Thanks for any thoughts.

scratt
09-15-2009, 10:58 PM
I can't see you generating the buffers anywhere. Have these been allocated before?

Also I always enable the vertex array before I set the Vertex Pointer. I can't see you doing the latter anywhere.. The Vertex Pointer is pretty crucial to any drawing! ;)

I always found this web page a useful resource when I was learning VBOs.

http://www.songho.ca/opengl/gl_vbo.html

wadesworld
09-18-2009, 09:34 AM
I can't see you generating the buffers anywhere. Have these been allocated before?

Also I always enable the vertex array before I set the Vertex Pointer. I can't see you doing the latter anywhere.. The Vertex Pointer is pretty crucial to any drawing!

For the benefit of others, I'll have to embarrass myself and admit my mistakes:

Yes, both of these things were done. They were simply omissions to my cut-and-paste.

The problem actually turned out to be that as I rewrote things in an effort to get VBO's working, somehow I managed to move my buffer initialization function call to a point after the glBufferDataARB call. Thus, I was copying an empty array.

That's not apparent from the code above though, because as I was cutting and pasting the code into the message board, I placed it in the correct order, but somehow still failed to see it in my original code.

Anyhow, thanks for the help. Problem solved.