L'exécutable

Le source : MaterielLumieres.cpp

/* Auteur: Nicolas JANEY                   */
/* nicolas.janey@univ-fcomte.fr            */
/* Octobre 2005                            */
/* Materiel et lumieres sur des spheres    */

#include <stdio.h>

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

#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"

static int mode = 0;
static int disc = 120;
static float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
static float gris[] = { 0.25F,0.25F,0.25F,1.0F };
static float noir[] = { 0.0F,0.0F,0.0F,1.0F };

void myinit(void) {
  glClearColor(0.0F,0.0F,0.0F,1.0F) ;
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_NORMALIZE);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glMaterialf(GL_FRONT,GL_SHININESS,32.0F);
}

void activationLumieres() {
  switch ( mode ) {
    case 0 :
    case 1 :
    case 2 : 
    case 3 : glEnable(GL_LIGHT1);
             glDisable(GL_LIGHT2);
             break;
    case 4 : glEnable(GL_LIGHT1);
             glEnable(GL_LIGHT2);
             break; }
}

void configurationLumieres() {
  switch ( mode ) {
    case 0 :
    case 1 : { GLfloat l_pos[] = { 0.0F,0.0F,1.0F,0.0F };
               glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
               glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
               glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
               glLightfv(GL_LIGHT0,GL_AMBIENT,noir); }
             break;
    case 2 :
    case 3 :
    case 4 : { GLfloat l_pos[] = { 1.0F,0.0F,0.0F,0.0F };
               glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
               glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
               glLightfv(GL_LIGHT0,GL_SPECULAR,blanc);
               glLightfv(GL_LIGHT0,GL_AMBIENT,noir); }
             { GLfloat l_pos[] = { 0.0F,0.0F,0.0F,1.0F };
               glLightfv(GL_LIGHT1,GL_POSITION,l_pos);
               glLightfv(GL_LIGHT1,GL_DIFFUSE,blanc);
               glLightfv(GL_LIGHT1,GL_SPECULAR,blanc);
               glLightfv(GL_LIGHT1,GL_AMBIENT,noir); }
             { GLfloat l_pos[] = { 0.0F,0.0F,10.0F,1.0F };
               GLfloat l_dir[] = { 0.0F,0.0F,-1.0F,0.0F };
               glLightfv(GL_LIGHT2,GL_POSITION,l_pos);
               glLightfv(GL_LIGHT2,GL_DIFFUSE,blanc);
               glLightfv(GL_LIGHT2,GL_SPECULAR,blanc);
               glLightfv(GL_LIGHT2,GL_AMBIENT,noir);
               glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,l_dir);
               glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,25); }
             break;
    case 5 : { glLightfv(GL_LIGHT0,GL_AMBIENT,gris); }
             { glLightfv(GL_LIGHT1,GL_AMBIENT,gris); }
             { glLightfv(GL_LIGHT2,GL_AMBIENT,gris); }
             break; }
}

void configurationMateriel() {
  switch (mode) {
    case 0 : glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
             glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
             glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
             glMaterialfv(GL_FRONT,GL_EMISSION,noir);
             break;
    case 1 :
    case 2 : glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
             glMaterialfv(GL_FRONT,GL_SPECULAR,vert);
             glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
             glMaterialfv(GL_FRONT,GL_EMISSION,noir);
             break;
    case 5 : glMaterialfv(GL_FRONT,GL_DIFFUSE,vert);
             glMaterialfv(GL_FRONT,GL_SPECULAR,bleu);
             glMaterialfv(GL_FRONT,GL_AMBIENT,gris);
             glMaterialfv(GL_FRONT,GL_EMISSION,gris);
             break;
    case 6 : glMaterialfv(GL_FRONT,GL_DIFFUSE,vert);
             glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
             glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
             glMaterialfv(GL_FRONT,GL_EMISSION,noir);
             break;
    case 7 : glMaterialfv(GL_FRONT,GL_DIFFUSE,noir);
             glMaterialfv(GL_FRONT,GL_SPECULAR,bleu);
             glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
             glMaterialfv(GL_FRONT,GL_EMISSION,noir);
             break;
    case 8 : glMaterialfv(GL_FRONT,GL_DIFFUSE,noir);
             glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
             glMaterialfv(GL_FRONT,GL_AMBIENT,gris);
             glMaterialfv(GL_FRONT,GL_EMISSION,noir);
             break;
    case 9 : glMaterialfv(GL_FRONT,GL_DIFFUSE,noir);
             glMaterialfv(GL_FRONT,GL_SPECULAR,noir);
             glMaterialfv(GL_FRONT,GL_AMBIENT,noir);
             glMaterialfv(GL_FRONT,GL_EMISSION,gris);
             break; }
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  manipulateurSouris();
  manipulateurClavier();
  activationLumieres();
  configurationLumieres();
  configurationMateriel();
  switch (mode) {
    case 3 : 
    case 4 :
    case 5 :
    case 6 :
    case 7 :
    case 8 :
    case 9 : glPushMatrix();
             glTranslatef(10.0F,0.0F,0.0F);
             glutSolidSphere(2.0,disc,disc);
             glPopMatrix();
    case 0 : 
    case 1 : 
  case 2 : glutSolidSphere(5.0,disc,disc);
             break; }
  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) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 43   : disc++ ;
                  glutPostRedisplay() ;
                  break ;
      case 45   : disc-- ;
                  if ( disc < 3 )
                    disc = 3;
                  glutPostRedisplay() ;
                  break ;
      case 0x0D : mode = (mode+1)%10 ;
                  glutPostRedisplay() ;
                  break ; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(240,160); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Materiel et lumieres");
  myinit(); 
  creationMenuBasique();
  setParametresOrthoBasique(-5.0,9.0,-7.0,7.0,-50.0,50.0);
  setManipulateurDistance(1.0F);
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

RETOUR