Jump to content
  • Advertisement
Sign in to follow this  

loading and drawing a vbo

This topic is 2903 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

Hi All,
I'm trying to load and a display a model using the assimp model loader and VBO's. I'm new to assimp and vbo's so am having a few problems, basically the nothing is displayed. All I'm doing now is reading the vertexes and indexes. I'm hoping someone help me out with using VBO's basically I read in the data using assimp then setting up the vertex and index vbo's as follows

/*get number of meshes*/
number_of_meshes = scene->mNumMeshes;
// For each mesh
for ( unsigned int n = 0; n < scene->mNumMeshes; ++n )

struct aiMesh* mesh = scene->mMeshes[n];

/*create face array*/
unsigned int *facearray;
facearray = (unsigned int *)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3);
unsigned int faceIndex = 0;

for ( unsigned int t = 0; t < mesh->mNumFaces; ++t )
const struct aiFace* face = &mesh->mFaces[t];
memcpy(&facearray[faceIndex], face->mIndices,3 * sizeof( unsigned int ) );
faceIndex += 3;
aMesh.numfaces = scene->mMeshes[n]->mNumFaces;

/*face vbo*/
glGenBuffers( 1, &aMesh.index_vbo );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, aMesh.index_vbo );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * (mesh->mNumFaces * 3), facearray, GL_STATIC_DRAW);

/* buffer for vertex positions*/
GLfloat *vertexarray;
vertexarray = ( GLfloat *)malloc(sizeof( GLfloat ) * mesh->mNumVertices * 3);
unsigned int vetexindex = 0;

if ( mesh->HasPositions() )
for ( unsigned int t = 0; t < mesh->mNumVertices; ++t )

vertexarray[ vetexindex ] = mesh->mVertices[ t ].x;
vertexarray[ vetexindex + 1 ] = mesh->mVertices[ t ].y;
vertexarray[ vetexindex + 2 ] = mesh->mVertices[ t ].z;
vetexindex += 3;
glGenBuffers( 1, &aMesh.vertex_vbo );
glBindBuffer( GL_ARRAY_BUFFER, aMesh.vertex_vbo );
glBufferData( GL_ARRAY_BUFFER, sizeof(float) * ( mesh->mNumVertices * 3 ), vertexarray, GL_STATIC_DRAW );

aMesh.numvertexes = scene->mMeshes[n]->mNumFaces;

/*degug for faces or elements*/
for( unsigned int x=0; x < mesh->mNumVertices ; ++x )
printf("%d array %d x %f y %f z %f \n",x,facearray[ x ], vertexarray[ facearray[ x ] ], vertexarray[ facearray[ x ] + 1 ], vertexarray[ facearray[ x ] + 2 ] );

mymeshes.push_back( aMesh );
free( vertexarray );
free( facearray );

I can see that vertexarray and facearray contain data from my printf statement. But don't know if the data has been passed to the VBO's?

next I draw the scene using

glPushMatrix( );

for (unsigned int n=0; n < number_of_meshes; n++)

printf("\n\n vertex vbo %d index vbo %d \n\n\n",mymeshes.at( n ).vertex_vbo, mymeshes.at( n ).index_vbo );

glBindBuffer(GL_ARRAY_BUFFER, mymeshes.at( n ).vertex_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mymeshes.at( n ).index_vbo );
glVertexPointer(3, GL_FLOAT, 0, 0 );

glDrawElements(GL_TRIANGLE_STRIP, mymeshes.at( n ).index_vbo, GL_UNSIGNED_INT, 0);


glBindBuffer(GL_ARRAY_BUFFER, 0);


// draw all children
//for (unsigned int n = 0; n < nd->mNumChildren; ++n)
// recursive_render( nd->mChildren[n]);

glPopMatrix( );

I'm having some problems as they seem to be so many examples with so slightly different ways of doing things.
Thanks in advance!

Share this post

Link to post
Share on other sites
Hardcode a mesh first maybe:

Mesh m;
m.mNumVertices =3;
m.mVertices[0] = something

So make a triangle and see if that works first.

Share this post

Link to post
Share on other sites
Are you sure you have a single GL_TRIANGLE_STRIP? Sound like you have GL_TRIANGLES because you have the value of 3.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!