Le source : TD-TeapotIllumine.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2003 */
/* Un teapot illumine */
#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"
#include "ModuleMatriceVecteur.h"
static GLfloat shininess = 100.0F ;
static GLfloat angle = 0.0F ;
static int mode = 0;
static int obj = 1;
static float dif[4] = { 1.0F,1.0F,1.0F,1.0F };
static int f1;
static int f2;
static float *spec1;
static float *spec2;
void myinit(void) {
glClearColor(0.5F,0.5F,1.0F,1.0F) ;
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurGrisMoyen());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurGrisMoyen());
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
{ GLfloat l_pos0[] = { 1.0F,1.0F,3.0F,1.0F };
glPushMatrix();
glRotatef(angle,1.0F,0.0F,0.0F);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos0);
glPopMatrix(); }
{ GLfloat l_pos1[] = { -1.0F,-1.0F,3.0F,0.0F };
glPushMatrix();
glRotatef(-angle,1.0F,0.0F,0.0F);
glLightfv(GL_LIGHT1,GL_POSITION,l_pos1);
glPopMatrix(); }
switch ( mode ) {
case 0 : spec1 = couleurNoir();
spec2 = couleurNoir();
break;
case 1 : spec1 = couleurGrisClair();
spec2 = couleurGrisClair();
break;
case 2 : spec1 = couleurVert();
spec2 = couleurBleu();
break; }
glLightfv(GL_LIGHT0,GL_SPECULAR,spec1);
glLightfv(GL_LIGHT1,GL_SPECULAR,spec2);
glMaterialf(GL_FRONT,GL_SHININESS,shininess);
glMaterialfv(GL_FRONT,GL_DIFFUSE,dif);
if ( obj )
glutSolidTeapot(1.3);
else
glutSolidSphere(1.3,360,180);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void postRedisplay(void) {
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
}
void idle(void) {
angle += 1.0F;
postRedisplay();
}
void incrementComposante(float *c) {
*c += 0.01F;
if ( *c > 1.0F )
*c = 1.0F ;
}
void decrementComposante(float *c) {
*c -= 0.01F;
if ( *c < 0.0F )
*c = 0.0F ;
}
void key(unsigned char key,int x,int y) {
static int anim = 0;
if ( keyManipulateur(key,x,y) )
postRedisplay();
else
switch ( key ) {
case '1' : incrementComposante(&dif[0]);
postRedisplay();
break;
case '2' : incrementComposante(&dif[1]);
postRedisplay();
break;
case '3' : incrementComposante(&dif[2]);
postRedisplay();
break;
case '4' : decrementComposante(&dif[0]);
postRedisplay();
break;
case '5' : decrementComposante(&dif[1]);
postRedisplay();
break;
case '6' : decrementComposante(&dif[2]);
postRedisplay();
break;
case 'o' : obj = !obj;
postRedisplay();
break;
case 45 : shininess *= 1.03F ;
postRedisplay();
break;
case 43 : shininess /= 1.03F ;
postRedisplay();
break;
case 0x0D : mode = (mode+1) % 3;
postRedisplay();
break;
case ' ' : anim = !anim;
glutIdleFunc((anim) ? idle : NULL); }
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,-h,0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display2() {
glClearColor(0.5F,0.5F,0.5F,1.0F);
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
float pos = 1.0F;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Diffusion materiau : %6.3f %6.3f %6.3f",dif[0],dif[1],dif[2]) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Reflectivite : %7.3f",shininess) ;
vecteur l_pos0 = { 1.0F,1.0F,3.0F,1.0F };
matrice m;
toRotationX(m,angle);
produitMatriceVecteur(m,l_pos0,l_pos0);
vecteur l_pos1 = { -1.0F,-1.0F,3.0F,1.0F };
toRotationX(m,-angle);
produitMatriceVecteur(m,l_pos1,l_pos1);
pos += 1.0F;
glColor3fv(couleurBlanc());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Position lumiere 1 : %6.3f %6.3f %6.3f",l_pos0[0],l_pos0[1],l_pos0[2]) ;
pos += 1.0F;
glColor3fv(spec1);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Coul speculaire lum 1 : %6.3f %6.3f %6.3f",spec1[0],spec1[1],spec1[2]) ;
pos += 1.0F;
glColor3fv(couleurBlanc());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Position lumiere 2 : %6.3f %6.3f %6.3f",l_pos1[0],l_pos1[1],l_pos1[2]) ;
pos += 1.0F;
glColor3fv(spec2);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Coul speculaire lum 2 : %6.3f %6.3f %6.3f",spec2[0],spec2[1],spec2[2]) ;
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char **argv) {
spec1 = spec2 = couleurNoir();
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(340,230);
glutInitWindowPosition(50,50);
f1 = glutCreateWindow("Lumieres et materiaux en OpenGL");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.5,1.5,-1.5,1.5,-50.0,50.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutInitWindowSize(460,130);
glutInitWindowPosition(60,340);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMainLoop();
return(0);
}