L'exécutable

TP-Animation-Clavier-Camera01.gif (14799 octets)

Le source : TP-Animation-Clavier-Camera-Bis.cpp

/* Auteur: Nicolas JANEY           */
/* nicolas.janey@univ-fcomte.fr    */
/* Septembre 2004                  */
/* Animation, gestion du clavier   */
/* et camera                       */

#include <stdlib.h>

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

static int l = 1;
static int anim = 1;
static float az = 0.0F;
static int tx;
static int ty;
static int mode =0;
static int obj = 1;

void idle(void) {
  az += 1.0F;
  glutPostRedisplay();
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case ' '  : anim = !anim;
                glutIdleFunc((anim) ? idle : NULL);
                break;
    case 0x0D : mode = (mode+1)%4;
                glutPostRedisplay();
                break;
    case 'l'  :
    case 'L'  : l = !l ;
                glutPostRedisplay();
                break;
    case 0x1B : exit(0); }
}

void special(int key,int x,int y) {
  switch ( key ) {
    case GLUT_KEY_F2 : obj = !obj;
                       glutPostRedisplay();
                       break; }
}

void reshape(int dx,int dy) {
  tx = dx;
  ty = dy;
  glViewport(0,0,tx,ty);
}

void display(void) {
  glClearColor(0.8F,0.8F,0.8F,1.0F) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
  if ( l )
    glEnable(GL_LIGHTING);
    else
    glDisable(GL_LIGHTING);
  switch (mode) {
    case 0 : glMatrixMode(GL_PROJECTION);
             glLoadIdentity();
             glOrtho((double) -3*tx/ty,(double) 3*tx/ty,
                     -3.0,3.0,
                     -3.0,3.0) ;
             glMatrixMode(GL_MODELVIEW);
             glLoadIdentity();
             break;
    case 1 : glMatrixMode(GL_PROJECTION);
             glLoadIdentity();
             gluPerspective(3.5F,(double) tx/ty,
                            97.0,103.0) ;
             glMatrixMode(GL_MODELVIEW);
             glLoadIdentity();
             gluLookAt(0.0,0.0,100.0,
                       0.0,0.0,0.0,
                       0.0,1.0,0.0);
             break;
    case 2 : glMatrixMode(GL_PROJECTION);
             glLoadIdentity();
             gluPerspective(72.0F,(double) tx/ty,
                            2.0,8.0) ;
             glMatrixMode(GL_MODELVIEW);
             glLoadIdentity();
             gluLookAt(0.0,0.0,5.0,
                       0.0,0.0,0.0,
                       0.0,1.0,0.0);
             break;
    case 3 : glMatrixMode(GL_PROJECTION);
             glLoadIdentity();
             gluPerspective(42.0F,(double) tx/ty,
                            2.0,12.0) ;
             glMatrixMode(GL_MODELVIEW);
             glLoadIdentity();
             gluLookAt(5.0,5.0,5.0,
                       0.0,0.0,0.0,
                       0.0,1.0,0.0);
             break; }
  glPushMatrix();
  glRotatef(az,0.0F,0.0F,1.0F);
  glPushMatrix();
  glTranslatef(1.5F,0.0F,0.0F);
  if ( obj )
    glutSolidSphere(1.0,36,36);
    else
    glutWireSphere(1.0,18,12);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-1.5F,0.0F,0.0F);
  if ( obj )
    glutSolidCube(2.0);
    else
    glutWireCube(2.0);
  glPopMatrix();
  glPopMatrix();
  glFlush() ;
  glutSwapBuffers();
}

void init(void) {
  GLfloat l_pos[] = { 0.0F,0.0F,1.0F,0.0F };
  GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F };
  glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
  glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
  glEnable(GL_LIGHT0);
  glColor4fv(c) ;
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  glutInitWindowSize(240,240); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Animation, clavier et caméra");
  init(); 
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutDisplayFunc(display);
  glutIdleFunc(idle);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

RETOUR