//////////////////////////////////////////////////////
// Example #1: Draw Lines along major axis
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>

#define SCL(A) ((A)/30.0)


void cylinder( float radius, float height) {
  GLUquadricObj *quadObj;
  quadObj = gluNewQuadric();
  gluQuadricDrawStyle(quadObj, GLU_LINE);
  gluQuadricNormals(quadObj, GLU_SMOOTH);
  gluCylinder(quadObj, radius*2, radius*2, height, radius*90, height*10);
}

void Box(float sizex, float sizey, float sizez,  GLenum type ) {

  
 //normals       
  static GLfloat n[6][3] =
  {
    {-1.0, 0.0, 0.0}, /*   to our left  */
    {0.0, 1.0, 0.0},  /*   up  */
    {1.0, 0.0, 0.0},  /*  to our right  */
    {0.0, -1.0, 0.0}, /*   down */
    {0.0, 0.0, 1.0},  /*  towards us  */
    {0.0, 0.0, -1.0}  /*  away from us  */
  };
  
  //corner numbers for 6 faces of a cube
  static GLint faces[6][4] =
  {
    {0, 1, 2, 3},
    {3, 2, 6, 7},
    {7, 6, 5, 4},
    {4, 5, 1, 0},
    {5, 6, 2, 1},
    {7, 4, 0, 3}
  };
  
  // 8 points on the cube
  GLfloat v[8][3];
  
  GLint i;

 // [][0] is x co-ord  [][1] is y co-ord  [][2] is z co-ord
  // set up point x co-ords
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -sizex/2 ;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] =  sizex/2 ;
  // set up point y co-ords
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -sizey/2 ;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] =  sizey/2 ;
  // set up point z co-ords
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = -sizez/2 ;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] =  sizez/2 ;

  for (i = 5; i >= 0; i--) {
    //glBegin(GL_QUADS); //
    glBegin(type);
    glNormal3fv(&n[i][0]);
    glVertex3fv(&v[faces[i][0]][0]);
    glVertex3fv(&v[faces[i][1]][0]);
    glVertex3fv(&v[faces[i][2]][0]);
    glVertex3fv(&v[faces[i][3]][0]);
    glEnd();
  }                
}


void leg(float servo1, float servo2, float servo3) {


   glColor3f(1,0,0);                                // set colour
   glRotated(servo1,0,1,0);                            // set angle
   glTranslated(SCL((5/2)-1), 0, 0);            // distance to left pivot from middle
   Box(SCL(5), SCL(3), SCL(6),  GL_LINE_LOOP ); // create it  
   glTranslated(SCL((5/2)-1), 0, 0);              // distance to right pivot from middle
   
   glColor3f(1,1,0);                               // set colour
   glRotated(servo2,0,0,1);                           // set angle
   glTranslated(SCL((11/2)-1), 0, 0);              // distance to left pivot from middle
   Box(SCL(11), SCL(3), SCL(6),  GL_LINE_LOOP ); // create it  
   glTranslated(SCL((11/2)-1), 0, 0);            // distance to right pivot from middle
   
   glColor3f(0,0,1);
   glRotated(servo3,0,0,1);
   glTranslated(SCL((17/2)-1), 0, 0);
   Box(SCL(17), SCL(3), SCL(6),  GL_LINE_LOOP );   
   

}


void Display(void) {
   static int T = 0;

   glClear(GL_COLOR_BUFFER_BIT);  // clear buffer
    
   glLoadIdentity();
   // position the robot
   glRotated(10, 0, 0, 1);
   glRotated(23, 1, 0, 0);
   
 
   
   glPushMatrix();
   
   glColor3f(1,1,1);
   glTranslated(0, SCL(4), 0); // position body
   glRotated(90, 1, 0, 0);       
   cylinder( SCL(16/2), SCL(6.4));// create body
  
   
   
   glPopMatrix(); glPushMatrix();

   glRotated(17, 0, 1, 0);   
     glTranslated(SCL(15), 0, 0); // position to first leg       
   leg(-T, T*2, -T*3) ;
   
   glPopMatrix(); glPushMatrix();

   glRotated(17+(36.5*1), 0, 1, 0);   
      glTranslated(SCL(15), 0, 0); // position to first leg      
   leg(-T, T*2, -T*3) ;  
   
   glPopMatrix(); glPushMatrix();

   glRotated(17+(36.5*2), 0, 1, 0);   
     glTranslated(SCL(15), 0, 0); // position to first leg       
   leg(-T, T*2, -T*3) ;  
   
   glPopMatrix(); glPushMatrix();

   glRotated(17+(36.5*3), 0, 1, 0);   
      glTranslated(SCL(15), 0, 0); // position to first leg      
   leg(-T, T*2, -T*3) ;     
   
   
   
   glPopMatrix(); glPushMatrix();
   glRotated(-17, 0, 1, 0);   
     glTranslated(SCL(15), 0, 0); // position to first leg       
   leg(-T, T*2, -T*3) ;
      
   glPopMatrix(); glPushMatrix();
   glRotated(-17-(36.5*1), 0, 1, 0);   
      glTranslated(SCL(15), 0, 0); // position to first leg      
   leg(-T, T*2, -T*3) ;  
   
   glPopMatrix(); glPushMatrix();
   glRotated(-17-(36.5*2), 0, 1, 0);   
     glTranslated(SCL(15), 0, 0); // position to first leg       
   leg(-T, T*2, -T*3) ;  
   
   glPopMatrix(); // dont pop last one
   glRotated(-17-(36.5*3), 0, 1, 0);   
      glTranslated(SCL(15), 0, 0); // position to first leg      
   leg(-T, T*2, -T*3) ;     
   
   
   
 
   
   
   

   glLoadIdentity();

   glFlush();

   if (T < 360) T++;
   else T = 1;

   glutSwapBuffers();
 }
       
        
void Timer(int extra) {
  glutPostRedisplay();
  glutTimerFunc(30,Timer,0);
}
        
        
int main(int argc, char **argv) {
   glutInit(&argc, argv);


   glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB );
   glutInitWindowSize(640,480);
   glutCreateWindow("This is the window title");
   glutDisplayFunc(Display);



   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1,1,  -1, 1,  1, 3);
   glTranslated(0,0,-2);
 //  glTranslated(SCL(-41.25/2),SCL(-12.5/2),0);


   glMatrixMode(GL_MODELVIEW); // set camera position
   glLoadIdentity();
   //glRotated(45,1,0,0);


   glutTimerFunc(0,Timer,0);

   glutMainLoop();
   return 0;
}
