Assimp Faces all have indices (0,1,2)

2019-07-12 19:00发布

问题:

I'm using Assimp to load in OBJ files to render in OpenGL using my own rendering pipeline.

But when I load in a file, every face has indices (0,1,2), rather than appropriate entries into the vertex array.

Every example I could find does something similar to this (which is what I'm doing):

for (size_t k = 0; k<mesh->mNumFaces; ++k)
{
    if (mesh->mFaces->mNumIndices == 3)
    {
        out.index_list.push_back(mesh->mFaces->mIndices[0]);
        out.index_list.push_back(mesh->mFaces->mIndices[1]);
        out.index_list.push_back(mesh->mFaces->mIndices[2]);
    }
    else
    {
        std::cout << "wierd number of indices to a face: " << mesh->mFaces->mNumIndices << std::endl;
    }
}

or this (which I've tried, and is very wrong):

for (size_t k = 0; k<mesh->mNumFaces; ++k)
{
    if (mesh->mFaces->mNumIndices == 3)
    {
        out.index_list.push_back(mesh->mFaces->mIndices[0]+k*3);
        out.index_list.push_back(mesh->mFaces->mIndices[1]+k*3);
        out.index_list.push_back(mesh->mFaces->mIndices[2]+k*3);
    }
    else
    {
        std::cout << "wierd number of indices to a face: " << mesh->mFaces->mNumIndices << std::endl;
    }
}

I've also tried some variations based on the relative number of vertexes and faces in a mesh, guessing that it should be a triangle strip, etc... and that also isn't working.

Example:

if (mesh->mNumFaces == mesh->mNumVertices-2)
    for (size_t k = 0; k<mesh->mNumVertices-2; ++k)
    {
        if (k%2)
        {
            out.index_list.push_back(k+1);
            out.index_list.push_back(k+0);
            out.index_list.push_back(k+2);
        }
        else
        {
            out.index_list.push_back(k+0);
            out.index_list.push_back(k+1);
            out.index_list.push_back(k+2);
        }
    }
else if...

I'm obviously missing something very basic and obvious here, but I can't see what it is.

回答1:

I think your code just loads the the indices of the first triangles(faces) in the mesh(aiMesh).

mesh->mFace is a pointer which points to the first element of the array of aiFaces.

Your (first) code doesn't take into account the variable k, your face index.

Instead, do it like this:

for (size_t k = 0; k<mesh->mNumFaces; ++k)
{
    if (mesh->mFaces->mNumIndices == 3)
    {
        // kth face!
        out.index_list.push_back(mesh->mFaces[k].mIndices[0]);
        out.index_list.push_back(mesh->mFaces[k].mIndices[1]);
        out.index_list.push_back(mesh->mFaces[k].mIndices[2]);
    }
    else
    {
        std::cout << "wierd number of indices to a face: " << mesh->mFaces->mNumIndices << std::endl;
    }
}

This way your index_list should be filled with the correct indices.

Hope this helps! :)



回答2:

mFaces is an array... ie , a pointer. Doing mFaces->mIndices is like doing mFaces[0]->mIndices.

That's why you are getting the same indices...