L'exécutable

Le source : TD-Croix.cpp

/* Auteur: Nicolas JANEY         */
/* nicolas.janey@univ-fcomte.fr  */
/* Janvier 2004                  */
/* Visualisation en perspective  */

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

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

#include "Croix.h"

static float r1 = 0.0F;
static float r2 = 0.0F;
static float r3 = 0.0F;
static float disc = 1.0F;
static int mode = 0;
static int anim = 0;
static int lumiere = 0;
static int culling = 0;
static GLfloat couleurBlanc[] = { 1.0F,1.0F,1.0F,1.0F };
static GLfloat couleurRouge[] = { 1.0F,0.0F,0.0F,1.0F };
static GLfloat couleurVert[] = { 0.0F,1.0F,0.0F,1.0F };
static GLfloat couleurBleu[] = { 0.0F,0.0F,1.0F,1.0F };
static GLfloat couleurNoir[] = { 0.0F,0.0F,0.0F,1.0F };

void init(void) {
  GLfloat mat_shininess[] = { 50.0F };
  GLfloat light0_position[] = { 1.0F,1.0F,1.0F,0.0F };
  GLfloat light1_position[] = { -1.0F,1.0F,1.0F,0.0F };
  GLfloat light2_position[] = { 1.0F,-1.0F,1.0F,0.0F };
  glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc);
  glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurVert);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBleu);
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHT2);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_AUTO_NORMAL);
  glClearColor(0.0F,0.0F,0.0F,1.0F);
}

void scene(void) {
  int fac =(int) (18*disc);
  glPushMatrix();
  glRotatef(r1,0.0F,1.0F,0.0F);
  glRotatef(r2,1.0F,0.0F,0.0F);
  glRotatef(r3,0.0F,0.0F,1.0F);
  glColor4fv(couleurBlanc);
  croix(fac);
  glPopMatrix();
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  if ( lumiere )
    glEnable(GL_LIGHTING);
    else
    glDisable(GL_LIGHTING);
  if ( culling )
    glEnable(GL_CULL_FACE);
    else
    glDisable(GL_CULL_FACE);
  glPolygonMode(GL_FRONT_AND_BACK,(mode) ? GL_LINE : GL_FILL);
  glPushMatrix();
  scene();
  glPopMatrix();
  glFlush();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("\nAttention erreur %d\n",error);
  glutSwapBuffers();
}

void idle(void) {
  r1 += 2.1137F ;
  r2 += 3.174F ;
  r3 += 4.715F ;
  glutPostRedisplay() ;
}

void special(int k,int x,int y) {
  switch (k) {
    case GLUT_KEY_F1        : mode = !mode;
                              glutPostRedisplay();
                              break;
    case GLUT_KEY_UP        : disc *= 1.01F;
                              glutPostRedisplay();
                              break;
    case GLUT_KEY_DOWN      : disc /= 1.01F;
                              glutPostRedisplay();
                              break; }
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 32     : lumiere = !lumiere;
                  glutPostRedisplay();
                  break;
    case 0x0D   : culling = !culling;
                  glutPostRedisplay();
                  break;
    case 27     : exit(0);
                  break; }
}

void souris(int bouton,int etat,int x,int y) {
  if ( ( bouton == GLUT_LEFT_BUTTON ) && ( etat == GLUT_UP ) )
    anim = !anim;
  glutIdleFunc((anim) ? idle : NULL);
}

void reshape(int w,int l) {
  glViewport(0,0,w,l);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  /* définition du type de caméra et parametrage */
  gluPerspective(5,(float) w/l,100.0,200.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  /* placement et orientation de la camera */
  gluLookAt(100.0,100.0,100.0,
            0.0,0.0,0.0,
            0.0,1.0,0.0);
}

static void select(int selection) {
  switch (selection) {
    case 0  : exit(0); }
  glutPostRedisplay();
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(270,250); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Camera en perspective"); 
  init();
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutMouseFunc(souris);
  glutDisplayFunc(display);
  glutCreateMenu(select);
  glutAddMenuEntry("Quitter",0);
  glutAttachMenu(GLUT_RIGHT_BUTTON);
  glutMainLoop();
  return(0);
}

RETOUR