
            
Fichier source : BrasRobotComplet.cpp
/* Fonctionnement de GLUt                       */
    /*                                              */
    /* Auteur: Nicolas JANEY                        */
    /* nicolas.janey@univ-fcomte.fr                 */
    /* Janvier 2019                                 */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    #include <GL/glut.h>
    #include <GL/gl.h>
    #include <GL/glu.h>
    
    /* Variables globales                           */
    
    static float r1 = 0.0F;            // Angle de rotation du bras du robot
    static float r2 = 0.0F;            // Angle de rotation de l'avant-bras du robot
    static float r3 = 0.0F;            // Angle de rotation de la pince
    static float r4 = 9.0F;            // Ouverture de la pince (angle)
    static int pMode = 1;              // Flag de switch entre modes d'affichage wireframe et fill 
    
    static int wTx = 600;              // Resolution horizontale de la fenetre
    static int wTy = 360;              // Resolution verticale de la fenetre
    static int wPx = 50;               // Position horizontale de la fenetre
    static int wPy = 50;               // Position verticale de la fenetre
    
    /* Fonction d'initialisation des parametres     */
    /* OpenGL ne changeant pas au cours de la vie   */
    /* du programme                                 */
    
    static void init(void) {
      glEnable(GL_LIGHTING);
      glEnable(GL_LIGHT0);
      glDepthFunc(GL_LESS);
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_NORMALIZE);
    }
    
    /* Scene dessinee                               */
    
    #include <math.h>
    
    #ifndef M_PI
    #define M_PI 3.14159
    #endif
    
    /* Dessin d'une "boite" parallelipipedique      */
    
    void mySolidCube(double w,double h,double d) {
      glPushMatrix();
      glScalef((float) w,(float) h,(float) d);
      glutSolidCube(1.0);
      glPopMatrix();
    }
    
    /* Dessin d'un cylindre                         */
    
    static void mySolidCylinder(double hauteur,double rayon,int ns) {
      glPushMatrix();
      hauteur /= 2.0F;
      glBegin(GL_QUAD_STRIP);
      for( int i = 0 ; i <= ns ; i++ ){
        float a = (2*M_PI*i)/ns;
        float cs = cos(a);
        float sn = -sin(a);
        glNormal3f(cs,0.0F,sn);
        float x = rayon*cs;
        float z = rayon*sn;
        glVertex3f(x,hauteur,z);
        glVertex3f(x,-hauteur,z); }
      glEnd();
      glPopMatrix();
    }
    
    /* Une mandibule                                */
    
    void mandibule(float r4) {
      glPushMatrix();
      glTranslatef(0.0F,0.0F,-0.65F);
      glRotatef(r4,0.0F,-1.0F,0.0F);
      glRotatef(90.0F,0.0F,1.0F,0.0F);
      glutSolidCone(0.3,1.8,12,6);
      glPopMatrix();
    }
    
    static void brasRobot(float r1,float r2,float r3,float r4) {
      glPushMatrix();
      glRotatef(r1,0.0F,1.0F,0.0F);
      glTranslatef(1.5F,0.0F,0.0F);
      glPushMatrix();
      glRotatef(90.0F,0.0F,0.0F,1.0F);
      mySolidCylinder(3.0,0.5,24);
      glPopMatrix();
      glTranslatef(1.5F,0.0F,0.0F);
      glRotatef(r2,0.0F,1.0F,0.0F);
      glTranslatef(1.5F,0.0F,0.0F);
      glPushMatrix();
      glRotatef(90.0F,0.0F,0.0F,1.0F);
      mySolidCylinder(3.0,0.4,24);
      glPopMatrix();
      glTranslatef(1.8F,0.0F,0.0F) ;
      glRotatef(r3,1.0F,0.0F,0.0F) ;
      mySolidCube(0.6,1.2,1.8) ;
      glTranslatef(0.3F,0.0F,0.0F) ;
      mandibule(r4);
      glRotatef(180.0F,1.0F,0.0F,0.0F);
      mandibule(r4);
      glPopMatrix();
    }
    
    void scene(void) {
      glPushMatrix();
      glRotatef(20.0F,1.0F,1.0F,1.0F);
      glScalef(0.2F,0.2F,0.2F);
      brasRobot(r1,r2,r3,r4);
      glPopMatrix();
    }
    
    /* Fonction executee lors d'un rafraichissement */
    /* de la fenetre de dessin                      */
    
    static void display(void) {
      if ( pMode == 1 ) {
        glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
        glEnable(GL_LIGHTING); }
        else {
        glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
        glDisable(GL_LIGHTING); }
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glPushMatrix();
      scene();
      glPopMatrix();
      glFlush();
      glutSwapBuffers();
      int error = glGetError();
      if ( error != GL_NO_ERROR )
        printf("Attention erreur %d\n",error);
    }
    
    /* Fonction executee lors d'un changement       */
    /* de la taille de la fenetre OpenGL            */
    
    static void reshape(int wx,int wy) {
      wTx = wx; 
      wTy = wy; 
      glViewport(0,0,wx,wy); 
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      double ratio =(double) wx/wy; 
      if ( wx > wy ) 
        glOrtho(-ratio,ratio,-1.0,1.0,-10.0,10.0);
        else
        glOrtho(-1.0,1.0,-1.0/ratio,1.0/ratio,-10.0,10.0);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }
    
    /* Fonction executee lors de l'appui            */
    /* d'une touche alphanumerique du clavier       */
    
    static void keyboard(unsigned char key,int x,int y) {
      switch (key) {
        case 45 : 
          { r4 += 0.1F;
            if ( r4 > 21.0F )
              r4 = 21.0F;
              else
              glutPostRedisplay(); }
            break;
        case 43 :
          { r4 -= 0.1F;
            if ( r4 < 9.0F )
              r4 = 9.0F;
              else
              glutPostRedisplay(); }
            break;
        case ' ' :
          { pMode = !pMode;
            glutPostRedisplay(); }
          break;
        case 'f' :
        case 'F' :
          glutFullScreen();
          break;
        case 0x1B :
          exit(0);
          break; }
    }
    
    /* Fonction executee lors de l'appui            */
    /* d'une touche speciale du clavier :           */
    /*   - touches de curseur                       */
    /*   - touches de fonction                      */
    
    static void special(int specialKey,int x,int y) {
      switch(specialKey) {
        case GLUT_KEY_UP :
          r1 += 1.0F;
          glutPostRedisplay();
          break;
        case GLUT_KEY_DOWN :
          r1 -= 1.0F;
          glutPostRedisplay();
          break;
        case GLUT_KEY_PAGE_UP :
          r2 += 1.0F;
          glutPostRedisplay();
          break;
        case GLUT_KEY_PAGE_DOWN :
          r2 -= 1.0F;
          glutPostRedisplay();
          break;
        case GLUT_KEY_RIGHT :
          r3 += 1.0F;
          glutPostRedisplay();
          break;
        case GLUT_KEY_LEFT :
          r3 -= 1.0F;
          glutPostRedisplay();
          break; }
    }
    
    /* Fonction exécutée automatiquement            */
    /* lors de l'exécution de la fonction exit()    */
    
    static void clean(void) {
      printf("Bye\n");
    }
    
    /* Fonction principale                          */
    
    int main(int argc,char **argv) {
    
      atexit(clean);
    
      glutInit(&argc,argv);
      glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
      glutInitWindowSize(wTx,wTy);
      glutInitWindowPosition(wPx,wPy);
      glutCreateWindow("Un bras robot avec une pince de saisie");
      init();
      glutKeyboardFunc(keyboard);
      glutSpecialFunc(special);
      glutReshapeFunc(reshape);
      glutDisplayFunc(display);
      glutMainLoop();
      return(0);
    }