L'exécutable

Le source : GestionClavierEtSouris.cpp

/* Auteur: Nicolas JANEY                    */
/* nicolas.janey@univ-fcomte.fr             */
/* Octobre 2006                             */
/* Gestion du clavier et de la souris       */
/* 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 dd = 0;
static int sx = 0;
static int sy = 0;
static float ax = 0.0F;
static float ay = 0.0F;
static float px = 0.0F;
static float py = 0.0F;
static float pz = 0.0F;
static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;
static float zoom = 1.0F;

static int l = 1;

void mouse(int bouton,int etat,int x,int y) {
  if ( bouton == GLUT_LEFT_BUTTON ) {
    sx = x;
    sy = y; 
    if ( etat == GLUT_DOWN ) {
      dd = 1; }
    if ( etat == GLUT_UP ) {
      dd = 0; } }
}

void motion(int x,int y) {
  if ( dd == 1 ) {
    ay += (x-sx);
    ax += (y-sy);
    sx = x; 
    sy = y;
    glutPostRedisplay(); }
}

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

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 'x'  : px -= 0.1F;
                glutPostRedisplay();
                break;
    case 'X'  : px += 0.1F;
                glutPostRedisplay();
                break;
    case 'y'  : py -= 0.1F;
                glutPostRedisplay();
                break;
    case 'Y'  : py += 0.1F;
                glutPostRedisplay();
                break;
    case 'z'  : pz -= 0.1F;
                glutPostRedisplay();
                break;
    case 'Z'  : pz += 0.1F;
                glutPostRedisplay();
                break;
    case 43   : zoom = zoom*1.01F;
                glutPostRedisplay();
                break;
    case 45   : zoom = zoom/1.01F;
                glutPostRedisplay();
                break;
    case 0x0D : l = !l ;
                glutPostRedisplay();
                break;
    case 0x1B : exit(0); }
}

void reshape(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,-10.0,10.0) ;
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

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();
  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();
  glTranslatef(px,py,pz);
  glRotatef(ax+rx,1.0F,0.0F,0.0F);
  glRotatef(ay+ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  glScalef(zoom,zoom,zoom);
  scene();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Attention, erreur OpenGL %d\n",error);
}

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(250,250); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Gestion clavier");
  init(); 
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutMouseFunc(mouse);
  glutMotionFunc(motion);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR