首页 > 解决方案 > assimp 模型加载不会加载所有网格

问题描述

我正在努力通过带有多个网格的 assimp 加载模型,这是加载模型的代码:

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;
}

我要加载的模型有两个网格,头部和躯干

如果我像这样开始循环:for (int l = scene->mNumMeshes - 1; l >= 0 ; l--)那么它几乎正确地加载了模型,一些模型顶点没有被正确绘制,但是头部和几乎所有的躯干都被绘制了。

如果我像这样开始循环: for (unsigned int l = 0; l < scene->mNumMeshes; l++)只绘制躯干(但完全绘制,没有任何缺失的部分)

奇怪的是,顶点和索引计数都相同。

标签: c++openglassimp

解决方案


您还需要设置由 aiNode 实例描述的转换。因此,在节点上加载场景循环时,设置本地节点变换,将分配的网格绘制到当前节点。如果不应用这些转换信息,模型的渲染将被破坏。


推荐阅读