我的图形课程有任务要做。我必须仅使用GL_TRIANGLE_STRIP构建3/4。我已经完成了,但是有一个我无法接受的重大错误。
GLfloat radius = 3, alpha, beta, PI = 3.14314159;
//lHorizontal and lVertical im changing using keyboard it says how many
sphere divisions are
for (alpha = 0.0; alpha < PI; alpha += PI / lVertical)
{
glBegin(GL_TRIANGLE_STRIP);
for (beta = 0.0; beta < 1.5075*PI; beta += PI / lHorizontal)
{
glColor3f(1, 1, 0);
x = radius * cos(beta)*sin(alpha);
y = radius * sin(beta)*sin(alpha);
z = radius * cos(alpha);
glNormal3f(x, y, z);
glVertex3f(x, y, z);
glColor3f(0, 1, 1);
x = radius * cos(beta)*sin(alpha + PI / lVertical);
y = radius * sin(beta)*sin(alpha + PI / lVertical);
z = radius * cos(alpha + PI / lVertical);
glNormal3f(x, y, z);
glVertex3f(x, y, z);
}
glEnd();
}
glColor3f(1, 1, 0);
glBegin(GL_TRIANGLE_STRIP);
for (alpha = PI; alpha < 2.01*PI; alpha += PI / lVertical) {
x = -radius * sin(alpha);
y = 0;
z = radius * cos(alpha);
glNormal3f(x, y, z);
glVertex3f(x, y, z);
glNormal3f(0, 0, 0);
glVertex3f(0, 0, 0);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for (alpha = PI; alpha < 2.01*PI; alpha += PI / lVertical) {
x = 0;
y = radius * sin(alpha);
z = -radius * cos(alpha);
glNormal3f(x, y, z);
glVertex3f(x, y, z);
glNormal3f(0, 0, 0);
glVertex3f(0, 0, 0);
}
glEnd();
这段代码的结果是:
随着Im的变化,lHorizontal出现了不该出现的部分(请参见图片)。我可以在代码中进行哪些更改以避免出现此问题?
答案 0 :(得分:0)
PI的值是错误的:
..., PI = 3.14314159; // 3.14159... ?
更正此错误后,让我们在外循环中测试alpha
的{{1}}的值:
lVertical = 8
输出:
int lVertical = 8;
GLfloat radius = 3, alpha, beta, PI = 3.141592654;
for (alpha = 0.0; alpha < PI; alpha += PI / lVertical)
{
printf("%f, %f\n", alpha, alpha + PI / lVertical);
}
0.000000, 0.392699
0.392699, 0.785398
0.785398, 1.178097
1.178097, 1.570796
1.570796, 1.963495
1.963495, 2.356194
2.356194, 2.748893
2.748893, 3.141593
3.141593, 3.534292 <<
大于3.534292
,这意味着它是PI
的无效值。效果是最后一个多边形环“折叠”起来,产生了不应该存在的部分。
最好直接从整数索引计算角度:
alpha