/* material.c */ /* Purpose: demonstrate lighting */ #ifdef __APPLE__ #include #else #include #endif #include #define KEY_ESC 27 /* glut doesn't define this one */ /* Module level variables */ GLfloat mAngle = 0; /* Module level constants */ /* The following are material definitions for the body. Ambiance, Diffuse, Specular, Shininess */ static float mMaterials[] = { /* Totally weird material */ 1.0, 0.0, 0.0, 1.000000, 0.0, 1.0, 0.0, 1.000000, 0.0, 0.0, 1.0, 1.000000, 10.00000, 0.0, 0.0, 0.0, /* Brass */ 0.329412, 0.223529, 0.027451, 1.000000, 0.780392, 0.568627, 0.113725, 1.000000, 0.992157, 0.941176, 0.807843, 1.000000, 27.897400, 0.0 , 0.0 , 0.0, /* Bronze */ 0.212500, 0.127500, 0.054000, 1.000000, 0.714000, 0.428400, 0.181440, 1.000000, 0.393548, 0.271906, 0.166721, 1.000000, 25.600000, 0.0 , 0.0 , 0.0, /* Polished_Bronze */ 0.250000, 0.148000, 0.064750, 1.000000, 0.400000, 0.236800, 0.103600, 1.000000, 0.774597, 0.458561, 0.200621, 1.000000, 76.800003, 0.0 , 0.0 , 0.0, /* Chrome */ 0.250000, 0.250000, 0.250000, 1.000000, 0.400000, 0.400000, 0.400000, 1.000000, 0.774597, 0.774597, 0.774597, 1.000000, 76.800003, 0.0 , 0.0 , 0.0, /* Copper */ 0.191250, 0.073500, 0.022500, 1.000000, 0.703800, 0.270480, 0.082800, 1.000000, 0.256777, 0.137622, 0.086014, 1.000000, 12.800000, 0.0 , 0.0 , 0.0, /* Polished_Copper */ 0.229500, 0.088250, 0.027500, 1.000000, 0.550800, 0.211800, 0.066000, 1.000000, 0.580594, 0.223257, 0.069570, 1.000000, 51.200001, 0.0 , 0.0 , 0.0, /* Gold */ 0.247250, 0.199500, 0.074500, 1.000000, 0.751640, 0.606480, 0.226480, 1.000000, 0.628281, 0.555802, 0.366065, 1.000000, 51.200001, 0.0 , 0.0 , 0.0, /* Polished_Gold */ 0.247250, 0.224500, 0.064500, 1.000000, 0.346150, 0.314300, 0.090300, 1.000000, 0.797357, 0.723991, 0.208006, 1.000000, 83.199997, 0.0 , 0.0 , 0.0, /* Pewter */ 0.105882, 0.058824, 0.113725, 1.000000, 0.427451, 0.470588, 0.541176, 1.000000, 0.333333, 0.333333, 0.521569, 1.000000, 9.846150, 0.0 , 0.0 , 0.0, /* Silver */ 0.192250, 0.192250, 0.192250, 1.000000, 0.507540, 0.507540, 0.507540, 1.000000, 0.508273, 0.508273, 0.508273, 1.000000, 51.200001, 0.0 , 0.0 , 0.0, /* Polished_Silver */ 0.231250, 0.231250, 0.231250, 1.000000, 0.277500, 0.277500, 0.277500, 1.000000, 0.773911, 0.773911, 0.773911, 1.000000, 89.599998, 0.0 , 0.0 , 0.0, /* Emerald */ 0.021500, 0.174500, 0.021500, 0.550000, 0.075680, 0.614240, 0.075680, 0.550000, 0.633000, 0.727811, 0.633000, 0.550000, 76.800003, 0.0 , 0.0 , 0.0, /* Jade */ 0.135000, 0.222500, 0.157500, 0.950000, 0.540000, 0.890000, 0.630000, 0.950000, 0.316228, 0.316228, 0.316228, 0.950000, 12.800000, 0.0 , 0.0 , 0.0, /* Obsidian */ 0.053750, 0.050000, 0.066250, 0.820000, 0.182750, 0.170000, 0.225250, 0.820000, 0.332741, 0.328634, 0.346435, 0.820000, 38.400002, 0.0 , 0.0 , 0.0, /* Pearl */ 0.250000, 0.207250, 0.207250, 0.922000, 1.000000, 0.829000, 0.829000, 0.922000, 0.296648, 0.296648, 0.296648, 0.922000, 11.264000, 0.0 , 0.0 , 0.0, /* Ruby */ 0.174500, 0.011750, 0.011750, 0.550000, 0.614240, 0.041360, 0.041360, 0.550000, 0.727811, 0.626959, 0.626959, 0.550000, 76.800003, 0.0 , 0.0 , 0.0, /* Turquoise */ 0.100000, 0.187250, 0.174500, 0.800000, 0.396000, 0.741510, 0.691020, 0.800000, 0.297254, 0.308290, 0.306678, 0.800000, 12.800000, 0.0 , 0.0 , 0.0, /* Black_Plastic */ 0.000000, 0.000000, 0.000000, 1.000000, 0.010000, 0.010000, 0.010000, 1.000000, 0.500000, 0.500000, 0.500000, 1.000000, 32.000000, 0.0 , 0.0 , 0.0, /* Black_Rubber */ 0.020000, 0.020000, 0.020000, 1.000000, 0.010000, 0.010000, 0.010000, 1.000000, 0.400000, 0.400000, 0.400000, 1.000000, 10.000000, 0.0 , 0.0 , 0.0 }; GLvoid display( GLvoid ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glRotatef( mAngle, 1, 1, 0 ); /* Note that glut defines the normals for its 3D objects */ glutSolidTorus( 0.3, 0.6, 200, 200 ); glutSwapBuffers(); } GLvoid keyboard( GLubyte key, GLint x, GLint y) { static GLuint currentMatIndex = 0; switch (key) { case KEY_ESC: /* exit when escape key is pressed */ exit(0); break; case 'l': case 'L': { static GLboolean lightOn = GL_FALSE; lightOn = !lightOn; /* Turn on/off the OpenGL lighting calculations */ lightOn ? glEnable( GL_LIGHTING ) : glDisable( GL_LIGHTING ); } break; case ' ': /* Set up a material for our torus */ currentMatIndex += 16; glMaterialfv( GL_FRONT, GL_AMBIENT, &(mMaterials[0 + currentMatIndex]) ); glMaterialfv( GL_FRONT, GL_DIFFUSE, &(mMaterials[4 + currentMatIndex]) ); glMaterialfv( GL_FRONT, GL_SPECULAR, &(mMaterials[8 + currentMatIndex]) ); glMaterialf( GL_FRONT, GL_SHININESS, mMaterials[12 + currentMatIndex] ); /* and set a colur for the non lit mode */ glColor4fv( &(mMaterials[4 + currentMatIndex]) ); break; } glutPostRedisplay(); } GLvoid specialkeys( GLint key, GLint x, GLint y) { switch( key ) { case GLUT_KEY_RIGHT: mAngle += 2.0; break; case GLUT_KEY_LEFT: mAngle -= 2.0; break; } glutPostRedisplay(); } void init( void ) { /* What colour is the ambient light */ GLfloat pAmbientLightModel[] = { 0.2, 0.2, 0.2, 1.0 }; /*GLfloat pAmbientLightModel[] = { 0.0, 0.0, 0.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_AMBIENT, pAmbientLightModel ); /* Set up a material for our torus */ glMaterialfv( GL_FRONT, GL_AMBIENT, &(mMaterials[0]) ); glMaterialfv( GL_FRONT, GL_DIFFUSE, &(mMaterials[4]) ); glMaterialfv( GL_FRONT, GL_SPECULAR, &(mMaterials[8]) ); glMaterialf( GL_FRONT, GL_SHININESS, mMaterials[12] ); /* and set a colour for the non lit mode */ glColor4fv( &( mMaterials[4] ) ); /*glEnable( GL_CULL_FACE );*/ /*glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );*/ glEnable( GL_DEPTH_TEST ); } void reshape(GLint width, GLint height) { /* We still want the output to cover the whole window */ glViewport(0, 0, width, height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(-1, 1, -1, 1, -1, 1); glMatrixMode( GL_MODELVIEW ); } int main( int argc, char *argv[] ) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA ); glutCreateWindow( argv[0] ); glutDisplayFunc( display ); glutKeyboardFunc( keyboard ); glutSpecialFunc( specialkeys ); glutReshapeFunc( reshape ); init(); glutMainLoop(); return 0; }