L'exécutable

Utilisation d'une caméra de visualisation en projection en perspective "placée" en position (5.0,5.0,5.0) (fonctions gluPerspective en mode GL_PROJECTION et gluLookAt en mode GL_MODELVIEW).

90° d'ouverture verticale

167° d'ouverture verticale (c'est trop)

Le source : CameraOpenGL5.cpp

SceneOpenGL.cpp - SceneOpenGL.h

/* Gestion de camera OpenGL                     */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2009                               */

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

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

#include "SceneOpenGL.h"

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

static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;

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

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  const GLfloat light0_position[] = { 1.0,1.0,1.0,0.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);
  glPushMatrix();
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL\n",error);
}

/* Fonction executee lorsqu'aucun evenement     */
/* n'est en file d'attente                      */

void idle(void) {
  rx += 0.4256F;
  ry += 0.6117F;
  rz += 0.4174F;
  glutPostRedisplay();
}

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

void reshape(int x,int y) {
  glViewport(0,0,x,y); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  double d = sqrt(3.0)*5.0;
  gluPerspective(167.0,(float) x/y,1.0,d+13.0F);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(5.0,5.0,5.0,
            0.0,0.0,0.0,
            0.0,1.0,0.0);
}

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

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

/* Fonction executee lors de l'appui            */
/* d'une touche de curseur ou d'une touche      */
/* page up ou page down                         */

void special(int key,int x,int y) {
  switch(key) {
    case GLUT_KEY_UP :
      rx++;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_DOWN :
      rx--;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_LEFT :
      ry++;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_RIGHT :
      ry--;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_PAGE_UP :
      rz++;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_PAGE_DOWN :
      rz--;
      glutPostRedisplay() ;
      break; }
}

/* Fonction principale                          */

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Gestion de caméra OpenGL"); 
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutReshapeFunc(reshape);
  //glutIdleFunc(idle);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR