glBindBuffer(GL_ARRAY_BUFFER, obj->VBOvertices);
glVertexPointer(3, GL_FLOAT, 0, (char *) NULL);
glDrawArrays(GL_TRIANGLES, 0, obj->nbVertices);
Rendering a mesh using VBO
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
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.
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)
Some quick Pseudo code:
Hope that helps!
//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 BuffersglGenBuffersARB(1, &VBuffer);glGenBuffersARB(1, &IBuffer);//Fill Vertex buffer with dataglBindBufferARB( GL_ARRAY_BUFFER_ARB, VBuffer);glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(cVert)*numVert, verts, GL_STATIC_DRAW_ARB);//Fill Index buffer with dataglBindBufferARB( 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 changedglBindBufferARB( 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!
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!
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!
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement