Le source : DiffusionSousLumiereDirectionnelle.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Janvier 2005 */
/* Calcul de la quantite de lumiere diffusee */
/* par une surface eclairee */
/* par une lumiere direcionnelle */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ModuleCouleurs.h"
#include "ModuleFleche.h"
#include "ModuleFont.h"
#include "ModuleMatriceVecteur.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
typedef struct Vecteur {
float x;
float y;
float z;
float t; } Vecteur;
typedef struct Position {
float x;
float y;
float z;
float t; } Position;
typedef struct Couleur {
float r;
float v;
float b;
float a; } Couleur;
typedef struct Energie {
float r;
float v;
float b; } Energie;
typedef struct LumiereDirectionnelle {
Vecteur dir;
Couleur coul;
float energie; } LumiereDirectionnelle;
typedef struct Materiel {
Couleur kd; } Materiel;
static Vecteur n ;
static int f1;
static int f2;
static float nRx = 0.0F;
static float nRy = 0.0F;
static float nRz = 0.0F;
static float lRx = 0.0F;
static float lRy = 0.0F;
static float lRz = 0.0F;
static Position p = { -0.1F,0.1F,0.1F,1.0F } ;
static Position po = { 0.5F,1.2F,0.0F,1.0F } ;
static LumiereDirectionnelle ld = { { 0.0F,1.0F,0.0F },
{ 1.0F,0.5F,0.5F,1.0F },
1.0F } ;
static Materiel mt = { 0.8F,1.0F,0.6F,1.0F };
static Energie e;
void myinit(void) {
GLfloat light_Position0[] = { 1.0F,0.0F,1.0F,0.0F };
GLfloat light_Position1[] = { -1.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
glLightfv(GL_LIGHT0,GL_POSITION,light_Position0);
glLightfv(GL_LIGHT1,GL_POSITION,light_Position1);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
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);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
}
float produitScalaire(Vecteur *v1,Vecteur *v2) {
return(v1->x*v2->x + v1->y*v2->y + v1->z*v2->z);
}
void lumiereDiffusee(Vecteur *n,LumiereDirectionnelle *ld,Materiel *m,Energie *e) {
float ps = produitScalaire(n,&ld->dir);
if ( ps < 0.0F )
e->r = e->v = e->b = 0.0F;
else {
e->r = m->kd.r * ps * ld->energie * ld->coul.r;
e->v = m->kd.v * ps * ld->energie * ld->coul.v;
e->b = m->kd.b * ps * ld->energie * ld->coul.b; }
}
void display(void) {
{ vecteur N = { 0.0F,1.0F,0.0F,0.0F };
matrice mRx;
toRotationX(mRx,nRx) ;
produitMatriceVecteur(mRx,N,N) ;
matrice mRy;
toRotationY(mRy,nRy) ;
produitMatriceVecteur(mRy,N,N) ;
matrice mRz;
toRotationZ(mRz,nRz) ;
produitMatriceVecteur(mRz,N,N) ;
n.x = N[0];
n.y = N[1];
n.z = N[2];
n.t = 0.0F; }
{ vecteur L = { 0.0F,1.0F,0.0F,0.0F };
matrice mRx;
toRotationX(mRx,lRx) ;
produitMatriceVecteur(mRx,L,L) ;
matrice mRy;
toRotationY(mRy,lRy) ;
produitMatriceVecteur(mRy,L,L) ;
matrice mRz;
toRotationZ(mRz,lRz) ;
produitMatriceVecteur(mRz,L,L) ;
ld.dir.x = L[0];
ld.dir.y = L[1];
ld.dir.z = L[2];
ld.dir.t = 0.0F; }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix() ;
glTranslatef(0.0F,-0.2F,0.0F);
manipulateurSouris();
manipulateurClavier();
glPushMatrix();
glTranslatef(p.x,p.y,p.z) ;
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,(float *) &mt.kd) ;
glEnable(GL_CULL_FACE);
glutSolidSphere(0.05,10,10);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glPopMatrix();
glColor4fv((float *) &mt.kd) ;
placeFontCursor(n.x/2.0F,n.y/2.0F,n.z/2.0F) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"N") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,(float *) &mt.kd) ;
flecheEnVolume(n.x,n.y,n.z,0.04F,0.15F,0.01F);
glDisable(GL_LIGHTING);
glPopMatrix();
glPushMatrix();
glTranslatef(po.x,po.y,po.z) ;
glColor4fv((float *) &ld.coul) ;
placeFontCursor(-ld.dir.x/2.0F,-ld.dir.y/2.0F,-ld.dir.z/2.0F) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"L") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,(float *) &ld.coul) ;
flecheEnVolume(-ld.dir.x,-ld.dir.y,-ld.dir.z,0.04F,0.15F,0.01F);
glPopMatrix();
glDisable(GL_LIGHTING);
lumiereDiffusee(&n,&ld,&mt,&e);
glPushMatrix();
glColor4f(e.r,e.v,e.b,0.5F) ;
glTranslatef(p.x,p.y,p.z) ;
glRotatef(nRz,0.0F,0.0F,1.0F);
glRotatef(nRy,0.0F,1.0F,0.0F);
glRotatef(nRx,1.0F,0.0F,0.0F);
glScalef(1.6F,0.00001F,1.4F) ;
glEnable(GL_CULL_FACE);
glutSolidCube(1.0) ;
glDisable(GL_CULL_FACE);
glPopMatrix();
glPopMatrix() ;
glDisable(GL_DEPTH_TEST);
glFlush();
glutSwapBuffers() ;
glutPostWindowRedisplay(f2);
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '1' : ld.coul.r -= 0.02F ;
if ( ld.coul.r < 0.0F )
ld.coul.r = 0.0F;
glutPostWindowRedisplay(f1);
break;
case '4' : ld.coul.r += 0.02F ;
if ( ld.coul.r > 1.0F )
ld.coul.r = 1.0F;
glutPostWindowRedisplay(f1);
break;
case '2' : ld.coul.v -= 0.02F ;
if ( ld.coul.v < 0.0F )
ld.coul.v = 0.0F;
glutPostWindowRedisplay(f1);
break;
case '5' : ld.coul.v += 0.02F ;
if ( ld.coul.v > 1.0F )
ld.coul.v = 1.0F;
glutPostWindowRedisplay(f1);
break;
case '3' : ld.coul.b -= 0.02F ;
if ( ld.coul.b < 0.0F )
ld.coul.b = 0.0F;
glutPostWindowRedisplay(f1);
break;
case '6' : ld.coul.b += 0.02F ;
if ( ld.coul.b > 1.0F )
ld.coul.b = 1.0F;
glutPostWindowRedisplay(f1);
break;
case 'x' : nRx += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'X' : nRx -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'y' : nRy += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'Y' : nRy -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'z' : nRz += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'Z' : nRz -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0);
break; }
}
void special2(int key,int x,int y) {
switch (key) {
case GLUT_KEY_F7 : mt.kd.r -= 0.02F ;
if ( mt.kd.r < 0.0F )
mt.kd.r = 0.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F8 : mt.kd.r += 0.02F ;
if ( mt.kd.r > 1.0F )
mt.kd.r = 1.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F9 : mt.kd.v -= 0.02F ;
if ( mt.kd.v < 0.0F )
mt.kd.v = 0.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F10 : mt.kd.v += 0.02F ;
if ( mt.kd.v > 1.0F )
mt.kd.v = 1.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F11 : mt.kd.b -= 0.02F ;
if ( mt.kd.b < 0.0F )
mt.kd.b = 0.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F12 : mt.kd.b += 0.02F ;
if ( mt.kd.b > 1.0F )
mt.kd.b = 1.0F;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F1 : lRx -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F2 : lRx += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F3 : lRy -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F4 : lRy += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F5 : lRz -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F6 : lRz += 2.0F ;
glutPostWindowRedisplay(f1);
break; }
}
void special(int key,int x,int y) {
if ( specialManipulateur(key,x,y) ) {
glutPostWindowRedisplay(f1); }
else
special2(key,x,y);
}
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() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glPushMatrix();
float pos = 1.0F;
glColor4fv((float *) &mt.kd);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"NORMALE : %6.3f %6.3f %6.3f",n.x,n.y,n.z) ;
pos += 1.0F;
glColor4fv((float *) &mt.kd);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"KD : %6.3f %6.3f %6.3f",mt.kd.r,mt.kd.v,mt.kd.b) ;
pos += 1.0F;
glColor4fv((float *) &ld.coul);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"DIR LUM : %6.3f %6.3f %6.3f",ld.dir.x,ld.dir.y,ld.dir.z) ;
pos += 1.0F;
glColor4fv((float *) &ld.coul);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"COULEUR : %6.3f %6.3f %6.3f",ld.coul.r,ld.coul.v,ld.coul.b) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"ENERGIE : %6.3f",ld.energie) ;
pos += 1.0F;
glColor3f(e.r,e.v,e.b);
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"COULEUR : %6.3f %6.3f %6.3f",e.r,e.v,e.b) ;
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowPosition(50,50);
glutInitWindowSize(300,240);
f1 = glutCreateWindow("Lumiere diffusée");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.0,1.0,-1.0,1.0,-5.0,5.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(25.0F,15.0F,0.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutInitWindowSize(380,130);
glutInitWindowPosition(60,320);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special2);
glutMainLoop();
return(0);
}