L'exécutable

Des objets affectés d'un matériel pouvant être paramétré:

Le source: ParamMaterial.cpp

/* Auteur: Nicolas JANEY         */
/* nicolas.janey@univ-fcomte.fr  */
/* Decembre 2001                 */
/* Les parametres de material    */

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

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

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

static float diffuse0[4] = { 0.5F,0.5F,0.7F,1.0F };
static float ambiant0[4] = { 0.1F,0.1F,0.3F,1.0F };
static float specular0[4] = { 0.4F,0.4F,0.0F,1.0F };
static float emission0[4] = { 0.0F,0.0F,0.0F,1.0F };
static float shininess0[1] = { 40.0F };

static float diffuse1[4] = { 0.8F,0.8F,0.8F,1.0F };
static float ambiant1[4] = { 0.0F,0.0F,0.0F,1.0F };
static float specular1[4] = { 0.0F,0.0F,0.0F,1.0F };
static float emission1[4] = { 0.0F,0.0F,0.0F,1.0F };
static float shininess1[1] = { 0.0F };

static float diffuse2[4] = { 0.0F,0.0F,0.0F,1.0F };
static float ambiant2[4] = { 0.8F,0.8F,0.8F,1.0F };
static float specular2[4] = { 0.0F,0.0F,0.0F,1.0F };
static float emission2[4] = { 0.0F,0.0F,0.0F,1.0F };
static float shininess2[1] = { 0.0F };

static float diffuse3[4] = { 0.0F,0.0F,0.0F,1.0F };
static float ambiant3[4] = { 0.0F,0.0F,0.0F,1.0F };
static float specular3[4] = { 1.0F,0.0F,0.0F,1.0F };
static float emission3[4] = { 0.0F,0.0F,0.0F,1.0F };
static float shininess3[1] = { 45.0F };

static float diffuse4[4] = { 0.0F,0.0F,0.0F,1.0F };
static float ambiant4[4] = { 0.0F,0.0F,0.0F,1.0F };
static float specular4[4] = { 1.0F,0.0F,0.0F,1.0F };
static float emission4[4] = { 0.0F,0.0F,0.0F,1.0F };
static float shininess4[1] = { 5.0F };

static float diffuse5[4] = { 0.0F,0.0F,0.0F,1.0F };
static float ambiant5[4] = { 0.0F,0.0F,0.0F,1.0F };
static float specular5[4] = { 0.0F,0.0F,0.0F,1.0F };
static float emission5[4] = { 1.0F,1.0F,0.0F,1.0F };
static float shininess5[1] = { 0.0F };

static float diffuse6[4] = { 0.3F,0.0F,0.6F,1.0F };
static float ambiant6[4] = { 0.1F,0.3F,0.0F,1.0F };
static float specular6[4] = { 1.0F,0.7F,0.0F,1.0F };
static float emission6[4] = { 0.0F,0.0F,0.1F,1.0F };
static float shininess6[1] = { 111.0F };

static float diffuse[4];
static float ambiant[4];
static float specular[4];
static float emission[4];
static float shininess[1];

static float *tp[5] = { shininess,emission,specular,ambiant,diffuse };
static float *tc[7] = { couleurBlanc(),couleurMagenta(),couleurCyan(),
                        couleurJaune(),couleurRouge(),couleurVert(),couleurBleu() };
static float posLum[4] = { 0.5F,0.5F,3.0F,1.0F };
static float noir[4] = { 0.0F,0.0F,0.0F,1.0F };
static int param = 4;
static int pparam = 0;
static int nbparam[5] = { 1,3,3,3,3 };
static int f1;
static int f2;
static int disc = 60;
static int aff = 0;
static int cc = 0;

void affecteMaterial(float *d,float *a,float *s,float *e,float *sh){
  for ( int i = 0 ; i < 4 ; i++ ) {
    diffuse[i] = d[i];
    ambiant[i] = a[i];
    specular[i] = s[i];
    emission[i] = e[i]; }
  shininess[0] = sh[0];
}

void init(void) {
  setManipulateurClavierAngle(20.0F,30.0F,0.0F);
  affecteMaterial(diffuse0,ambiant0,specular0,emission0,shininess0);
}

void display1() {
  glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  manipulateurClavier();
  glLightfv(GL_LIGHT0,GL_DIFFUSE,tc[cc]);
  glLightfv(GL_LIGHT0,GL_AMBIENT,tc[cc]);
  glLightfv(GL_LIGHT0,GL_SPECULAR,tc[cc]);
  glLightfv(GL_LIGHT0,GL_POSITION,posLum);
  glEnable(GL_LIGHTING);
  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse);
  glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambiant);
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular);
  glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emission);
  glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shininess);
  manipulateurSouris();
  switch(aff) {
    case 0 : glutSolidTorus(0.4,0.9,disc,disc) ;
             break;
    case 1 : glutSolidSphere(1.2,disc,disc) ;
             break;
    case 2 : glNormal3f(0.0F,0.0F,1.0F);
             glBegin(GL_QUADS);
             for ( int i = 0 ; i < disc ; i++ )
               for ( int j = 0 ; j < disc ; j++ ) {
                 glVertex2f(-1.0F+i*2.0F/disc,-1.0F+j*2.0F/disc);
                 glVertex2f(-1.0F+(i+1)*2.0F/disc,-1.0F+j*2.0F/disc);
                 glVertex2f(-1.0F+(i+1)*2.0F/disc,-1.0F+(j+1)*2.0F/disc);
                 glVertex2f(-1.0F+i*2.0F/disc,-1.0F+(j+1)*2.0F/disc); }
             glEnd(); }
  glPopMatrix();
  glDisable(GL_LIGHTING);
  glutSwapBuffers();
}

void display2() {
  glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  glColor4fv(couleurBlanc());
  glBegin(GL_LINES);
  glVertex2f(2.0F,0.0F);
  glVertex2f(333.0F,0.0F);
  glEnd();
  glColor4fv(couleurJaune());
  glBegin(GL_LINE_LOOP);
  glVertex2f(2.0F,5.0F+param*20.0F);
  glVertex2f(333.0F,5.0F+param*20.0F);
  glVertex2f(333.0F,25.0F+param*20.0F);
  glVertex2f(2.0F,25.0F+param*20.0F);
  glEnd();
  glColor4fv(couleurRouge());
  glBegin(GL_LINE_LOOP);
  glVertex2f(122.0F+70.0*pparam,6.0F+param*20.0F);
  glVertex2f(186.0F+70.0*pparam,6.0F+param*20.0F);
  glVertex2f(186.0F+70.0*pparam,24.0F+param*20.0F);
  glVertex2f(122.0F+70.0*pparam,24.0F+param*20.0F);
  glEnd();
  glPopMatrix();
  glPushMatrix();
  float pos = 0;
  glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"SHININESS : %6.2f",shininess[0]) ;
  pos = 1;
  glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"EMISSION  : %6.2f %6.2f %6.2f",emission[0],emission[1],emission[2]) ;
  pos = 2;
  glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"SPECULAR  : %6.2f %6.2f %6.2f",specular[0],specular[1],specular[2]) ;
  pos = 3;
  glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"AMBIENT   : %6.2f %6.2f %6.2f",ambiant[0],ambiant[1],ambiant[2]) ;
  pos = 4;
  glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"DIFFUSE   : %6.2f %6.2f %6.2f",diffuse[0],diffuse[1],diffuse[2]) ;
  pos = -4.5F;
  glColor4fv(couleurBlanc());
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"POSITION  : %6.2f %6.2f %6.2f",posLum[0],posLum[1],posLum[2]) ;
  pos = -3.5F;
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"SPECULAR  : %6.2f %6.2f %6.2f",tc[cc][0],tc[cc][1],tc[cc][2]) ;
  pos = -2.5F;
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"AMBIENT   : %6.2f %6.2f %6.2f",tc[cc][0],tc[cc][1],tc[cc][2]) ;
  pos = -1.5F;
  placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
  simpleBitmapOutput(REGULAR8x13,"DIFFUSE   : %6.2f %6.2f %6.2f",tc[cc][0],tc[cc][1],tc[cc][2]) ;
  glPopMatrix();
  glutSwapBuffers();
}

void augmente(float *v,float dif,float max) {
  *v += dif;
  if ( *v > max )
    *v = max;
}

void diminue(float *v,float dif,float min) {
  *v -= dif;
  if ( *v < min )
    *v = min;
}

void augmenteCoef(float *v) {
  *v += 0.01F;
  if ( *v > 1.0F )
    *v = 1.0F;
}

void diminueCoef(float *v) {
  *v -= 0.01F;
  if ( *v < 0.0F )
    *v = 0.0F;
}

void special2(int k,int x,int y) {
  switch (k) {
    case GLUT_KEY_PAGE_DOWN : switch (param) {
                                case 4 :
                                case 3 :
                                case 2 :
                                case 1 : diminue(&tp[param][pparam],0.1F,0.0F);
                                         break;
                                case 0 : diminue(&tp[param][pparam],1.0F,0.0F);
                                         break; }
                              glutPostWindowRedisplay(f2);
                              glutPostWindowRedisplay(f1);
                              break;
    case GLUT_KEY_PAGE_UP   : switch (param) {
                                case 4 :
                                case 3 :
                                case 2 :
                                case 1 : augmente(&tp[param][pparam],0.1F,1.0F);
                                         break;
                                case 0 : augmente(&tp[param][pparam],1.0F,128.0F);
                                         break; }
                              glutPostWindowRedisplay(f2);
                              glutPostWindowRedisplay(f1);
                              break;
    case GLUT_KEY_F1        : affecteMaterial(diffuse0,ambiant0,specular0,emission0,shininess0);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F2        : affecteMaterial(diffuse1,ambiant1,specular1,emission1,shininess1);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F3        : affecteMaterial(diffuse2,ambiant2,specular2,emission2,shininess2);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F4        : affecteMaterial(diffuse3,ambiant3,specular3,emission3,shininess3);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F5        : affecteMaterial(diffuse4,ambiant4,specular4,emission4,shininess4);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F6        : affecteMaterial(diffuse5,ambiant5,specular5,emission5,shininess5);
                              aff = 0;
                              setManipulateurClavierAngle(20.0F,30.0F,0.0F);
                              setManipulateurSourisAngle(0.0F,0.0F,0.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_F7        : affecteMaterial(diffuse6,ambiant6,specular6,emission6,shininess6);
                              aff = 2;
                              setManipulateurClavierAngle(2.0F,2.0F,2.0F);
                              setManipulateurSourisAngle(1.0F,2.0F,3.0F);
                              glutPostWindowRedisplay(f1);
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_UP        : param = (param+1)%5;
                              pparam = pparam%nbparam[param];
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_DOWN      : param = (param+4)%5;
                              pparam = pparam%nbparam[param];
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_RIGHT     : pparam = (pparam+1)%nbparam[param];
                              glutPostWindowRedisplay(f2);
                              break;
    case GLUT_KEY_LEFT      : pparam = (pparam+nbparam[param]-1)%nbparam[param];
                              glutPostWindowRedisplay(f2);
                              break; }
}

void special1(int k,int x,int y) {
  if ( specialManipulateur(k,x,y) )
    glutPostWindowRedisplay(f1);
    else
    special2(k,x,y);
}

void key2(unsigned char key,int x,int y) {
  switch ( key ) {
    case '8'    : special2(GLUT_KEY_UP,x,y);
                  break;
    case '2'    : special2(GLUT_KEY_DOWN,x,y);
                  break;
    case '4'    : special2(GLUT_KEY_LEFT,x,y);
                  break;
    case '6'    : special2(GLUT_KEY_RIGHT,x,y);
                  break;
    case '9'    : special2(GLUT_KEY_PAGE_UP,x,y);
                  break;
    case '3'    : special2(GLUT_KEY_PAGE_DOWN,x,y);
                  break;
    case 43     : switch (param) {
                    case 4 :
                    case 3 :
                    case 2 :
                    case 1 : augmente(&tp[param][pparam],0.01F,1.0F);
                             break;
                    case 0 : augmente(&tp[param][pparam],0.1F,128.0F);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 45     : switch (param) {
                    case 4 :
                    case 3 :
                    case 2 :
                    case 1 : diminue(&tp[param][pparam],0.01F,0.0F);
                             break;
                    case 0 : diminue(&tp[param][pparam],0.1F,0.0F);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case '0'    : switch ( param ) {
                    case 4 : diffuse[0] = diffuse[1] = diffuse[2] = 0.0F;
                             break;
                    case 3 : ambiant[0] = ambiant[1] = ambiant[2] = 0.0F;
                             break;
                    case 2 : specular[0] = specular[1] = specular[2] = 0.0F;
                             break;
                    case 1 : emission[0] = emission[1] = emission[2] = 0.0F;
                             break;
                    case 0 : shininess[0] = 0.0F; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case '1'    : switch ( param ) {
                    case 4 : diffuse[0] = diffuse[1] = diffuse[2] = 1.0F;
                             break;
                    case 3 : ambiant[0] = ambiant[1] = ambiant[2] = 1.0F;
                             break;
                    case 2 : specular[0] = specular[1] = specular[2] = 1.0F;
                             break;
                    case 1 : emission[0] = emission[1] = emission[2] = 1.0F;
                             break;
                    case 0 : shininess[0] = 128.0F; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'r'    : switch ( param ) {
                    case 4 : augmenteCoef(&diffuse[0]);
                             break;
                    case 3 : augmenteCoef(&ambiant[0]);
                             break;
                    case 2 : augmenteCoef(&specular[0]);
                             break;
                    case 1 : augmenteCoef(&emission[0]); }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'R'    : switch ( param ) {
                    case 4 : diminueCoef(&diffuse[0]);
                             break;
                    case 3 : diminueCoef(&ambiant[0]);
                             break;
                    case 2 : diminueCoef(&specular[0]);
                             break;
                    case 1 : diminueCoef(&emission[0]); }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'v'    : switch ( param ) {
                    case 4 : augmenteCoef(&diffuse[1]);
                             break;
                    case 3 : augmenteCoef(&ambiant[1]);
                             break;
                    case 2 : augmenteCoef(&specular[1]);
                             break;
                    case 1 : augmenteCoef(&emission[1]);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'V'    : switch ( param ) {
                    case 4 : diminueCoef(&diffuse[1]);
                             break;
                    case 3 : diminueCoef(&ambiant[1]);
                             break;
                    case 2 : diminueCoef(&specular[1]);
                             break;
                    case 1 : diminueCoef(&emission[1]);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'b'    : switch ( param ) {
                    case 4 : augmenteCoef(&diffuse[2]);
                             break;
                    case 3 : augmenteCoef(&ambiant[2]);
                             break;
                    case 2 : augmenteCoef(&specular[2]);
                             break;
                    case 1 : augmenteCoef(&emission[2]);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'B'    : switch ( param ) {
                    case 4 : diminueCoef(&diffuse[2]);
                             break;
                    case 3 : diminueCoef(&ambiant[2]);
                             break;
                    case 2 : diminueCoef(&specular[2]);
                             break;
                    case 1 : diminueCoef(&emission[2]);
                             break; }
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 's'    : shininess[0] += 0.1F;
                  if ( shininess[0] > 128.0F )
                    shininess[0] = 128.0F;
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'S'    : shininess[0] -= 0.1F;
                  if ( shininess[0] < 0.0F )
                    shininess[0] = 0.0F;
                  glutPostWindowRedisplay(f2);
                  glutPostWindowRedisplay(f1);
                  break;
    case 'd'    : disc += 5;
                  glutPostWindowRedisplay(f1);
                  break;
    case 'D'    : disc -= 5;
                  if ( disc < 5 )
                    disc = 5;
                  glutPostWindowRedisplay(f1);
                  break;
    case ' '    : cc = (cc+1)%7;
                  glutPostWindowRedisplay(f1);
                  glutPostWindowRedisplay(f2);
                  break;
    case 0x0D   : aff = (aff+1)%3;
                  glutPostWindowRedisplay(f1);
                  break;
    case 0x1B   : exit(0);
                  break; }
}

void key1(unsigned char ky,int x,int y) {
  if ( keyManipulateur(ky,x,y) )
    glutPostWindowRedisplay(f1);
    else
    key2(ky,x,y);
}

void reshape2(int w,int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0,w,110-h,110,-1.0,1.0); 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void myinit(void) {
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_NORMALIZE);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHT0);
  glLightModelfv(GL_LIGHT_MODEL_AMBIENT,noir);
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  init();
  glutInitWindowSize(250,200);
  glutInitWindowPosition(30,50);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  f1 = glutCreateWindow("Le materiel");
  myinit();
  creationMenuBasique();
  setParametresOrthoBasique(-1.5,1.5,-1.5,1.5,-500.0,500.0);
  setManipulateurDistance(1.0F);
  glutReshapeFunc(reshapeOrthoBasique);
  glutDisplayFunc(display1);
  glutKeyboardFunc(key1);
  glutSpecialFunc(special1);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutInitWindowSize(335,200);
  glutInitWindowPosition(50,290);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  f2 = glutCreateWindow("Valeurs");
  myinit();
  creationMenuBasique();
  glutDisplayFunc(display2);
  glutReshapeFunc(reshape2);
  glutKeyboardFunc(key2);
  glutSpecialFunc(special2);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

RETOUR