GLUT GL 3/4 球体有问题

Posted

技术标签:

【中文标题】GLUT GL 3/4 球体有问题【英文标题】:GLUT GL 3/4 sphere something wrong 【发布时间】:2019-01-16 17:44:05 【问题描述】:

我的图形课有任务要做。我必须只使用 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();

这段代码的结果是:

当我改变 lHorizo​​ntal 时出现不应该存在的部分(看图片)。我可以在代码中进行哪些更改以避免此问题?

【问题讨论】:

【参考方案1】:

PI的值是错误的:

..., PI = 3.14314159; // 3.14159... ?

纠正这个问题后,让我们在外循环中测试alpha 的值,对于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 的无效值。效果是最后一个多边形环“折叠”,产生不应该存在的部分。

最好直接从整数索引计算角度:

for (int i = 0; int i < lVertical; i++)

    GLfloat alpha1 = (PI / lVertical) * i;
    GLfloat alpha2 = (PI / lVertical) * (i + 1);

    glBegin(GL_TRIANGLE_STRIP);

    for (int j = 0; j < lHorizontal; j++)
    
        GLfloat beta1 = (PI / lHorizontal) * j;
        GLfloat beta2 = (PI / lHorizontal) * (j + 1);

        glColor3f(1, 1, 0);
        x = radius * cos(beta1)*sin(alpha1);
        y = radius * sin(beta1)*sin(alpha1);
        z = radius * cos(alpha1);

        glNormal3f(x, y, z);
        glVertex3f(x, y, z);

        glColor3f(0, 1, 1);
        x = radius * cos(beta2)*sin(alpha2);
        y = radius * sin(beta2)*sin(alpha2);
        z = radius * cos(alpha2);

        glNormal3f(x, y, z);
        glVertex3f(x, y, z);
    

    glEnd();

【讨论】:

@PawełKierski 定义“不起作用”。可以发张结果图片吗?

以上是关于GLUT GL 3/4 球体有问题的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL/C++ 3D 球体

glutSolidSphere 未点亮

使用 opengl 在球体上渲染图像

如何在曲线上为实体球体设置动画

如何在 3D 对象(如球体)上绘制文本

JOGL:在指定位置绘制球体会导致问题