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

/*- Inclusao das bibliotecas IUP e CD: ------------------------------------*/
#include <windows.h>                /* inclui as definicoes do windows para o OpenGL */
#include <gl/gl.h>                  /* prototypes do OpenGL */
#include <gl/glu.h>                 /* prototypes do OpenGL */



unsigned char checker[64][64][3];

double vertices[][3]={{-1,-1, 1}, {-1, 1, 1}, { 1, 1, 1}, { 1,-1, 1},
                      {-1,-1,-1}, {-1, 1,-1}, { 1, 1,-1}, { 1,-1,-1}};

double colors[][3]= { {1,0,0},{0,1,1},{1,1,0},{0,1,0},{0,0,1},{1,0,1}};


static void polygon(int a, int b, int c, int d)
{
   glBegin(GL_POLYGON);
      glColor3dv(colors[a]);
      glTexCoord2d(0.,0.);
      glVertex3dv(vertices[a]);

      glColor3dv(colors[b]);
      glTexCoord2d(0.,1.);
      glVertex3dv(vertices[b]);

      glColor3dv(colors[c]);
      glTexCoord2d(1.,1.);
      glVertex3dv(vertices[c]);

      glColor3dv(colors[d]);
      glTexCoord2d(1.,0.);
      glVertex3dv(vertices[d]);
   glEnd();
}

static void colorCube( void)  
{
   polygon(0,3,2,1);
   polygon(2,3,7,6);
   polygon(3,0,4,7);
   polygon(1,2,6,5);
   polygon(4,5,6,7);
   polygon(5,4,0,1);
}


static void checkerboard( )
{
   int i,j;
   for(i=0;i<64;i++){
      for(j=0; j<64; j++){
         int c = (( ( (i&0x8)==0) ^ ((j&0x8))==0) )*255;
         checker[i][j][0]=(unsigned char)c;
         checker[i][j][1]=(unsigned char)c;
         checker[i][j][2]=(unsigned char)c;
      }
   }
}


void ccInit(void)
{
   checkerboard( );
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_TEXTURE_2D);
   glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0,GL_RGB,GL_UNSIGNED_BYTE,checker);
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
   glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
}


void ccDraw(void)
{
   colorCube();
}




