OpenGL自定义四边形有灯光错误

时间:2016-09-12 15:56:38

标签: c++ opengl

我试图找出如何编写自己的原语,如gluSolidCube()..

在下面的代码中,我绘制了2个四边形。一个借助于这个方法(红色的)和另一个 - 由我自己在glBegin()/ glEnd()(蓝色的)的帮助下。

glPushMatrix();
    glRotatef(-angleX, 0, 0, 1);
    glRotatef(angleY, 0, 1, 0);

    glColor3f(1, 0, 0);
    glTranslatef(0.6, 0, 0);
    glutSolidCube(1);

    glColor3f(0, 0, 1);
    glTranslatef(-1.2, 0, 0);
    glPolygonMode(GL_FRONT, GL_FILL);
    glShadeModel(GL_SMOOTH);
    glBegin(GL_QUADS);
        glVertex3f(0.5, 0.5, 0.5);
        glVertex3f(0.5, 0.5, -0.5);
        glVertex3f(-0.5, 0.5, -0.5);
        glVertex3f(-0.5, 0.5, 0.5);

        glVertex3f(0.5, 0.5, 0.5);
        glVertex3f(0.5, 0.5, -0.5);
        glVertex3f(0.5, -0.5, -0.5);
        glVertex3f(0.5, -0.5, 0.5);

        glVertex3f(-0.5, 0.5, -0.5);
        glVertex3f(-0.5, 0.5, 0.5);
        glVertex3f(-0.5, -0.5, 0.5);
        glVertex3f(-0.5, -0.5, -0.5);

        glVertex3f(0.5, 0.5, -0.5);
        glVertex3f(-0.5, 0.5, -0.5);
        glVertex3f(-0.5, -0.5, -0.5);
        glVertex3f(0.5, -0.5, -0.5);

        glVertex3f(0.5, 0.5, 0.5);
        glVertex3f(-0.5, 0.5, 0.5);
        glVertex3f(-0.5, -0.5, 0.5);
        glVertex3f(0.5, -0.5, 0.5);

        glVertex3f(0.5, -0.5, 0.5);
        glVertex3f(0.5, -0.5, -0.5);
        glVertex3f(-0.5, -0.5, -0.5);
        glVertex3f(-0.5, -0.5, 0.5);
    glEnd();
glPopMatrix();

因此,您可以在下面的屏幕截图中看到,红色四边形上的闪光与蓝色闪光灯不同。怎么解决这个问题?

screen 1

screen 2

1 个答案:

答案 0 :(得分:1)

如果没有sensible vertex/face normals,OpenGL固定功能照明将无法正常工作。

您需要提供some,可能是glNormal()