Jump to content
  • Advertisement
Sign in to follow this  
qbic

Rendering a mesh using VBO

This topic is 4864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do o use polygon data with VBO's? I have loaded an .obj model and trying to rendering it using VBO's, but vertices aren't orderd in such way that it can be rendered without polygons. This is the rendering code i'm using
glBindBuffer(GL_ARRAY_BUFFER, obj->VBOvertices);
glVertexPointer(3, GL_FLOAT, 0, (char *) NULL);
glDrawArrays(GL_TRIANGLES, 0, obj->nbVertices);

I'm not sure if i am making my self very clear on this question, so please ask if you don't understand what i mean or if you want more info.

Share this post


Link to post
Share on other sites
Advertisement
Either order vertices or use glIndexPointer and glDrawElements (indices can be stored in VBO too) and supply opengl an buffer of indexes of vertices which to render (in specified order)

Share this post


Link to post
Share on other sites
Some quick Pseudo code:


//Check for all the appropriate extensions first and such. Yadda Yadda Yadda.

//-----------------
// Loading Code
//-----------------

#define BUFFER_OFFSET(i) ((float *)NULL + (i))

struct cVert
{
float u, v; //TexCoords
float nx, ny, nz; //Normals
float x, y, z; //Position
};

GLuint VBuffer, IBuffer;

//Generate Buffers
glGenBuffersARB(1, &VBuffer);
glGenBuffersARB(1, &IBuffer);

//Fill Vertex buffer with data
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBuffer);
glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(cVert)*numVert, verts, GL_STATIC_DRAW_ARB);

//Fill Index buffer with data
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, IBuffer);
glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(uint)*numIndex, indices, GL_STATIC_DRAW_ARB);

//-----------------
// Rendering Code
//-----------------

//These only have to be set again if they have been NULLED out or changed
glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBuffer);
glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, IBuffer);

//You can also use glInterleavedArrays( GL_T2F_N3F_V3F, 0, NULL );
glTexCoordPointer( 2, GL_FLOAT, sizeof(cVert), BUFFER_OFFSET(0) );
glNormalPointer( GL_FLOAT, sizeof(cVert), BUFFER_OFFSET(2) );
glVertexPointer( 3, GL_FLOAT, sizeof(cVert), BUFFER_OFFSET(5) );

//Depending on implementation glDrawRangeElements() may be faster.
glDrawElements( GL_TRIANGLES, numIndex, GL_UNSIGNED_INT, NULL );

//Done! Tada!



Hope that helps!

Share this post


Link to post
Share on other sites
Just 2 more questions about the pseudo code.
First, the...
#define BUFFER_OFFSET(i) ((float *)NULL + (i))
Should that be NULL or should i replace it with the accual buffer name?
And second, how should the index buffer looks like?
Just and array with vertex numbers?

Sorry for beeing noobish :)
Cheers!

Share this post


Link to post
Share on other sites
VBO works as follows - you bind a buffer and then supply pointers for vertex/texcoord/normal arrays as if you're not using VBO. Except that when using VBO you supply offsets to buffer bound to VBO.

So no, NULL is there for a purpose... So BUFFER_OFFSET returns you a index to ith float value in buffer bound to VBO (it's basically the same as #define BUFFER_OFFSET(i) (GLfloat *)(sizeof(GLfloat)*i - maybe it will be more clear to you)

and the index buffer is just array of GLuint specifying vertex numbers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!