/* lighting.c */ /* Purpose: demonstrate lighting */ #ifdef __APPLE__ #include #else #include #endif #include #define KEY_ESC 27 /* glut doesn't define this one */ /* Functions */ GLvoid display( GLvoid ) { glClear( GL_COLOR_BUFFER_BIT ); /* Note that glut defines the normals for its 3D objects */ glutSolidTorus( 0.3, 0.6, 50, 50 ); glFlush(); } GLvoid keyboard( GLubyte key, GLint x, GLint y) { switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); break; case 'l': case 'L': { static GLboolean lightOn = GL_FALSE; /* Toggle the light on/off */ lightOn = !lightOn; /* Turn on/off the OpenGL lighting calculations */ lightOn ? glEnable( GL_LIGHTING ) : glDisable( GL_LIGHTING ); glutPostRedisplay(); } break; } } void init( void ) { /* What colour is the ambient light */ GLfloat pAmbientLightModel[] = { 0.3, 0.3, 0.3, 1.0 }; /* Where is the infinite directional light */ GLfloat pLight0Pos[] = { 10.0, 10.0, 2.0 , 0.0 }; /* What are the ambient, diffuse, and specular material properties for our torus */ GLfloat pMatAmbDiff[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat pMatSpecular[] = { 1.0, 1.0, 1.0, 1.0 }; /* Get some ambient light */ glLightModelfv( GL_LIGHT_MODEL_AMBIENT, pAmbientLightModel ); /* Get an infinite, but directional light (like a sun) */ glEnable( GL_LIGHT0 ); glLightfv( GL_LIGHT0, GL_POSITION, pLight0Pos ); /* Set up a material for our torus */ glMaterialfv( GL_FRONT, GL_SPECULAR, pMatSpecular ); glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pMatAmbDiff ); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutCreateWindow( argv[0] ); glutDisplayFunc( display ); glutKeyboardFunc( keyboard ); init(); glutMainLoop(); return 0; }