L'exécutable

TP02-02a.png

Le source : BrasRobotCamera1.cpp

/* Modelisation d'un bras robot,                */
/* gestion du clavier                           */
/* et camera de visualisation en pespective     */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2011                               */

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

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

#ifndef M_PI
#define M_PI 3.14159
#endif

/* Variables et constantes globales             */
/* pour les angles et les couleurs utilises     */

static int aff = 1;
static int lum = 1;
static float r1 = 10.0F;
static float r2 = -20.0F;
        
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F };

/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */

void init(void) {
  const GLfloat shininess[] = { 50.0 };
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHT2);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_AUTO_NORMAL);
}

void mySolidCylinder(float hauteur,float rayon,int ns,int nl,int bases) {
  GLboolean nm = glIsEnabled(GL_NORMALIZE);
  if ( !nm )
    glEnable(GL_NORMALIZE);
  float normale[4];
  glGetFloatv(GL_CURRENT_NORMAL,normale);
  glPushMatrix();
  for ( int j = 0 ; j < nl ; j++ ) {
    float hi = hauteur/2-j*hauteur/nl;
    float hf = hi-hauteur/nl;
    glBegin(GL_QUAD_STRIP);
    forint 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,hi,z);
      glVertex3f(x,hf,z); }
    glEnd(); }
  if ( bases ) {
    glBegin(GL_POLYGON);
    glNormal3f(0.0F,1.0F,0.0F);
    forint i = 0 ; i < ns ; i++ ) {
      float a = (2*M_PI*i)/ns;
      float cs = cos(a);
      float sn = -sin(a);
      float x = rayon*cs;
      float z = rayon*sn;
      glVertex3f(x,hauteur/2.0F,z); }
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3f(0.0F,-1.0F,0.0F);
    forint i = 0 ; i < ns ; i++ ) {
      float a = (2*M_PI*i)/ns;
      float cs = cos(a);
      float sn = sin(a);
      float x = rayon*cs;
      float z = rayon*sn;
      glVertex3f(x,-hauteur/2.0F,z); }
    glEnd(); }
  glPopMatrix();
  glNormal3f(normale[0],normale[1],normale[2]);
  if ( !nm )
    glDisable(GL_NORMALIZE);
}

void brasRobot(float r1,float r2) {
  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,12,12,1);
  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,12,12,1);
  glPopMatrix();
  glPopMatrix();
}

/* Scene dessinee                               */

void scene(void) {
  glPushMatrix();
  brasRobot(r1,r2);
  glPopMatrix();
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */

void reshape(int x,int y) {
  double distance = sqrt(500.0);
  glViewport(0,0,x,y); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(16.0,(double) x/y,distance-7.0,distance+7.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0.0,10.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin                      */

void display(void) {
  glClearColor(0.5F,0.5F,0.5F,0.5F);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  const GLfloat light0_position[] = { 0.0,0.0,0.0,1.0 };
  const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 };
  const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 };
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  if ( lum )
    glEnable(GL_LIGHTING);
    else
    glDisable(GL_LIGHTING);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  glPushMatrix();
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

/* Fonction executee lors de la frappe          */
/* d'une touche du special clavier              */

void special(int code,int x,int y) {
  switch ( code ) {
    case GLUT_KEY_UP :
      r1 += 1.0F;
      glutPostRedisplay(); 
      break;
    case GLUT_KEY_DOWN :
      r1 -= 1.0F;
      glutPostRedisplay(); 
      break;
    case GLUT_KEY_RIGHT :
      r2 += 1.0F;
      glutPostRedisplay(); 
      break;
    case GLUT_KEY_LEFT :
      r2 -= 1.0F;
      glutPostRedisplay(); 
      break; }
}

/* Fonction executee lors de l'appui            */
/* d'une touche alphanumerique du clavier       */

void keyboard(unsigned char key,int x,int y) {
  switch (key) {
    case 0x20 :
      aff = !aff;
      glutPostRedisplay();
      break;
    case 0x0D :
      lum = !lum;
      glutPostRedisplay();
      break;
    case 0x1B :
      exit(0);
      break; }
}

/* Fonction principale                          */

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(600,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Bras robot avec camera avec gluLookAt"); 
  init();
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR