我正在尝试访问.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
答案 0 :(得分:1)
附加顶点是否只是现有顶点的副本?如果是这种情况,可能是因为所述顶点是多个面的一部分。由于Assimp存储法线,UV映射等以及顶点位置,因此作为两个不同面的一部分的相同顶点具有两个法线,两个UV坐标等。这可能是附加顶点的原因。
答案 1 :(得分: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);
}
}
小心,我不编译那些代码,只是在文本编辑器中编码。祝好运。