Le source : TD-LumieresMateriauxOpenGL.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2004 */
/* Un objet 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 = 64.0F ;
static GLfloat angle = 0.0F ;
static int obj = 1;
static float dif[4] = { 0.5F,0.5F,0.5F,1.0F };
static int f1;
static int f2;
void myinit(void) {
glClearColor(0.5F,0.5F,1.0F,1.0F) ;
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurVert());
glLightfv(GL_LIGHT1,GL_SPECULAR,couleurBleu());
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void scene(void) {
glMaterialfv(GL_FRONT,GL_SPECULAR,dif);
glMaterialfv(GL_FRONT,GL_DIFFUSE,dif);
if ( obj )
glutSolidTeapot(1.3);
else
glutSolidSphere(1.3,360,180);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
{ GLfloat l_pos0[] = { 0.0F,0.0F,10.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,1.0F,0.0F };
glPushMatrix();
glLightfv(GL_LIGHT1,GL_POSITION,l_pos1);
glPopMatrix(); }
glMaterialf(GL_FRONT,GL_SHININESS,shininess);
scene();
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 ;
if ( shininess > 128.0F )
shininess = 128.0F;
postRedisplay();
break;
case 43 : shininess /= 1.03F ;
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 = { 0.0F,0.0F,10.0F,1.0F };
matrice m;
toRotationX(m,angle);
produitMatriceVecteur(m,l_pos0,l_pos0);
pos += 1.0F;
glColor3fv(couleurBlanc());
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"Position lumiere : %6.3f %6.3f %6.3f",l_pos0[0],l_pos0[1],l_pos0[2]) ;
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char **argv) {
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(440,70);
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);
}