简体   繁体   中英

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. But the number of Vertices shown by assimp lib. are actually not the same as if I check them by opening the.Obj file with an text editor (eg notepad++). 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.

The Assimp Library loads model from file, it will builds a tree structure to store the object in model. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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