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

jjhzyzn0  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(232)

我试图访问一个.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();

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

uubf1zoe

uubf1zoe1#

额外的顶点只是现有顶点的副本吗?如果是这样的话,可能是因为所述顶点是多个面的一部分。由于Assimp存储法线、UV贴图等沿着顶点位置,作为两个不同面的一部分的同一顶点具有两个法线、两个UV坐标等。这可能是额外顶点的原因。

qzwqbdag

qzwqbdag2#

有点旧,但也许有人会发现它有用。
只要加上aiProcess_JoinIdenticalVertices

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

htzpubme3#

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

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

相关问题