我正在尝试使用ASSIMP从ply文件中提取顶点/法线/面部信息,然后使用OpenGL渲染它。以下是代码:
void PLYParser::importFile(std::string sFilePath)
{
const aiScene* scene = aiImportFile (sFilePath.c_str(), aiProcess_Triangulate); // TRIANGLES!
if (!scene)
{
std::cerr << "ERROR: reading mesh %s\n" << sFilePath << std::endl;
return;
}
printf (" %i animations\n", scene->mNumAnimations);
printf (" %i cameras\n", scene->mNumCameras);
printf (" %i lights\n", scene->mNumLights);
printf (" %i materials\n", scene->mNumMaterials);
printf (" %i meshes\n", scene->mNumMeshes);
printf (" %i textures\n", scene->mNumTextures);
for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++)
{
const aiMesh* mesh = scene->mMeshes[m_i];
printf (" %i vertices in mesh\n", mesh->mNumVertices);
g_point_count = mesh->mNumVertices;
for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++)
{
if (mesh->HasPositions ())
{
const aiVector3D* vp = &(mesh->mVertices[v_i]);
printf (" vp %i (%f,%f,%f)\n", v_i, vp->x, vp->y, vp->z);
}
if (mesh->HasNormals ())
{
const aiVector3D* vn = &(mesh->mNormals[v_i]);
printf (" vn %i (%f,%f,%f)\n", v_i, vn->x, vn->y, vn->z);
}
if (mesh->HasTextureCoords (0))
{
const aiVector3D* vt = &(mesh->mTextureCoords[0][v_i]);
printf (" vt %i (%f,%f)\n", v_i, vt->x, vt->y);
}
if (mesh->HasTangentsAndBitangents ())
{
// NB: could store/print tangents here
}
if( mesh->HasFaces() )
{
const struct aiFace * vf = &(mesh->mFaces[m_i]);
if (vf->mNumIndices == 3)
{
printf (" vf (%f,%f,%f)\n", vf->mIndices[0],vf->mIndices[1], vf->mIndices[2]);
}
}
}
}
std::cout << "Parsing over" << std::endl ;
}
顶点值打印正常,但面部相关信息是一种垃圾,类似这样的东西:
vp 5107 (0.003326,0.079200,0.001920)
vf (0.000000,-0.000000,26815622223676999148426976713858144581866377154979840994523259353566101584147672957460702289212360863380556296293016572911077072166495943380764853993996288.000000)
Q1。我在面部相关代码中做错了吗?
Q2。此外,我解析的任何ply文件中似乎都没有正常的相关信息。我必须手动计算一切的正常值。根本没有正常的信息吗?
P.S。 :我在几个不同的ply文件上尝试了代码,我仍然得到面部信息的垃圾值。
编辑1:
for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++)
{
const aiMesh* mesh = scene->mMeshes[m_i];
printf (" %i vertices in mesh\n", mesh->mNumVertices);
g_point_count = mesh->mNumVertices;
for (unsigned int v_i = 0; v_i < mesh->mNumFaces; v_i++)
{
if (mesh->HasFaces())
{
for (unsigned int f_i = 0; f_i < mesh->mNumFaces; f_i++)
{
const struct aiFace* vf = mesh->mFaces + f_i;
if (vf->mNumIndices == 3)
{
printf(" vf (%u,%u,%u)\n", vf->mIndices[0], vf->mIndices[1], vf->mIndices[2]);
}
}
}
}
}
通过将代码更改为上面 -
,将备用数字作为输出 vf (0,1,2)
vf (3,4,5)
vf (6,7,8)
vf (9,10,11)
vf (12,13,14)
vf (15,16,17)
..
..
and so on.
答案 0 :(得分:1)
<强> A1 强>
第一期:
printf (" vf (%f,%f,%f)\n", vf->mIndices[0], vf->mIndices[1], vf->mIndices[2]);
vf->mIndices
的类型为unsigned int*
,因此格式说明符%f
错误,应为%u
。试试这个:
printf (" vf (%u,%u,%u)\n", vf->mIndices[0], vf->mIndices[1], vf->mIndices[2]);
第二期:
您需要迭代网格的面列表,就像当前对顶点列表所做的那样。只需删除处理顶点循环中面的当前代码,并为面添加另一个循环(与顶点相同的级别),如下所示:
for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++)
{
// ...
}
if (mesh->HasFaces())
{
for (unsigned int f_i = 0; f_i < mesh->mNumFaces; f_i++)
{
const struct aiFace* vf = mesh->mFaces + f_i;
if (vf->mNumIndices == 3)
{
printf(" vf (%u,%u,%u)\n", vf->mIndices[0], vf->mIndices[1], vf->mIndices[2]);
}
}
}
<强> A2 强>
可以为PLY文件中的每个顶点指定正常信息(每个顶点元素3个float
属性,nx
,ny
和nz
。由于格式是灵活的,因此也可以以类似的方式指定每面正常。这完全取决于用于生成此文件的工具导出的内容。