/* Auteur: Nicolas JANEY                */
/* nicolas.janey@univ-fcomte.fr         */
/* Janvier 2014                         */
/* Modelisation OpenGL d'une pince      */
/* pour un bras robot                   */

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

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

#include "Modules/ModuleCouleurs.h"
#include "Modules/ModuleCylindres.h"
#include "Modules/ModuleFont.h"
#include "Modules/ModuleManipulateur.h"
#include "Modules/ModuleMenus.h"
#include "Modules/ModuleReshape.h"

static int maille = 0 ;
static int scn = 0;
static int n = 20;
static int wire = 0;

void init(void) {
  GLfloat light_position0[] = {  30.0F, 30.0F,30.0F,1.0F };
  GLfloat light_position1[] = { -30.0F,-30.0F,30.0F,1.0F };
  glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
  glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
  glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
  glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
  glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
  glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  setManipulateurClavierAngle(35.0F,20.0F,0.0F);
}

void axes() {
  glPushMatrix() ;
  glColor4fv(couleurJaune()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(1.0F,0.0F,0.0F) ;
  glEnd() ;
  placeFontCursor(1.05F,0.05F,0.05F);
  simpleBitmapOutput("x");
  glColor4fv(couleurCyan()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,1.0F,0.0F) ;
  glEnd() ;
  placeFontCursor(0.05F,1.05F,0.05F);
  simpleBitmapOutput("y");
  glColor4fv(couleurMagenta()) ;
  glBegin(GL_LINES) ;
  glVertex3f(0.0F,0.0F,0.0F) ;
  glVertex3f(0.0F,0.0F,1.0F) ;
  glEnd() ;
  placeFontCursor(0.05F,0.05F,1.05F);
  simpleBitmapOutput("z");
  if ( maille ) {
    glColor4fv(couleurGrisMoyen()) ;
    for ( int i = -100 ; i < 100 ; i++ ) {
      glBegin(GL_LINES) ;
      glVertex3f((float) i,200.0F,0.0F) ;
      glVertex3f((float) i,-200.0F,0.0F) ;
      glEnd() ;
      glBegin(GL_LINES) ;
      glVertex3f(200.0F,(float) i,0.0F) ;
      glVertex3f(-200.0F,(float) i,0.0F) ;
      glEnd() ; } }
  glPopMatrix() ;
}

///////////////////////////////////////////////////

void mandibule(float t,float r) {
  glPushMatrix();
  glTranslatef(t,0.0F,0.0F);
  glRotatef(r,0.0F,0.0F,1.0F);
  glRotatef(-90.0F,1.0F,0.0F,0.0F);
  glutSolidCone(0.15F,1.0F,18,3);
  glPopMatrix();
}

void pince(float t,float r) {
  glPushMatrix();
  solidCylindre(0.5F,0.3F,18,2);
  glTranslatef(0.0F,0.15f,0.0F);
  mandibule(t,r);
  glRotatef(180.0F,0.0F,1.0F,0.0F);
  mandibule(t,r);
  glPopMatrix();
}

///////////////////////////////////////////////////

void scene(void) {
  glPushMatrix();
  if ( scn )
    pince(0.35F,0.0F);
    else
    pince(0.35F,20.0F);
  glPopMatrix();
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix() ;
  manipulateurSouris();
  manipulateurClavier();
  glDisable(GL_LIGHTING);
  axes();
  glPolygonMode(GL_FRONT_AND_BACK,(wire) ? GL_LINE : GL_FILL);
  if ( !wire )
    glEnable(GL_LIGHTING);
    else
    glColor4fv(couleurBlanc()) ;
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

void key(unsigned char key,int x,int y) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 0x0D : wire = !wire;
                  glutPostRedisplay();
                  break;
      case 0x20 : scn = !scn;
                  glutPostRedisplay();
                  break;
      case 'm'  : maille = !maille ;
                  glutPostRedisplay();
                  break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(400,400); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Une pince");
  init(); 
  creationMenuBasique();
  setParametresOrthoBasique(-1.5,1.5,-1.5,1.5,-50.0,50.0);
  setManipulateurDistance(1.0F);
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}
