Skip to main content
gamedev.net
🔒 Locked

Converting a simple array into a VBO array

Started by penetrator Apr 6, 2005 at 8:44 AM 9 replies 1.9k views
Original Post
penetrator
penetrator
I have the following array: POLYS=2902 //number of polygons to draw POINTS=4 // each poly has 4 vertices COORD=3 // each point has 3 coords, xyz GLfloat data[POLYS*POINTS*COORD]; I draw (successfully) an object made of a series of quad strips looping through the vertices like this: for (int tt1=0; tt1<(POLYS*POINTS*COORD); tt1+=12)) { glBegin(GL_QUAD_STRIP); glVertex3f(data[tt1+0],data[tt1+1],data[tt1+2]); glVertex3f(data[tt1+3],data[tt1+4],data[tt1+5]); glVertex3f(data[tt1+6],data[tt1+7],data[tt1+8]); glVertex3f(data[tt1+9],data[tt1+10],data[tt1+11]); glEnd(); } I later tried to user VBO so i converted the code and used the following, but the object rendered has lot of faces more than it should be: glGenBuffersARB(1, &buffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data), data, GL_STATIC_DRAW_ARB); glEnableClientState( GL_VERTEX_ARRAY ); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer); glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL ); glDrawArrays(GL_QUAD_STRIP, 0, POLYS*POINTS*COORD); glDisableClientState( GL_VERTEX_ARRAY ); Packing data in VBO seems to be straightforward, i just have a mega array with sequential vertices, but for some reason there must be an error somewhere. Any help is greatly appreciated, is about a week i'm trying to solve this matter.
rick_appleton
rick_appleton
because you only call glVertex... four times between your glBegin() and glEnd() pair, the STRIP part isn't used. STRIP means that after the initial 4 vertices, each and every single vertex adds another quad. Try it with GL_QUADS in both the immediate mode and VBO's, that should work.

Also, I'm pretty certain that

glDrawArrays(GL_QUAD_STRIP, 0, POLYS*POINTS*COORD);

should be:

glDrawArrays(GL_QUAD_STRIP, 0, POLYS*POINTS);

which then becomes

glDrawArrays(GL_QUADS, 0, POLYS*POINTS);
penetrator
penetrator
Hi Rick, thx for your answer. I tried using QUAD instead of QUAD_STRIP but i get no polygons on the screen, both in immediate and VBO mode.
penetrator
penetrator
No, it renders only if i use quad_strip. And see that it works just fine in immediate mode. I suspect there is surely an error in how the vbo routines handle the array, but can't figure out what. it's a damn thing !
thx anyway for your help
rick_appleton
rick_appleton
Could you post the code you have when trying to use GL_QUADS in immediate mode? I'm certain that should work.

ps. if you use MSN you can add me if you want, it'll go a bit quicker (rick_appleton at hotmail.com)
penetrator
penetrator
ok, i added you at msn. i am the_propulsor@hotmail.com

tot ziens
rick_appleton
rick_appleton
Just to finish this off:

- moved from GL_QUAD_STRIP to GL_QUADS to accomodate the glBegin() and glEnd() being inside the original for loop
- moved the glBegin() and glEnd() out of the loop so the behaviour of immediate mode and VBO's is similar
- swapped every 3rd and 4th vertex around so the data is in GL_QUADS format rather than GL_QUAD_STRIP format- changed glDrawArrays line to:
glDrawArrays(GL_QUADS, 0, POLYS*POINTS);

Please note that drawing this with GL_QUADS might not be the best solution, but it most closely mimicks the original code.

Maybe penetrator will care to post the working code he ended up with.
penetrator
penetrator
Ok, hopefully can help other folks who run into this. I just wanted to render dynamic shadows (see Nehe lesson #27) using VBO instead of immediate mode, so to improve performances. After some hours spent figuring it out, thanks to Rick, i got to this:

1) re-arrange the array containing the vertices so that you can organize geometry for GL_QUADS (instead of GL_QUAD_STRIP like in Nehe lesson #27). This is easy, you just have to swap vertex 3 and 4 and is done.

2) Just draw the VBO object like this:
glEnableClientState( GL_VERTEX_ARRAY );
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(data),data,GL_DYNAMIC_DRAW_ARB);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, buffer);
glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL );
glDrawArrays(GL_QUADS, 0, POLYS*POINTS);
glDisableClientState( GL_VERTEX_ARRAY );


where data is the array containing the vertices: data[POLYS*POINTS*3]

Switching from immediate to VBO mode gave the app a boost of 25% of performances, and perhaps the VBO routine can be optimized even more (i'm currently studying the glBufferSubDataARB function).
Feel free to contact me if you need more info or if you have questions about the code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.