#include #include int angle=0; float color = 0.0; void MouseCallback(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { if (color == 0.0) color = 0.5; else color = 0.0; } glutPostRedisplay(); } void KeyboardCallback(unsigned char c, int x, int y) { switch (c) { case 'r': angle += 10; angle = angle % 360; break; case 'q': exit (0); break; } glutPostRedisplay(); } void DisplayCallback() { // clear the color buffer glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( -2.0, 2.0, -2.0, 2.0, -2.0, 2.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glRotatef( 30, 1.0, 0.0, 0.0 ); glRotatef( angle, 0.0, 1.0, 0.0 ); float r = (sqrt(5)+1)/2; static GLfloat vertices[20][3] = { {1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0}, {1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, 1.0}, {1.0, -1.0, -1.0}, {-1.0, -1.0, -1.0}, {1/r, 0.0, r}, {-1/r, 0.0, r}, {1/r, 0.0, -r}, {-1/r, 0.0, -r}, {r, 1/r, 0.0}, {-r, 1/r, 0.0}, {r, -1/r, 0.0}, {-r, -1/r, 0.0}, {0.0, r, 1/r}, {0.0, -r, 1/r}, {0.0, r, -1/r}, {0.0, -r, -1/r} }; glColor3f( 1.0-color, 0.0, 0.0 ); glBegin( GL_POLYGON ); glVertex3fv(vertices[0]); glVertex3fv(vertices[16]); glVertex3fv(vertices[1]); glVertex3fv(vertices[9]); glVertex3fv(vertices[8]); glEnd(); glColor3f( 0.0, 0.5+color, 0.0+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[4]); glVertex3fv(vertices[8]); glVertex3fv(vertices[9]); glVertex3fv(vertices[5]); glVertex3fv(vertices[17]); glEnd(); glColor3f( 1.0-color, 1.0-color, 0.0 ); glBegin( GL_POLYGON ); glVertex3fv(vertices[0]); glVertex3fv(vertices[8]); glVertex3fv(vertices[4]); glVertex3fv(vertices[14]); glVertex3fv(vertices[12]); glEnd(); glColor3f( 0.0, 0.0, 1.0-color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[1]); glVertex3fv(vertices[13]); glVertex3fv(vertices[15]); glVertex3fv(vertices[5]); glVertex3fv(vertices[9]); glEnd(); glColor3f( 1.0-color, 0.0, 1.0-color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[18]); glVertex3fv(vertices[2]); glVertex3fv(vertices[10]); glVertex3fv(vertices[11]); glVertex3fv(vertices[3]); glEnd(); glColor3f( 1.0-color, 1.0-color, 1.0-color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[19]); glVertex3fv(vertices[7]); glVertex3fv(vertices[11]); glVertex3fv(vertices[10]); glVertex3fv(vertices[6]); glEnd(); glColor3f( 0.5+color, 0.0, 0.0+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[2]); glVertex3fv(vertices[12]); glVertex3fv(vertices[14]); glVertex3fv(vertices[6]); glVertex3fv(vertices[10]); glEnd(); glColor3f( 0.0, 1.0-color, 0.0 ); glBegin( GL_POLYGON ); glVertex3fv(vertices[3]); glVertex3fv(vertices[11]); glVertex3fv(vertices[7]); glVertex3fv(vertices[15]); glVertex3fv(vertices[13]); glEnd(); glColor3f( 0.0+color, 0.0, 0.5+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[0]); glVertex3fv(vertices[12]); glVertex3fv(vertices[2]); glVertex3fv(vertices[18]); glVertex3fv(vertices[16]); glEnd(); glColor3f( 0.5+color, 0.5+color, 0.0+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[4]); glVertex3fv(vertices[17]); glVertex3fv(vertices[19]); glVertex3fv(vertices[6]); glVertex3fv(vertices[14]); glEnd(); glColor3f( 0.5+color, 0.0+color, 0.5+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[1]); glVertex3fv(vertices[16]); glVertex3fv(vertices[18]); glVertex3fv(vertices[3]); glVertex3fv(vertices[13]); glEnd(); glColor3f( 0.5+color, 0.5+color, 0.5+color ); glBegin( GL_POLYGON ); glVertex3fv(vertices[5]); glVertex3fv(vertices[15]); glVertex3fv(vertices[7]); glVertex3fv(vertices[19]); glVertex3fv(vertices[17]); glEnd(); // draw the buffer to the screen glutSwapBuffers(); } int main(int argc, char **argv) { // create window and rendering context glutInit( &argc, argv ); glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE ); glutInitWindowSize( 500, 500 ); glutCreateWindow( "openGLDemo" ); // register display callback glutDisplayFunc( DisplayCallback ); glutKeyboardFunc( KeyboardCallback ); glutMouseFunc( MouseCallback ); glViewport( 0, 0, 500, 500 ); glEnable( GL_DEPTH_TEST ); // pass control over to GLUT glutMainLoop(); return 0; // never reached }