When I use this code sample I get a flat white sphere.

I'm expecting a sphere that is lit from the side and rotating.

#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;
}

Why is this occurring?
enter image description here

有帮助吗?

解决方案

The problem was the placement of my window creation. None of my glEnable calls kicked in because they were placed before my window creation. To fix the issue, I simply moved this line:

glutCreateWindow("Embera 2.0.1");

up so the main function looked like so:

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top