如何正确渲染 3ds 文件中的面部
我已经实现了将 3DS 文件加载到我的 OpenGL 程序中的功能,但遇到了一个小问题。所有顶点均已正确放置,并且面已绘制,但问题是大多数(或全部)顶点似乎保留与一两个顶点的连接,从而创建大量额外的边。有人以前遇到过这个问题或者对如何解决它有建议吗?
以下代码块是我用来绘制脸部的循环。它一次循环一个顶点,跳过每四个值(理论上),因为它们是未使用的面修改器。
glBegin(GL_TRIANGLES);
for(int x = 1; x < 4*numberOfTriangles+1; x++)
{
//Face bit modifiers not needed, skip em.
if(tLoop == 4)
{
tLoop = 0;
continue;
}
else
{
glVertex3f(Vertices[Triangles[x]*3],Vertices[(Triangles[x]*3)+1],Vertices[(Triangles[x]*3)+2]);
tLoop++;
}
}
glEnd();
接下来是代表我遇到的问题的图像。 http://img.photobucket.com/albums/v298/Reaperc89/Pistol.jpg
I have implemented the ability to load 3DS files into an OpenGL program of mine, and run into a slight problem. All of the vertices are placed properly, and the faces are drawn, but the issue is that most(or all) of the vertices seem to retain a connection to one or two vertices, creating a large number of extra edges. Anyone run into this issue before or have a suggestion on how I can fix it?
The following block of code is the loop I use to draw the faces. It loops through one vertex at a time, skipping every fourth value (in theory) as they are unused face modifiers.
glBegin(GL_TRIANGLES);
for(int x = 1; x < 4*numberOfTriangles+1; x++)
{
//Face bit modifiers not needed, skip em.
if(tLoop == 4)
{
tLoop = 0;
continue;
}
else
{
glVertex3f(Vertices[Triangles[x]*3],Vertices[(Triangles[x]*3)+1],Vertices[(Triangles[x]*3)+2]);
tLoop++;
}
}
glEnd();
This next is an image representing the problem I am having.
http://img.photobucket.com/albums/v298/Reaperc89/Pistol.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
glBegin
和glEnd
在循环之外的事实绝对没有问题。使用每个顶点一个接一个地绘制三角形是正确的方法。它将每 3 个连续的顶点构建一个三角形,这就是您想要的。您的问题是,您在 else 块内增加了 tLoop ,因此实际上跳过了每五个索引,而不是每四个索引。因此展开阻止了它,但它与
glBegin/glEnd
不在循环之外工作无关。但就像评论中所说,无论如何你都不需要 tLoop,因为你可以使用 x 来代替:或者更好地展开循环:
但是放置
循环内的 >glBegin/glEnd
是你能做的最愚蠢的事情。事实上,如果您已经使用基于顶点/索引数组的表示,那么将渲染代码移植到顶点数组应该非常容易,这比立即模式快得多,在由 VBO 提供支持时更是如此。The fact that
glBegin
andglEnd
are outside the loop is absolutely no problem. Drawing triangles using every vertex one after the other is just the correct way. It will build a triangle form every 3 consecutive vertices, which is what you want.Your problem was, that you increased
tLoop
inside the else block, and therefore actually skipped every fifth index, instead of every fourth. So unrolling prevented it, but it has nothing to do withglBegin/glEnd
not working outside of the loop. But like said in the comment, you don't need thetLoop
anyway, as you can just usex
instead:or even better unroll the loop:
But placing the
glBegin/glEnd
inside the loop is the silliest thing you can do. In fact if you already use a vertex/index array based representation, it should be quite easy to port your rendering code to vertex arrays, which are much faster than immediate mode, more so when powered by VBOs.找出了我自己的问题。请注意开始和结束块如何出现在循环之外。该程序试图使用网格中的每个顶点一个接一个地绘制三角形。因此它造成了一个巨大的混乱。我更改为一次放置三个顶点(一个面),并将开始/结束块放置在循环内,并将循环更改为每次递增 4,从而允许我跳过面数据。
以防万一有人好奇。
Figured out my own problem. Notice how the begin and end blocks appear outside of the loop. The program was trying to draw triangles using every vertex in the mesh, one after another. Thus it kinda created one giant mess. I changed to placing three vertices at a time(one face), and I placed the begin/end blocks inside the loop, and changed the loop to increment by 4 each time, allowing me to skip the face data.
Just in case anyone was curious.