简体   繁体   English

无法使用 Assimp 访问 3d model (.OBJ) 的正确顶点数

[英]Cant access correct number of vertices for a 3d model (.OBJ) using Assimp

I am trying to access the vertices of an.Obj file and later do some operations on them.我正在尝试访问 an.Obj 文件的顶点,然后对它们进行一些操作。 But the number of Vertices shown by assimp lib.但是assimp lib显示的顶点数。 are actually not the same as if I check them by opening the.Obj file with an text editor (eg notepad++).实际上与我通过使用文本编辑器(例如记事本++)打开.Obj 文件来检查它们是不一样的。 Any suggestion in this regard would be really nice, Thanks in advance.在这方面的任何建议都非常好,在此先感谢。 I am using following code snippet:我正在使用以下代码片段:

   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可以在此处的索引值处看到两个文件中的顶点数

Are the additional vertices just copies of the existing ones? 附加顶点只是现有顶点的副本吗? If that is the case, it is probably because said vertices are part of more than one face. 如果是这种情况,则可能是因为所述顶点是多个面的一部分。 Since Assimp stores the normals, UV mapping, etc along with the vertex position, the same vertex that is part of two different faces has two normals, two UV coordinates, etc. That is probably the reason for the additional vertices. 由于Assimp将法线,UV映射等与顶点位置一起存储,因此属于两个不同面的一部分的同一顶点具有两个法线,两个UV坐标等。这可能是附加顶点的原因。

The Assimp Library loads model from file, it will builds a tree structure to store the object in model. Assimp Library从文件加载模型,它将建立一个树形结构以将对象存储在模型中。 For example: a house model contains wall, floor, and so on... If there is more than one object in your model, you are wrong by the way at your code. 例如:一个房屋模型包含墙壁,地板等...如果模型中有多个对象,那么您的代码是错误的。 if so, you can try the following way: 如果是这样,您可以尝试以下方式:

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

Be careful, i do not compile those codes, just coding in a text editor. 请注意,我不会编译这些代码,而只是在文本编辑器中进行编码。 Good luck. 祝好运。

bit old, but maybe someone will find it useful.有点旧,但也许有人会发现它有用。

just add aiProcess_JoinIdenticalVertices只需添加aiProcess_JoinIdenticalVertices

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM