L'exécutable

Fichiers source:

/* Un programme OpenGL de deplacement           */
/* d'une camera au sein d'une scene             */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Octobre 2009                                 */

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

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

#include "ScenePourAnimation.h"

void idle(void);

/* Variables globales                           */

static int l = 1;

static float px = 0.0F;
static float py = 0.5F;
static float pz = 0.0F;
static float angle = 0.0F;
static float ouv = 60.0F;
static int anim = 0;

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
/* Configuration d'une camera de visualisation  */
/* en projection en perspective                 */

void reshape(int tx,int ty) {
  /* Configuration du viewport d'affichage      */
  glViewport(0,0,tx,ty);
  /* Choix et configuration de la camera        */
  /* de visualisation                           */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(ouv,(double) tx/ty,0.01,100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

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

void display(void) {
  glClearColor(0.6F,0.6F,0.6F,1.0F) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
  glPushMatrix();
  gluLookAt(px,py,pz,
            px+sin(angle),py,pz+cos(angle),
            0.0,1.0,0.0);
  /* Affichage de la scene                      */
  scene(1);
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

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

void special(int key,int x,int y) {
  switch ( key ) {
    case GLUT_KEY_UP      : px += 0.1F*sin(angle);
                            pz += 0.1F*cos(angle);
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_DOWN    : px -= 0.1F*sin(angle);
                            pz -= 0.1F*cos(angle);
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_RIGHT   : angle -= 0.03F;
                            glutPostRedisplay();
                            break;
    case GLUT_KEY_LEFT    : angle += 0.03F;
                            glutPostRedisplay();
                            break; }
}

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

void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 0x0D : { if ( !anim ) {
                    anim = !anim;
                    glutIdleFunc((anim) ? idle : NULL);
                    glutSpecialFunc(NULL); } }
                break;
    case 0x20 : l = !l ;
                glutPostRedisplay();
                break;
    case 0x1B : exit(0); }
}

/* Fonction executee lorsqu'aucun evenement     */
/* n'est en attente de gestion                  */

void idle(void) {
  static int n = 0;
  n++;
  angle += 6.28318F/720;
  glutPostRedisplay();
  if ( n == 720 ) {
    glutSpecialFunc(special);
    anim = !anim;
    n = 0;
    glutIdleFunc(NULL); }
}

/* Fonction principale                          */

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(450,200); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Deplacement de camera");
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR