2017-04-25 292 views
0

我正在尝试访问.Obj文件的顶点,稍后对它们进行一些操作。但assimp lib显示的Vertices数量。实际上与我通过使用文本编辑器(例如记事本++)打开.Obj文件来检查它们是不一样的。在这方面的任何建议将非常好,预先感谢。 我使用下面的代码片段:使用Assimp无法访问3D模型(.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

+0

你可以显示点坐标的差异吗? –

+0

@jonas_toth我附上了快照,你可以在我的问题中看到它.. –

+0

好吧。我不太熟悉它。你有没有试过像一个立方体这样的小例子? –

回答

0

从文件中加载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); 
    } 
} 

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

+0

pscene-> mRootNode-> mNumMeshes具有零网格。 所以我无法迭代for循环。我也看到了UML图和理论上的顶点应该在mRootNode下,但那不是事实.. .. –

+0

你可以发送obj文件到我的电子邮件吗? – licp

+0

obj文件相当大,例如50 MB 但它的格式肯定是 第一个顶点,然后是法线,纹理和面 v 。 。 Vn 。 。 Vt 。 。 f 。 。 –

0

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