/******************************************************************** MyCube - based upon Angel's cube.c program Edited by H. Masterman to illustrate different aspects of transformations. Includes idle function to animate the cube. Substantially simplified from Angel's example A9. rev. 0 9 October 2000 rev. 1 Updated 26 February 2001 to clean up unnecessary code ********************************************************************/ #include #include GLfloat vertices[][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}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; void polygon(int a, int b, int c , int d) { /* draw a polygon via list of vertices */ glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glColor3fv(colors[b]); glVertex3fv(vertices[b]); glColor3fv(colors[c]); glVertex3fv(vertices[c]); glColor3fv(colors[d]); glVertex3fv(vertices[d]); glEnd(); } void colorcube(void) { /* map vertices to faces */ polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } static GLfloat theta1 = 0.0, theta2 = 0.0; //stores two angles void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear color buffer and z buffer glLoadIdentity(); // start with an identity in the MODELVIEW transformation //gluLookAt(0.0,3.0,10.0, 0.0,0.0,0.0,0,1,0); //multiply viewing component of transformation glTranslatef(0,0,-5); //Next we multiply on the transformations before drawing the cube glRotatef(theta1, 1,0,0); //glTranslatef(2,0,0); //glRotatef(theta1, 1,0,0); //glScalef(1.0, 0.2,0.2); //glTranslatef(2,2,0); //glRotatef(theta1, 0,1,0); colorcube(); // draw the color cube /* glRotatef(theta2,1,0,0); glTranslatef(2, 0,0); glScalef(0.5,0.5,0.5); colorcube(); */ glFlush(); glutSwapBuffers(); } void spinCube() { //idle callback. Angles are incremented by varying amounts each time it is called. theta1 += 0.9; // slow rotation if( theta1 > 360.0 ) theta1 -= 360.0; theta2 += 3.0; // fast rotation if (theta2 > 360.0) theta2 -= 360.0; glutPostRedisplay(); // generate a display event } void mouse(int btn, int state, int x, int y) { exit(0); //exit the program on any mouse click } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluPerspective(60.0, 1.0,1.0,30.0); //glOrtho(-4,4,-4,4,-4,4); glFrustum(-1,1,-1,1,1,30); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); /* need both double buffering and z buffer */ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH); glutInitWindowSize(700, 700); glutCreateWindow("My Rotating Cube Example"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glutMainLoop(); }