Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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());
share|improve this question
2  
Can you load simpler model and look at the index buffer data? You can use GDebugger for it : gremedy.com –  Bartek Banachewicz Jun 24 '12 at 9:48
    
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. –  Morgan Bengtsson Jun 27 '12 at 16:55

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Yep, that was it, silly mistake really. Thanks a bunch! –  Morgan Bengtsson Jun 27 '12 at 16:49
    
You're welcome :) –  sonic23 Jun 27 '12 at 19:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.