您应该注意的第一件事(编辑:我稍微误读了您的代码。您对三角形风扇做得很好)是三角形风扇的工作原理是这样的:
glVertex: Center point
for each outer point p
glVertex: p
例如:
p2__
/| ---___p1
/ | /
/ | /
p3/ | /
|\ | /
| \ | /
| \_O Center
| __---
p4
第二件事是气缸由三部分组成:
__
/ \
\__/ <---- circle on top (facing up)
| |
| |
| |
| | <---- tube in the middle
| |
| |
| |
\__/ <---- circle on the bottom (facing down)
所以你需要的算法是:
/* top triangle */
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, height, 0); /* center */
for (i = 0; i <= 2 * PI; i += resolution)
glVertex3f(radius * cos(i), height, radius * sin(i));
glEnd();
/* bottom triangle: note: for is in reverse order */
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0, 0, 0); /* center */
for (i = 2 * PI; i >= 0; i -= resolution)
glVertex3f(radius * cos(i), 0, radius * sin(i));
/* close the loop back to 0 degrees */
glVertex3f(radius, height, 0);
glEnd();
/* middle tube */
glBegin(GL_QUAD_STRIP);
for (i = 0; i <= 2 * PI; i += resolution)
{
glVertex3f(radius * cos(i), 0, radius * sin(i));
glVertex3f(radius * cos(i), height, radius * sin(i));
}
/* close the loop back to zero degrees */
glVertex3f(radius, 0, 0);
glVertex3f(radius, height, 0);
glEnd();
您尝试做的方式首先是不正确的,因为您实际上并没有制作一个圆柱体,而是堆叠了许多圆圈,其次是效率低下,因为您正在填充几乎不可见的空间(圆柱体内部)。