L'exécutable

Le source : GestionCamera.cpp

/* Auteur: Nicolas JANEY                   */
/* nicolas.janey@univ-fcomte.fr            */
/* Octobre 2006                            */
/* Gestion de camera en C + OpenGL + GLUT  */

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

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

#include "ModuleCylindres.h"

static int l = 1;
static int r = 1;
static int q = 0;

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

void reshape0(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(4.5*(double) -tx/ty,4.5*(double) tx/ty,-4.5,4.5,-4.5,4.5) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape1(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(10.0+4.5*(double) -tx/ty,10.0+4.5*(double) tx/ty,10.0-4.5,10.0+4.5,-10.0-4.5,-10.0+4.5) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape1Bis(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(10.0+4.5*(double) -tx/ty,10.0+4.5*(double) tx/ty,10.0-4.5,10.0+4.5,-10.0-1.5,-10.0+2.5) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape2(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(50.0,(double) tx/ty,10.0F-4.5F,10.0F+4.5F) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape2Bis(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(50.0,(double) tx/ty,10.0F-2.0F,10.0F+2.0F) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape3(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(5.0,(double) tx/ty,100.0F-4.5F,100.0F+4.5F) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void reshape4(int tx,int ty) {
  glViewport(0,0,tx,ty);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(23.0,(double) tx/ty,22.9F-4.5F,22.9F+4.5F) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(-10.0F,-5.0F,10.0F,0.0F,0.0F,-10.0F,0.0F,1.0F,0.0F);
}

void element() {
  glPushMatrix();
  glTranslatef(2.0F,0.0F,2.0F);
  solidCylindre(0.25F,3.0F,10,5);
  for ( int i = 0 ; i < 2 ; i++ ) {
    glTranslatef(0.0F,2.0F,0.0F);
    glutSolidCube(1.0F);
    glRotatef(90.0F,0.0F,0.0F,1.0F);
    glTranslatef(0.0F,2.0F,0.0F);
    solidCylindre(0.25F,3.0F,10,5); }
  glPopMatrix();
}

void scene() {
  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);
  if ( r )
    glRotatef(45.0F,1.0F,1.0F,0.0F);
  element();
  for ( int i = 1 ; i < 4 ; i++ ) {
    glPushMatrix();
    glRotatef(90.0F*i,1.0F,0.0F,0.0F);
    element();
    glPopMatrix(); }
  glPopMatrix();
}

void display(void) {
  if ( l )
    glEnable(GL_LIGHTING);
    else
    glDisable(GL_LIGHTING);
  glClearColor(0.8F,0.8F,0.8F,1.0F) ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
  glPushMatrix();
  switch ( q ) {
    case 1 : 
    case 2 : glTranslatef(10.0,10.0,10.0);
             break;
    case 5 : glTranslatef(0.0,0.0,-100.0);
             break;
    case 3 :
    case 4 :
    case 6 : glTranslatef(0.0,0.0,-10.0);
             break; }
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 'l'  : l = !l ;
                glutPostRedisplay();
                break;
    case 'r'  : r = !r ;
                glutPostRedisplay();
                break;
    case 0x0D : q = (q+1)%7 ;
                switch (q) {
                  case 0 : glutReshapeFunc(reshape0);
                           reshape0(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 1 : glutReshapeFunc(reshape1);
                           reshape1(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 2 : glutReshapeFunc(reshape1Bis);
                           reshape1Bis(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 3 : glutReshapeFunc(reshape2);
                           reshape2(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 4 : glutReshapeFunc(reshape2Bis);
                           reshape2Bis(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 5 : glutReshapeFunc(reshape3);
                           reshape3(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break;
                  case 6 : glutReshapeFunc(reshape4);
                           reshape4(glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
                           break; }
                glutPostRedisplay();
                break;
    case 0x1B : exit(0); }
}

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);
}

void idle(void) {
  rx += 1.1;
  ry += 1.2;
  rz += 1.3;
  glutPostRedisplay();
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
  glutInitWindowSize(250,250); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Gestion camera");
  init(); 
  glutKeyboardFunc(key);
  glutReshapeFunc(reshape0);
  glutDisplayFunc(display);
  glutIdleFunc(idle);
  glutMainLoop();
  return(0);
}

RETOUR