不正确的顶点&使用 ASSIMP 和 3D 模型加载中的正常访问OpenGL
我使用 ASSIMP 的 示例代码中提供的纹理 3D 模型加载示例< /a>.但是,对于某些模型,它似乎无法正确访问模型的顶点和法线。这是错误加载的模型的屏幕截图:
而模型应类似于以下屏幕截图:
在第一张图像中,建筑物的屋顶位于前视图的中心顶部。地板不见了。我认为这个问题是由于错误访问顶点位置和法线(CMIIW)引起的。以下是用于访问顶点位置和法线的代码片段:
glBegin(face_mode);
for(i = 0; i < face->mNumIndices; i++){
int vertexIndex = face->mIndices[i]; // get group index for current index
if(mesh->mColors[0] != NULL)
Color4f(&mesh->mColors[0][vertexIndex]);
if(mesh->mNormals != NULL)
if(mesh->HasTextureCoords(0)){
glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, mesh->mTextureCoords[0][vertexIndex].y);
}
glNormal3fv(&mesh->mNormals[vertexIndex].x);
glVertex3fv(&mesh->mVertices[vertexIndex].x);
}
glEnd();
如何正确访问模型的顶点位置和法线?
I use textured 3d model loading sample provided in ASSIMP's sample code. However, for some models, it doesn't seem to access the model's vertex and normal correctly. Here is a screenshot of the model loaded incorrectly:
While the model should be like the following screenshot:
In the first image, the roof of the building placed at the center-top of the front view. The floor is missing. I assume that this problem is caused by incorrect access to vertex positions and normals (CMIIW). The following is the snippet used to access both vertex position and normal:
glBegin(face_mode);
for(i = 0; i < face->mNumIndices; i++){
int vertexIndex = face->mIndices[i]; // get group index for current index
if(mesh->mColors[0] != NULL)
Color4f(&mesh->mColors[0][vertexIndex]);
if(mesh->mNormals != NULL)
if(mesh->HasTextureCoords(0)){
glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, mesh->mTextureCoords[0][vertexIndex].y);
}
glNormal3fv(&mesh->mNormals[vertexIndex].x);
glVertex3fv(&mesh->mVertices[vertexIndex].x);
}
glEnd();
How we can access the model's vertex positions and normals correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生的情况是,具有相同位置的顶点是“共享的”。这是有问题的,因为(用 OpenGL 术语来说),顶点是[位置、法线、纹理坐标、其他属性……]的整体组合。
在导出器或加载器的某个地方,此信息丢失了。
您可以在 3D 建模器中修复此问题,方法是在硬边缘处分割网格,即选择平滑照明表面的连续补丁并将它们转换为单独的子网格(例如,在 Blender 中通过“分割网格”功能,热键“Y”)。
What happens there is, that vertices with the same position are "shared". This is problematic, because (in OpenGL terms), a vertex is the whole combination of [position, normal, texture coordinates, other attributes…].
Somewhere in the exporter or loader this information is lost.
You can fix this in the 3D modeler, by splitting the mesh at hard edges, i.e. select contiguous patches of smoothly lit surfaces and turn them into individual submeshes (e.g. in Blender by the "Split Mesh" function, Hotkey 'Y').