/* * Animated Cube with 3 planes meeting in a corner. * compile with g++-4.4 -o cube cube.cc -lGL -lGLU -lglut */ #include #include #include #include int angle = -10; int step = 1; double axis[3] = {1.0, 0.0, 0.0}; void display () { int i; GLfloat c[3] = {0,1,0}; float l = 1; float d = l*sqrt(2); /* clear window */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); /* a white cube */ glPushMatrix(); glRotatef(angle,1,0.8,0); glColor4f(1,1,1,1); glutWireCube(2*l); /* three diagonal planes */ glPushMatrix(); glRotatef(45,1,0,0); for (i = 0; i < 3; i++) { glColor4f(c[i],c[(i+1)%3],c[(i+2)%3],.6); glRectf(-l, d, l, -d); glRotatef(120,l,d,0); } glPopMatrix(); glPopMatrix(); /* flush drawing routines to the window */ glFlush(); glutSwapBuffers(); } void reshape ( int width, int height ) { /* define the viewport transformation */ glViewport(0, 0, width, height); } void animate () { angle += step; if (abs(angle) == 30 ) { step *= -1; } sleep(1); glutPostRedisplay(); } int main ( int argc, char ** argv ) { /* initialize GLUT, using any commandline parameters passed to the program */ glutInit(&argc,argv); /* setup the size, position, and display mode for new windows */ glutInitWindowSize(400, 400); glutInitWindowPosition(0, 0); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); /* create and set up a window */ glutCreateWindow("D6 at cube corner"); glutDisplayFunc(display); /* register function that draws the objects */ glutReshapeFunc(reshape); /* register function handling resizing */ glutIdleFunc(animate); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); /* define the projection transformation */ glMatrixMode(GL_PROJECTION); /* choose projection matrix stack */ glLoadIdentity(); /* set it to the identity matrix */ gluPerspective(60,1,0.1, 6.0); /* an orthogonal parallel projection * left, right, bottom, top, zNear, * and zFar */ /* define the viewing transformation */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(3.0, 1.6, 2.0, /* eye*/ 0.0, 0.0, 0.0, /* point to look at */ 0.0, 1.0, 0.0); /* up direction */ /* tell GLUT to wait for events */ glutMainLoop(); }