glutSolidSphere没有被点燃

时间:2013-11-24 00:26:55

标签: c++ opengl freeglut

当我使用此代码示例时,我得到一个扁平的白色球体。

我期待一个从侧面照亮并旋转的球体。

#include <GL/glut.h>
#include <GL/freeglut.h>

int angle = 0;
int _refreshmilliseconds = (1 / 60) * 1000; // 60 frames a second

void timer(int value) {
        glutPostRedisplay();
        glutTimerFunc(_refreshmilliseconds, timer, 0);
}

void display(void) {
        /*
        OPEN GL AND GRAPHICS FUNCTIONALITY
        */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        // Lighting
        // Ambient light
        GLfloat ambientCol[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientCol);

        // Positioned light
        GLfloat lightCol0[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        GLfloat lightPos0[] = { 0.5f, 0.0f, 1.0f, 1.0f };

        glLightfv(GL_LIGHT0, GL_DIFFUSE, lightCol0);
        glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

        // Keyboard-controlled circle, player1
        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);
        glutSolidSphere(0.5f, 20, 20);
        glPopMatrix();

        angle += 2;
    if (angle >= 360) { angle = 0; }

        glutSwapBuffers();
}

int main(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);

        glutInitWindowSize(400, 400);                                           // Set window size
        glutInitWindowPosition(100, 100);                                       // Set window position
        glutCreateWindow("Embera 2.0.1");                                       // Create GLUT window

        glutDisplayFunc(display);
        glutTimerFunc(0, timer, 0);

        glutMainLoop();

        return 0;
}

为什么会发生这种情况? enter image description here

1 个答案:

答案 0 :(得分:1)

问题在于我的窗口创建的位置。我的glEnable个电话都没有被踢,因为它们是在我创建窗口之前放置的。为了解决这个问题,我简单地移动了这一行:

glutCreateWindow("Embera 2.0.1");

up所以主要功能看起来像这样:

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow("Embera 2.0.1");

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);

    glutInitWindowSize(400, 400);
    glutInitWindowPosition(100, 100);


    glutDisplayFunc(display);
    glutTimerFunc(0, timer, 0);

    glutMainLoop();

    return 0;