1

我已经实现了将 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

4

2 回答 2

3

glBeginglEnd在循环之外的事实绝对没有问题。一个接一个地使用每个顶点绘制三角形是正确的方法。它会每隔 3 个连续的顶点构建一个三角形,这就是你想要的。

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 with glBegin/glEnd not working outside of the loop. But like said in the comment, you don't need the tLoop anyway, as you can just use x instead:

glBegin(GL_TRIANGLES);
for(int x = 1; x < 4*numberOfTriangles+1; x++)
    if(x % 4)   //works if x starts at 1, though I don't know why x has to start at 1
        glVertex3f(Vertices[Triangles[x]*3],Vertices[(Triangles[x]*3)+1],Vertices[(Triangles[x]*3)+2]);
glEnd();

or even better unroll the loop:

glBegin(GL_TRIANGLES);
for(int x = 1; x < 4*numberOfTriangles+1; x+=4) {
    glVertex3f(Vertices[Triangles[x]*3],Vertices[(Triangles[x]*3)+1],Vertices[(Triangles[x]*3)+2]);
    glVertex3f(Vertices[Triangles[x+1]*3],Vertices[(Triangles[x+1]*3)+1],Vertices[(Triangles[x+1]*3)+2]);
    glVertex3f(Vertices[Triangles[x+2]*3],Vertices[(Triangles[x+2]*3)+1],Vertices[(Triangles[x+2]*3)+2]);
}
glEnd();

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.

于 2011-10-25T15:15:48.743 回答
0

想通了我自己的问题。注意开始和结束块是如何出现在循环之外的。该程序试图使用网格中的每个顶点一个接一个地绘制三角形。因此,它有点造成了巨大的混乱。我改为一次放置三个顶点(一个面),并将开始/结束块放在循环内,并将循环更改为每次增加 4,从而允许我跳过面数据。

以防万一有人好奇。

于 2011-10-24T23:32:33.603 回答