【问题标题】:Cant access correct number of vertices for a 3d model (.OBJ) using Assimp无法使用 Assimp 访问 3d 模型 (.OBJ) 的正确顶点数
【发布时间】:2017-06-04 07:28:21
【问题描述】:

我正在尝试访问 .Obj 文件的顶点,然后对它们进行一些操作。但是 assimp lib 显示的顶点数。实际上与我通过使用文本编辑器(例如 notepad++)打开 .Obj 文件来检查它们不同。在这方面的任何建议都会非常好,在此先感谢。 我正在使用以下代码 sn-p:

   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();

Difference between the no. of vertices in both files can be seen at index value here

【问题讨论】:

  • 你能显示点坐标的差异吗?
  • @jonas_toth 我附上了快照,你可以在我的问题中看到它..
  • 好吧。我对它不是很熟悉。您是否尝试过一个最小的示例,例如一个立方体或类似的东西?
  • @jonas_toth 是的,我已经尝试过有 8 个顶点的立方体,而 assimp 显示了 24 个顶点,所以它已经成功了 3 次..!!这个 ×3 背后的逻辑是什么?有什么想法吗?
  • 也许它不重用顶点。当你有一个四边形时,你可以把它分成两个三角形。它可以为每个三角形相应地复制顶点。在一个立方体中,每个顶点应该属于3个面(三角形),这样可以解释它

标签: c++ .obj assimp


【解决方案1】:

额外的顶点只是现有顶点的副本吗?如果是这种情况,可能是因为所述顶点是多个面的一部分。由于 Assimp 将法线、UV 贴图等与顶点位置一起存储,因此属于两个不同面的同一顶点具有两个法线、两个 UV 坐标等。这可能是额外顶点的原因。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • pscene->mRootNode->mNumMeshes 有零个网格。所以我无法遍历 for 循环。我也看过 UML 图,理论上顶点应该在 mRootNode 下,但事实并非如此..!!
    • 你能把obj文件发到我的邮箱吗?
    • obj 文件很大,例如50 MB 但它的格式肯定是在第一个顶点下,然后是法线、纹理和面 v 。 . Vn 。 . VT。 . F 。 .
    猜你喜欢
    • 2018-05-11
    • 2018-03-05
    • 2016-12-26
    • 2014-07-25
    • 1970-01-01
    • 2022-11-22
    • 2013-07-15
    • 2019-11-02
    • 2023-04-04
    相关资源
    最近更新 更多