Strange mesh import problem with Assimp and OpenGL
- by Morgan
Using the assimp library for importing 3D data into an OpenGL application. I get some strange problems regarding indexing of the vertices:
If I use the following code for importing vertex indices:
for (unsigned int t = 0; t < mesh->mNumFaces; ++t)
{
const struct aiFace * face = &mesh->mFaces[t];
if (face->mNumIndices == 3)
{
indices->push_back(face->mIndices[0]);
indices->push_back(face->mIndices[1]);
indices->push_back(face->mIndices[2]);
}
}
I get the following result:
Instead, if I use the following code:
for(int k = 0; k < 2 ; k++)
{
for (unsigned int t = 0; t < mesh->mNumFaces; ++t)
{
const struct aiFace * face = &mesh->mFaces[t];
if (face->mNumIndices == 3)
{
indices->push_back(face->mIndices[0]);
indices->push_back(face->mIndices[1]);
indices->push_back(face->mIndices[2]);
}
}
}
I get the correct result:
Hence adding the indices twice, renders the correct result?
The OpenGL buffer is populated, like so:
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
indices->size() * sizeof(unsigned int),
indices->data(),
GL_STATIC_DRAW);
And rendered as follows:
glDrawElements(GL_TRIANGLES,
vertexCount*3,
GL_UNSIGNED_INT,
indices->data());