glBegin
和glEnd
在循环之外的事实绝对没有问题。一个接一个地使用每个顶点绘制三角形是正确的方法。它会每隔 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.