L'exécutable

TD4-4.png (21543 octets) TD4-5.png (21836 octets) TD4-6.png (22148 octets)

Le source : HuitSpheres.cpp

/* Huit spheres colorees affichees              */
/* par differentes cameras                      */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Septembre 2010                               */

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

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

/* Variables et constantes globales             */

static const float blanc[]   = { 1.0F,1.0F,1.0F,1.0F };
static const float jaune[]   = { 1.0F,1.0F,0.0F,1.0F };
static const float cyan[]    = { 1.0F,0.0F,1.0F,1.0F };
static const float magenta[] = { 0.0F,1.0F,1.0F,1.0F };
static const float rouge[]   = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[]    = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[]    = { 0.0F,0.0F,1.0F,1.0F };
static const float noir[]    = { 0.0F,0.0F,0.0F,1.0F };
static const float gris[]  = { 0.7F,0.7F,0.7F,1.0F };
static int vue = 0;
static int width;
static int height;

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

void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 0x0D :
      vue = (vue+1)%3;
      glutPostRedisplay(); 
      break;
    case 0x1B :
      exit(0); 
      break; }
}

/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */

void init(void) {
  const GLfloat mat_shininess[] = { 50.0 };
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,gris);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,gris);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_CULL_FACE);
  glClearColor(0.5F,0.5F,0.5F,0.5F);
}

/* Scene dessinee                               */

void scene(void) {
  glPushMatrix();
  glPushMatrix();
  glTranslatef(2.0F,2.0F,2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(2.0F,2.0F,-2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,jaune);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(2.0F,-2.0F,2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,cyan);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(2.0F,-2.0F,-2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-2.0F,2.0F,2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,magenta);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-2.0F,2.0F,-2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,vert);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-2.0F,-2.0F,2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,bleu);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-2.0F,-2.0F,-2.0F);
  glMaterialfv(GL_FRONT,GL_DIFFUSE,noir);
  glutSolidSphere(1.0,72,72);
  glPopMatrix();
  glPopMatrix();
}

/* 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 };
  glViewport(0,0,width,height); 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  switch (vue) {
    case 0:
      gluPerspective(2.7F,(float) width/height,1.0,160.0);
      break;
    case 1:
      gluPerspective(22.0F,(float) width/height,1.0,25.0);
      break;
    case 2:
      gluPerspective(18.0F,(float) width/height,1.0,35.0);
      break; }
  glMatrixMode(GL_MODELVIEW) ;
  glLoadIdentity() ;
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  switch (vue) {
    case 0:
      gluLookAt(0.0,0.0,150.0,0.0,0.0,0.0,0.0,1.0,0.0);
      break;
    case 1:
      gluLookAt(0.0,0.0,-20.0,0.0,0.0,0.0,0.0,1.0,0.0);
      break;
    case 2:
      gluLookAt(0.0,20.0,-20.0,0.0,0.0,0.0,0.0,1.0,0.0);
      break; }
  scene();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL\n",error);
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */
/* -> Ajustement de la camera de visualisation  */

void reshape(int w,int h) {
  width = w;
  height = h;
}

/* 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("Differents points de vue sur 8 spheres"); 
  init();
  glutKeyboardFunc(keyboard);
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR