Le source : LumiereSpeculaire.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* La lumiere speculaire en OpenGL */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleFleche.h"
#include "ModuleFont.h"
#include "ModuleManipulateur.h"
#include "ModuleMatriceVecteur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static GLfloat px=0.0F ;
static GLfloat pz=1.0F ;
static int f1 ;
static int f2 ;
static float scal ;
static vecteur r ;
static vecteur e ;
static vecteur i ;
static float angle ;
static int disc = 50;
static float baseColor = 0.25F;
static float shininess = 5.0F;
static float fact = 0.75F;
void myinit(void) {
GLfloat light_position[] = { 0.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
float reflexionSpeculaire(vecteur i,vecteur r,vecteur e,float *angle,float px,float pz) {
float scal;
i[0] = px+9.0F;
i[1] = -11.0F;
i[2] = pz;
normalise(i);
r[0] = px+9.0F;
r[1] = 11.0F;
r[2] = pz;
normalise(r);
e[0] = 8.0F-px;
e[1] = 11.0F;
e[2] = -pz;
normalise(e) ;
scal = produitScalaire(r,e);
*angle = acos(scal)*180/3.14159 ;
if ( scal < 0.0F )
scal = 0.0F;
scal = pow(scal,shininess);
return(scal);
}
float reflexionSpeculaire(float px,float pz) {
float scal;
vecteur i;
i[0] = px+9.0F;
i[1] = -11.0F;
i[2] = pz;
normalise(i);
vecteur r;
r[0] = px+9.0F;
r[1] = 11.0F;
r[2] = pz;
normalise(r);
vecteur e;
e[0] = 8.0F-px;
e[1] = 11.0F;
e[2] = -pz;
normalise(e) ;
scal = produitScalaire(r,e);
if ( scal < 0.0F )
scal = 0.0F;
scal = pow(scal,shininess);
return(scal);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
scal = reflexionSpeculaire(i,r,e,&angle,px,pz);
manipulateurSouris();
manipulateurClavier();
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune()) ;
glTranslatef(-9.0F,5.0F,0.0F) ;
glutSolidSphere(0.5,30,30);
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert()) ;
glTranslatef(8.0F,5.0F,0.0F) ;
glutSolidSphere(0.5,30,30);
glPopMatrix();
glPushMatrix();
glDisable(GL_LIGHTING);
glColor4fv(couleurJaune()) ;
placeFontCursor(-4.5F+px/2,0.0F,pz/2) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"I") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glPushMatrix();
glTranslatef(-9.0F,5.0F,0.0F);
flecheEnVolume(px+9.0F,-11.0F,pz,0.35F,1.5F,0.07F);
glPopMatrix();
glColor4fv(couleurRouge()) ;
placeFontCursor(5.5F+px/2,0.0F,3*pz/2) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"R") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glPushMatrix();
glTranslatef(px,-6.0F,pz);
flecheEnVolume(px+9.0F,11.0F,pz,0.35F,1.5F,0.07F);
glPopMatrix();
glColor4fv(couleurBleu()) ;
placeFontCursor(px,1.0F,pz) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"N") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glPushMatrix();
glTranslatef(px,-6.0F,pz);
flecheEnVolume(0.0F,14.0F,0.0F,0.35F,1.5F,0.07F);
glPopMatrix();
glColor4fv(couleurVert()) ;
placeFontCursor(2.5F+px/2,1.0F,pz/2) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"E") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glPushMatrix();
glTranslatef(8.0F,5.0F,0.0F);
flecheEnVolume(px-8.0F,-11.0F,pz,0.35F,1.5F,0.07F);
glPopMatrix();
glPopMatrix();
glBegin(GL_QUADS);
for ( int i = 0 ; i < disc ; i++ )
for ( int j = 0 ; j < disc ; j++ ) {
float xi = -10.0F+i*20.0F/disc ;
float xf = -10.0F+(i+1)*20.0F/disc ;
float yi = -10.0F+j*20.0F/disc ;
float yf = -10.0F+(j+1)*20.0F/disc ;
float f = reflexionSpeculaire(xi,yi)*fact;
glColor3f(baseColor+f,baseColor+f,baseColor+f);
glVertex3f(xi,-6.0F,yi) ;
f = reflexionSpeculaire(xf,yi)*fact;
glColor3f(baseColor+f,baseColor+f,baseColor+f);
glVertex3f(xf,-6.0F,yi) ;
f = reflexionSpeculaire(xf,yf)*fact;
glColor3f(baseColor+f,baseColor+f,baseColor+f);
glVertex3f(xf,-6.0F,yf) ;
f = reflexionSpeculaire(xi,yf)*fact;
glColor3f(baseColor+f,baseColor+f,baseColor+f);
glVertex3f(xi,-6.0F,yf) ; }
glEnd();
glPopMatrix();
glFlush();
glutPostWindowRedisplay(f2);
glutSwapBuffers();
}
void key2(unsigned char key,int x,int y) {
switch ( key ) {
case 43 : disc++ ;
glutPostWindowRedisplay(f1);
break;
case 45 : disc-- ;
if ( disc < 2 )
disc = 2;
glutPostWindowRedisplay(f1);
break;
case 's' : shininess /= 1.05F;
glutPostWindowRedisplay(f1);
break;
case 'S' : shininess *= 1.05F;
glutPostWindowRedisplay(f1);
break;
case 'f' : fact *= 1.05F;
glutPostWindowRedisplay(f1);
break;
case 'F' : fact /= 1.05F;
glutPostWindowRedisplay(f1);
break;
case '4' : px -= 0.1F ;
if ( px < -6.0F )
px = -6.0F ;
glutPostWindowRedisplay(f1);
break;
case '6' : px += 0.1F ;
if ( px > 6.0F )
px = 6.0F ;
glutPostWindowRedisplay(f1);
break;
case '8' : pz -= 0.1F ;
if ( pz < -6.0F )
pz = -6.0F ;
glutPostWindowRedisplay(f1);
break;
case '2' : pz += 0.1F ;
if ( pz > 6.0F )
pz = 6.0F ;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0);
break; }
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostWindowRedisplay(f1);
else
key2(key,x,y);
}
void display2(void) {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glPushMatrix();
float pos = 1.0F;
glColor4fv(couleurJaune());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"INCIDENT : %6.3f %6.3f %6.3f",i[0],i[1],i[2]) ;
pos += 1.0F;
glColor4fv(couleurRouge());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"REFLEXION : %6.3f %6.3f %6.3f",r[0],r[1],r[2]) ;
pos += 1.0F;
glColor4fv(couleurBleu());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"NORMALE : %6.3f %6.3f %6.3f",0.0F,1.0F,0.0F) ;
pos += 1.0F;
glColor4fv(couleurVert());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"OBSERVATEUR : %6.3f %6.3f %6.3f",-e[0],-e[1],-e[2]) ;
pos += 1.0F;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"ANGLE : %f degres",angle);
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"SHININESS : %f",shininess);
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"COEFFICIENT : %f",fact);
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"REFLEXION SPECULAIRE : %f",scal);
glPopMatrix();
glutSwapBuffers();
}
void special(int key,int x,int y) {
if ( specialManipulateur(key,x,y) ) {
glutPostWindowRedisplay(f1); }
}
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();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,250);
glutInitWindowPosition(50,50);
f1 = glutCreateWindow("Réflexion spéculaire");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-11.0,11.0,-11.0,11.0,-50.0,50.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(20.0F,30.0F,0.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutInitWindowSize(360,170);
glutInitWindowPosition(60,340);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key2);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}