#include <cstdlib>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

int Width = 500;
int Height = 500;

void Display()
{
	glClearColor(0, 0, 0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	gluLookAt(4, 2, 2,
                  0, 0, 0,
                  0, 0, 1);

	glColor3ub(255, 0, 0);
	glBegin(GL_QUAD_STRIP);
		glColor3ub(255, 0, 0);
		glVertex3f(-1, -1, -1);
		glColor3ub(0, 0, 255);
		glVertex3f(-1, -1, 1);

		glColor3ub(255, 0, 0);
		glVertex3f(-1, 1, -1);
		glColor3ub(0, 0, 255);
		glVertex3f(-1, 1, 1);

		glColor3ub(255, 0, 0);
		glVertex3f(1, 1, -1);
		glColor3ub(0, 0, 255);
		glVertex3f(1, 1, 1);

		glColor3ub(255, 0, 0);
		glVertex3f(1, -1, -1);
		glColor3ub(0, 0, 255);
		glVertex3f(1, -1, 1);

		glColor3ub(255, 0, 0);
		glVertex3f(-1, -1, -1);
		glColor3ub(0, 0, 255);
		glVertex3f(-1, -1, 1);
	glEnd();

	glBegin(GL_QUADS);
		glColor3ub(255, 0, 0);
		glVertex3f(-1, -1, -1);
		glVertex3f(-1, 1, -1);
		glVertex3f(1, 1, -1);
		glVertex3f(1, -1, -1);

		glColor3ub(0, 0, 255);
		glVertex3f(-1, -1, 1);
		glVertex3f(-1, 1, 1);
		glVertex3f(1, 1, 1);
		glVertex3f(1, -1, 1);
	glEnd();

	glFinish();
}

void Reshape(int w, int h)
{
	Width = w;
	Height = h;

	glEnable(GL_DEPTH_TEST);
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45, (double)Width / Height, 3, 10);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void Keyboard(unsigned char key, int x, int y)
{
	const char ESCAPE = '\033';
	if (key == ESCAPE)
		exit(0);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(Width, Height);
	glutCreateWindow("Cube Example");

	glutDisplayFunc(Display);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);

	glutMainLoop();
	return 0;
}
