#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "glut.h"
/*
this example shows, how to create 3D geometry (3 faces of a cube) and
display it using OpenGL. A single callback function is registered in main:
glutDisplayFunction(risi)
The cube faces model is rotated about x and y axis
to obtain spatial impression.
*/

typedef struct {
  float x;
  float y;
  float z;
} POINT;


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

int quads[3][4] = {{0, 3, 2, 1}, {4, 5, 6, 7},
			{0, 1, 5, 4}};

POINT normals[3] = {{0.0, 0.0, -1.0}, {0.0, 0.0, 1.0}, {0.0, -1.0, 0.0}};

void risi()
{
  float fi;
  int i,j;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glScalef(0.5, 0.5, 0.5);
  glRotatef(30.0, 0.0, 1.0, 0.0);
  glRotatef(30.0, 1.0, 0.0, 0.0);
  glColor3f(0.0, 1.0, 0.0);
  for (i=0;i<3;i++) {
    if (i==0) glColor3f(1.0, 0.0, 0.0);
    if (i==1) glColor3f(0.0, 1.0, 0.0);
    if (i==2) glColor3f(0.0, 0.0, 1.0);
    glBegin(GL_QUADS);
      glNormal3f(normals[i].x, normals[i].y, normals[i].z);
      for(j=0; j<4; j++)
        glVertex3f(vertices[quads[i][j]].x,
		vertices[quads[i][j]].y,
		vertices[quads[i][j]].z);
    glEnd();
  }
  glFlush();
}

int main(int argn, char **argc)
{
  glutInit(&argn, argc);
  glutInitWindowSize(500, 500);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH | GLUT_RGB);
  glutCreateWindow("my first program");
  glutDisplayFunc (risi);
  glEnable(GL_LIGHTING);
  glEnable(GL_COLOR_MATERIAL);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  glutMainLoop();
  return 0;
}
