
#include <stdlib.h>
#include <GL/glut.h>

	GLfloat vertices[] = {-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[] = {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};

	GLfloat bcolors[] = {0.0,0.0,0.0,0.0,0.0,0.0,
	0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0, 
	0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0};

    GLubyte cubeIndices[]={0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};
	float eyez = 0.0 ;
	float atx,aty,atz ;
	float upx = 1.0 ;
	float upy = 0.0 ;
	float upz = 1.0 ;
	float eyex = 0.0 ;
	float eyey = 1.0 ;

static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 0;

void display(void)
{

	GLfloat light[3]={0.0, 20.0, 0.0};
	GLfloat m[16];

    int i,j;
	for(i=0;i<16;i++) m[i]=0.0;

    m[0]=m[5]=m[10]=1.0;
	m[7]=-1.0/light[1];

/* display callback, clear frame buffer and z buffer,
   rotate cube and draw, swap buffers */

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	gluLookAt(eyex,eyey,eyez,atx,aty,atz,upx,upy,upz);
	glPushMatrix();
	glTranslatef(0.0, 3.0, 0.0);
    glColorPointer(3,GL_FLOAT, 0, colors); 
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);


	glutSwapBuffers(); 
}

void spinCube()
{

/* Idle callback, spin cube 2 degrees about selected axis */

	theta[axis] += 2.0;
	if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
	glutPostRedisplay();
}

void keyboard (unsigned char key, int x, int y)  {
	switch (key) {
		case 'x':
			eyex = 1.0;
			eyez = eyey = 0.0;

			glutPostRedisplay();
			break;

		case 'y':
			eyey = 2.0;
			eyez = eyex = 0.0;
			glutPostRedisplay();
			break;


		case 'z':
			eyez = 2.0;
			eyey = eyex = 0.0;


			glutPostRedisplay();
			break;

		case 'U':
			upx = 1.0 ;
			upy = upz = 0.0 ;


		case 'd':
		case 'D':
			upy = 1.0 ;
			upx = upz = 0.0 ;


		case 'r':
		case 'R':
			upz = 1.0 ;
			upy = upx = 0.0 ;		



  glutPostRedisplay();
   }
}


void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (w <= h)
        glOrtho(-4.0, 4.0, -3.0 * (GLfloat) h / (GLfloat) w,
            5.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
    else
        glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
            4.0 * (GLfloat) w / (GLfloat) h, -3.0, 5.0, -10.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
}

void
main(int argc, char **argv)
{


/* need both double buffering and z buffer */

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("colorcube");
    glutReshapeFunc(myReshape);
    glutDisplayFunc(display);
    glutIdleFunc(spinCube);
    glEnable(GL_DEPTH_TEST);
 	glEnableClientState(GL_COLOR_ARRAY); 
	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glutKeyboardFunc(keyboard);
    glColorPointer(3,GL_FLOAT, 0, colors); 
	glClearColor(1.0,1.0,1.0,1.0);
	glColor3f(1.0,1.0,1.0);
    glutMainLoop();
}
