 
/*
x - Rotate ROT_ANGLE degrees about the x-coordinate.
X - Rotate -ROT_ANGLE degrees about the x-coordinate.
and similarly for y, Y, z and Z.

use 'q' or 'Q' to quit.
*/

#include <GL/glut.h>
#include <stdlib.h>
#define ROT_ANGLE 2 

void myInit () {
	 
	 glClearColor (1.0, 1.0, 1.0, 1.0);
	 glEnable (GL_DEPTH_TEST);
	 
	 glMatrixMode (GL_PROJECTION);
	 glLoadIdentity();
	 glOrtho (-10, 10, -10, 10, -10, 10);
	 glMatrixMode (GL_MODELVIEW);
}
 
void myRotate (unsigned char axis) {
	 
	switch (axis) {
		case 'x':
			glRotatef (ROT_ANGLE, 1.0, 0.0, 0.0);
			break;
		case 'y':
			glRotatef (ROT_ANGLE, 0.0, 1.0, 0.0);
			break;
		case 'z':
			glRotatef (ROT_ANGLE, 0.0, 0.0, 1.0);
			break;
		case 'X':
			glRotatef (-ROT_ANGLE, 1.0, 0.0, 0.0);
			break;
		case 'Y':
			glRotatef (-ROT_ANGLE, 0.0, 1.0, 0.0);
			break;
		case 'Z':
			glRotatef (-ROT_ANGLE, 0.0, 0.0, 1.0);
			break;
	}
	
}
 
void display () {
	 
	 GLfloat cube[8][3] = { {5.0, 5.0, -5.0}, {5.0, 5.0, 5.0}, {-5.0, 5.0, -5.0}, {-5.0, 5.0, 5.0},
	 {-5.0, -5.0, -5.0}, {-5.0, -5.0, 5.0}, {5.0, -5.0, -5.0}, {5.0, -5.0, 5.0} };
	 
	 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	 glBegin (GL_QUADS);
	 glColor3f (1.0, 0.0, 0.0);
	 glVertex3fv (cube[1]);
	 glVertex3fv (cube[3]);
	 glVertex3fv (cube[5]);
	 glVertex3fv (cube[7]);
	 
	 glColor3f (0.0, 1.0, 0.0);
	 glVertex3fv (cube[2]);
	 glVertex3fv (cube[0]);
	 glVertex3fv (cube[6]);
	 glVertex3fv (cube[4]);
	 
	 glColor3f (0.0, 0.0, 1.0);
	 glVertex3fv (cube[5]);
	 glVertex3fv (cube[4]);
	 glVertex3fv (cube[2]);
	 glVertex3fv (cube[3]);
	 
	 glColor3f (1.0, 1.0, 0.0);
	 glVertex3fv (cube[7]);
	 glVertex3fv (cube[6]);
	 glVertex3fv (cube[0]);
	 glVertex3fv (cube[1]);
	 
	 glColor3f (1.0, 0.0, 1.0);
	 glVertex3fv (cube[5]);
	 glVertex3fv (cube[4]);
	 glVertex3fv (cube[6]);
	 glVertex3fv (cube[7]);
	 
	 glColor3f (0.0, 1.0, 1.0);
	 glVertex3fv (cube[0]);
	 glVertex3fv (cube[1]);
	 glVertex3fv (cube[3]);
	 glVertex3fv (cube[2]);
	 glEnd();
	 glFlush();
}
 
void keyReact (unsigned char key, int x, int y) {
	 
	 switch (key) {
		 case 'q':
		 case 'Q':
			 exit (EXIT_SUCCESS);
			 break;
		 case 'x':
		 case 'y':
		 case 'z':
		 case 'X':
		 case 'Y':
		 case 'Z':
			 myRotate(key);
			 glutPostRedisplay();
			 break;
	}
}
 
 
 
int main (int argc, char **argv) {
	 
	 glutInit (&argc, argv);
	 glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH | GLUT_SINGLE);
	 glutCreateWindow ("Cube");	
	 glutInitWindowPosition (0, 0);
	 glutInitWindowSize (400, 500);
	 glutDisplayFunc (display);
	 glutKeyboardFunc (keyReact);
	 myInit();
	 glutMainLoop();
	 return 0;
}
 
