5
\$\begingroup\$

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:

enter image description here

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:

enter image description here

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());
\$\endgroup\$
2
  • 2
    \$\begingroup\$ Can you load simpler model and look at the index buffer data? You can use GDebugger for it : gremedy.com \$\endgroup\$ Jun 24, 2012 at 9:48
  • \$\begingroup\$ GDebugger seems to be a great tool. Though I seem to have some problems with it on an nVidia GPU on linux. Might be a subject to another question. \$\endgroup\$ Jun 27, 2012 at 16:55

1 Answer 1

4
\$\begingroup\$

The second Argument of the glDrawElements function should be the number of indices and not the number of vertices.

It should look something like this: glDrawElements(GL_TRIANGLES, indices->size(), GL_UNSIGNED_INT, indices->data());

If you have more vertices than indices, this would explain your problem. Because you try to draw more indices than which are actually stored in the buffer unless you put the indices twice.

\$\endgroup\$
1
  • \$\begingroup\$ Yep, that was it, silly mistake really. Thanks a bunch! \$\endgroup\$ Jun 27, 2012 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .