2

I'm struggling to load model via assimp with multiple meshes, here is code for loading model:

bool loadAssImp(
    const char * path,
    std::vector<unsigned short> & indices,
    std::vector<glm::vec3> & vertices,
    std::vector<glm::vec2> & uvs,
    std::vector<glm::vec3> & normals
) {

    Assimp::Importer importer;

    const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate);
    if (!scene) {
        cout << importer.GetErrorString() << endl;
        return false;
    }

    aiMesh* mesh;
    for (unsigned int l = 0; l < scene->mNumMeshes; l++)
    {
        mesh = scene->mMeshes[l];
        cout << l << endl;
        // Fill vertices positions
        vertices.reserve(mesh->mNumVertices);
        for (unsigned int i = 0; i < mesh->mNumVertices; i++) {

            aiVector3D pos = mesh->mVertices[i];
            vertices.push_back(glm::vec3(pos.x, pos.y, pos.z));
        }

        // Fill vertices texture coordinates
        uvs.reserve(mesh->mNumVertices);
        for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
            aiVector3D UVW = mesh->mTextureCoords[0][i]; // Assume only 1 set of UV coords; AssImp supports 8 UV sets.
            uvs.push_back(glm::vec2(UVW.x, UVW.y));
        }

        // Fill vertices normals
        normals.reserve(mesh->mNumVertices);
        for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
            aiVector3D n = mesh->mNormals[i];
            normals.push_back(glm::vec3(n.x, n.y, n.z));
        }


        // Fill face indices
        indices.reserve(3 * mesh->mNumFaces);
        for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
            // Assume the model has only triangles.
            indices.push_back(mesh->mFaces[i].mIndices[0]);
            indices.push_back(mesh->mFaces[i].mIndices[1]);
            indices.push_back(mesh->mFaces[i].mIndices[2]);
        }
    }

    // The "scene" pointer will be deleted automatically by "importer"
    return true;
}

model I'm trying to load has two meshes, head and torso

if I start loop like this: for (int l = scene->mNumMeshes - 1; l >= 0 ; l--) then it loads model almost correctly, some of the model vertices don't get drawn correctly but head and almost all of torso is drawn.

if I start loop like this: for (unsigned int l = 0; l < scene->mNumMeshes; l++) only torso is drawn (but is drawn fully without any missing parts)

strangely in both ways vertices and index count is the same.

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

2

You need to set the transformations descriped by the aiNode-instances as well. So when loading a scene loop over the nodes, set the local node transformation, the draw the assigned meshes to the current node. Whithout applying these transformation information the rendering of the model will be broken.

KimKulling
  • 2,654
  • 1
  • 15
  • 26