Le source : ReflexionDiffuse.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Juin 2001 */
/* Illustration de la reflexion diffuse */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleDessinArc.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
#include "ModuleMatriceVecteur.h"
static vecteur pl = { 2.2F,1.4F,0.8F,1.0F } ;
static vecteur pi = { 0.2F,0.0F,0.3F } ;
static vecteur n ;
static vecteur l ;
static int f1 ;
static int f2 ;
static float dif ;
static float degres ;
void myinit(void) {
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE) ;
glDepthFunc(GL_LESS);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT1,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT2,GL_AMBIENT,couleurNoir());
float l2[4] = { 0.0F,1.0F,0.0F,0.0F };
glLightfv(GL_LIGHT2,GL_POSITION,l2);
}
void axes() {
glPushMatrix() ;
glColor4fv(couleurJaune()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.25F,0.0F,0.0F) ;
glEnd() ;
placeFontCursor(0.3F,0.05F,0.05F);
simpleBitmapOutput(1,1,"x");
glColor4fv(couleurCyan()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.25F,0.0F) ;
glEnd() ;
placeFontCursor(0.05F,0.3F,0.05F);
simpleBitmapOutput(1,1,"y");
glColor4fv(couleurMagenta()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.0F,0.25F) ;
glEnd() ;
placeFontCursor(0.05F,0.05F,0.3F);
simpleBitmapOutput(1,1,"z");
glPopMatrix() ;
}
void solidHemisphere(double r,int n1,int n2) {
vecteur **p =(vecteur **) calloc(n1,sizeof(vecteur *));
int i;
for ( i = 0 ; i < n1 ; i++ )
p[i] =(vecteur *) calloc(n2,sizeof(vecteur));
for ( i = 0 ; i < n1 ; i++ ) {
double a1 = i * 3.14159 / 2.0 / (n1-1);
double csa1 = cos(a1);
double sna1 = sin(a1);
for ( int j = 0 ; j < n2 ; j++ ) {
double a2 = j * 3.14159 * 2.0 / (n2-1);
double csa2 = cos(a2);
double sna2 = sin(a2);
p[i][j][0] = csa2*csa1;
p[i][j][2] = sna2*csa1;
p[i][j][1] = sna1; } }
glBegin(GL_QUADS);
for ( i = 0 ; i < n1-1 ; i++ ) {
for ( int j = 0 ; j < n2 ; j++ ) {
glNormal3f(p[i+1][j][0],p[i+1][j][1],p[i+1][j][2]);
glVertex3f(r*p[i+1][j][0],r*p[i+1][j][1],r*p[i+1][j][2]);
glNormal3f(p[i+1][(j+1)%n2][0],p[i+1][(j+1)%n2][1],p[i+1][(j+1)%n2][2]);
glVertex3f(r*p[i+1][(j+1)%n2][0],r*p[i+1][(j+1)%n2][1],r*p[i+1][(j+1)%n2][2]);
glNormal3f(p[i][(j+1)%n2][0],p[i][(j+1)%n2][1],p[i][(j+1)%n2][2]);
glVertex3f(r*p[i][(j+1)%n2][0],r*p[i][(j+1)%n2][1],r*p[i][(j+1)%n2][2]);
glNormal3f(p[i][j][0],p[i][j][1],p[i][j][2]);
glVertex3f(r*p[i][j][0],r*p[i][j][1],r*p[i][j][2]); } }
glEnd();
for ( i = 0 ; i < n1 ; i++ )
free(p[i]);
free(p);
}
void solidBaseHemisphere(double r,int n2) {
vecteur *p =(vecteur *) calloc(n2,sizeof(vecteur));
int j;
for ( j = 0 ; j < n2 ; j++ ) {
double a2 = j * 3.14159 * 2.0 / (n2-1);
double csa2 = cos(a2);
double sna2 = sin(a2);
p[j][0] = r*csa2;
p[j][2] = r*sna2;
p[j][1] = 0.0F; }
glBegin(GL_POLYGON);
glNormal3f(0.0F,-1.0F,0.0F);
for ( j = 0 ; j < n2 ; j++ ) {
glVertex3f(p[j][0],p[j][1],p[j][2]); }
glEnd();
free(p);
}
void display(void) {
glClearColor(0.7F,0.7F,0.6F,0.0F);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurNoir());
glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBlanc());
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glTranslatef(0.0F,-0.65F,0.0F);
float lreel[4] = { pl[0],pl[1],pl[2],1.0F };
glLightfv(GL_LIGHT1,GL_POSITION,lreel);
{ glPushMatrix();
glTranslatef(0.4F,1.4F,1.4F);
axes();
glPopMatrix(); }
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,couleurNoir());
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurNoir());
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune());
{ glPushMatrix();
glTranslatef(pl[0],pl[1],pl[2]);
glutSolidSphere(0.06F,20,20);
glPopMatrix(); }
pi[1] = sqrt(1.0F-pi[0]*pi[0]-pi[2]*pi[2]);
n[0] = pi[0];
n[1] = pi[1];
n[2] = pi[2];
normalise(n);
l[0] = pl[0]-pi[0];
l[1] = pl[1]-pi[1];
l[2] = pl[2]-pi[2];
normalise(l);
dif = produitScalaire(l,n);
degres = acos(dif)*180/3.14159;
if ( dif < 0.0F )
dif = 0.0F;
glDisable(GL_LIGHTING);
glColor3fv(couleurBleu());
vecteur pint;
positionIntermediaireSurN(n,l,0.75F,pint);
traceAngleSurN(n,l,20,0.75F,1);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu());
{ glPushMatrix();
glTranslatef(pi[0],pi[1],pi[2]);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu());
glutSolidSphere(0.04F,20,20);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune());
flecheEnVolume(pl[0]-pi[0],pl[1]-pi[1],pl[2]-pi[2],0.03F,0.1F,0.01F);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge());
flecheEnVolume(l[0],l[1],l[2],0.03F,0.1F,0.012F);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurMagenta());
flecheEnVolume(pi[0],pi[1],pi[2],0.03F,0.1F,0.01F);
if ( dif > 0 ) {
glDisable(GL_LIGHTING);
vecteur axe = { n[2],0.0F,-n[0] };
glColor3fv(couleurVertFonce());
vecteur nd = { n[0]*dif,n[1]*dif,n[2]*dif };
for ( int i = 0 ; i < 360 ; i += 36 )
for ( int j = 30 ; j <= 90 ; j += 30 ) {
glPushMatrix();
glRotatef(i,n[0],n[1],n[2]);
glRotatef(j,axe[0],axe[1],axe[2]);
flecheEnVolume(nd[0],nd[1],nd[2],0.004F,0.01F,0.002F);
glPopMatrix(); }
glEnable(GL_LIGHTING); }
glPopMatrix(); }
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurNoir());
glDisable(GL_LIGHT2);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVertFonce());
solidHemisphere(1.0,50,50);
solidBaseHemisphere(1.0,50);
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
glColor3fv(couleurBleu());
placeFontCursor(pint[0],pint[1],pint[2]) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,SYMBOL,"q") ;
deplacementCursor(20,0,0) ;
simpleBitmapOutput(1,REGULAR8x13," = %6.2f",degres) ;
glColor3fv(couleurJaune());
placeFontCursor(pl[0],pl[1],pl[2]) ;
setAlignement(CENTER);
deplacementCursor(0,15,0) ;
simpleBitmapOutput(1,REGULAR8x13,"LUMIERE") ;
deplacementCursor(0,35,0) ;
simpleBitmapOutput(1,REGULAR8x13,"PONCTUELLE") ;
glColor3fv(couleurMagenta());
placeFontCursor(2*pi[0],2*pi[1],2*pi[2]) ;
deplacementCursor(15,5,0) ;
simpleBitmapOutput(1,REGULAR8x13,"N") ;
deplacementCursor(15,0,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glColor3fv(couleurRouge());
placeFontCursor(pi[0]+l[0],pi[1]+l[1],pi[2]+l[2]) ;
deplacementCursor(5,-15,0) ;
simpleBitmapOutput(1,REGULAR8x13,"L") ;
deplacementCursor(5,-20,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
setAlignement(LEFT);
glPopMatrix();
glFlush();
glutPostWindowRedisplay(f2);
glutSwapBuffers();
}
void key2(unsigned char key,int x,int y) {
switch ( key ) {
case 'a' : pl[0] += 0.02F;
glutPostWindowRedisplay(f1);
break;
case 'A' : pl[0] -= 0.02F;
glutPostWindowRedisplay(f1);
break;
case 'q' : pl[1] += 0.02F;
glutPostWindowRedisplay(f1);
break;
case 'Q' : pl[1] -= 0.02F;
glutPostWindowRedisplay(f1);
break;
case 'w' : pl[2] += 0.02F;
glutPostWindowRedisplay(f1);
break;
case 'W' : pl[2] -= 0.02F;
glutPostWindowRedisplay(f1);
break;
case '4' : { float px = pi[0] - 0.02F;
if ( px*px+pi[2]*pi[2] < 1.0F )
pi[0] = px ;
glutPostWindowRedisplay(f1); }
break;
case '6' : { float px = pi[0] + 0.02F;
if ( px*px+pi[2]*pi[2] < 1.0F )
pi[0] = px ;
glutPostWindowRedisplay(f1); }
break;
case '2' : { float pz = pi[2] - 0.02F;
if ( pz*pz+pi[0]*pi[0] < 1.0F )
pi[2] = pz ;
glutPostWindowRedisplay(f1); }
break;
case '8' : { float pz = pi[2] + 0.02F;
if ( pz*pz+pi[0]*pi[0] < 1.0F )
pi[2] = pz ;
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) {
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
float pos = 1.0F;
glColor4fv(couleurJaune());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"LUMIERE : %6.3f %6.3f %6.3f",pl[0],pl[1],pl[2]) ;
pos += 1.0F;
glColor4fv(couleurBleu());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"INTERSECTION : %6.3f %6.3f %6.3f",pi[0],pi[1],pi[2]) ;
pos += 1.0F;
glColor4fv(couleurMagenta());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"NORMALE : %6.3f %6.3f %6.3f",n[0],n[1],n[2]) ;
pos += 1.0F;
glColor4fv(couleurRouge());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"L : %6.3f %6.3f %6.3f",l[0],l[1],l[2]) ;
pos += 1.0F;
glColor4fv(couleurVert());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"DIFFUSION : %6.3f",dif) ;
pos += 1.0F;
glColor4fv(couleurVert());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"THETA : %7.3f",degres) ;
glPopMatrix();
glFlush();
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_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowPosition(10,10);
glutInitWindowSize(400,270);
f1 = glutCreateWindow("La reflexion diffuse");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.1F,1.1F,-1.1F,1.1F,-50.0,50.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(20.0F,-60.0F,0.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutInitWindowSize(370,130);
glutInitWindowPosition(60,360);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key2);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}
/* ************************************************** */