无法使用Assimp访问3D模型(.OBJ)的正确数量的顶点



我正在尝试访问.OBJ文件的顶点,然后对它们进行一些操作。但是Assimp Lib所示的顶点数量。实际上,与我通过使用文本编辑器(例如记事本 (打开.obj文件来检查它们并不相同。在这方面的任何建议都会非常好,谢谢。我正在使用以下代码段:

   std::string path = "model.obj";
    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate); 
    //i've changed the parameters but the issue is same
    auto mesh = scene->mMeshes[0]; //Zero index because Im loading single model only
    ofstream outputfile; //write the vertices in a text file read by assimp
    outputfile.open("vertex file.txt");

    for (int i = 0; i < mesh->mNumVertices; i++) {
        auto& v = mesh->mVertices[i];
        outputfile << v.x <<" " ;
        outputfile << v.y << " ";
        outputfile << v.z << " "<<endl;
    }
    outputfile.close();

no之间的差异。两个文件中的顶点都可以在此处以索引值

看到

是否只是现有的顶点的副本?如果是这种情况,那可能是因为说顶点是一个以上面的一部分。由于Assimp存储了正态,紫外线映射等以及顶点位置,因此是两个不同面的一部分的顶点具有两个正态,两个紫外线坐标等。这可能是额外顶点的原因。

有点老了,但也许有人会发现它有用。

只需添加aiProcess_JoinIdenticalVertices

const aiScene *scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_JoinIdenticalVertices);

Assimp Library从文件加载模型,它将构建一个树结构以将对象存储在模型中。例如:房屋模型包含墙,地板等...如果您的模型中有多个对象,则您的代码是错误的。如果是这样,您可以尝试以下方式:

void loadModel(const std::string& vPath)
{
    Assimp::Importer Import;
    const aiScene* pScene = Import.ReadFile(vPath, aiProcess_Triangulate | aiProcess_FlipUVs);
    if(!pScene || pScene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !pScene->mRootNode) 
    {
        std::cerr << "Assimp error: " << Import.GetErrorString() << endl;
        return;
    }
    processNode(pScene->mRootNode, pScene);
}
void processNode(aiNode* vNode, const aiScene* vScene)
{
    // Process all the vNode's meshes (if any)
    for (GLuint i = 0; i < vNode->mNumMeshes; i++)
    {
        aiMesh* pMesh = vScene->mMeshes[vNode->mMeshes[i]]; 
        for(GLuint i = 0; i < pMesh->mNumVertices; ++i)
        {
            // Here, you can save those coords to file
            pMesh->mVertices[i].x;
            pMesh->mVertices[i].y;
            pMesh->mVertices[i].z;
        }       
    }
    // Then do the same for each of its children
    for (GLuint i = 0; i < vNode->mNumChildren; ++i)
    {
        this->processNode(vNode->mChildren[i], vScene);
    }
} 

请小心,我不编译这些代码,而只是在文本编辑器中进行编码。祝你好运。

最新更新