2013-11-24 45 views
2

當我使用此代碼示例時,我得到一個平坦的白色球體。glutSolidSphere未被點亮

我期待着從側面照亮並旋轉的球體。

#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

問題是我的窗口創建的位置。我的glEnable電話中沒有一個是因爲它們被放置在我的窗口創建之前而被踢入的。要解決這個問題,我乾脆搬到這條線:

glutCreateWindow("Embera 2.0.1");

了,因此主要功能看起來像這樣:

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;