Le source : LumieresEtMateriel-1.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2008 */
/* Un programme OpenGL illustrant l'utilisation */
/* de lumieres et d'un materiel */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables globales */
static int obj = 0;
static float rx = 0.0F;
static float ry = 0.0F;
static int mouvement = 0 ;
static int button = 0 ;
static int mx;
static int my;
/* Fonction executee lors de l'appui */
/* d'une touche de la souris */
void mouse(int bouton,int etat,int x,int y) {
button = bouton;
mx = x ;
my = y ;
if ( etat == GLUT_DOWN ) {
mouvement = 1 ; }
if ( etat == GLUT_UP ) {
mouvement = 0 ; }
}
/* Fonction executee lors du deplacement */
/* de la souris devant la fenetre */
/* avec le bouton appuye */
void motion(int x,int y) {
switch ( button ) {
case GLUT_LEFT_BUTTON :
if ( mouvement == 1 ) {
rx += (y-my) ;
ry += (x-mx) ;
mx = x;
my = y;
glutPostRedisplay(); }
break; }
}
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
glEnable(GL_LIGHTING);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
}
/* Fonction de modelisation de la scene */
void scene(int obj) {
glEnable(GL_LIGHTING);
{ { float c[4] = { 2.0F,0.0F,0.0F,1.0F };
glLightfv(GL_LIGHT0,GL_DIFFUSE,c);
glLightfv(GL_LIGHT0,GL_SPECULAR,c); }
{ float dir[4] = { 1.0F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,dir); }
glEnable(GL_LIGHT0); }
{ { float c[4] = { 0.0F,1.0F,0.0F,1.0F };
glLightfv(GL_LIGHT1,GL_DIFFUSE,c);
glLightfv(GL_LIGHT1,GL_SPECULAR,c); }
{ float pos[4] = { -5.0F,-5.0F,15.0F,1.0F };
glLightfv(GL_LIGHT1,GL_POSITION,pos); }
glEnable(GL_LIGHT1); }
{ { float c[4] = { 0.0F,0.0F,1.0F,1.0F };
glLightfv(GL_LIGHT2,GL_DIFFUSE,c);
glLightfv(GL_LIGHT2,GL_SPECULAR,c); }
{ float pos[4] = { -1.0F,1.0F,15.0F,1.0F };
glLightfv(GL_LIGHT2,GL_POSITION,pos); }
{ float dir[4] = { 1.0F,-1.0F,-15.0F };
glLightfv(GL_LIGHT2,GL_SPOT_DIRECTION,dir); }
glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,8.594374F);
glLightf(GL_LIGHT2,GL_SPOT_EXPONENT,0.0F);
glEnable(GL_LIGHT2); }
{ float t[4] = { 0.6F,0.2F,0.8F,1.0F };
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,t); }
{ float t[4] = { 0.0F,0.0F,0.0F,1.0F };
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,t); }
{ float t[4] = { 0.0F,0.0F,0.0F,1.0F };
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,t); }
{ float t[4] = { 0.0F,0.0F,0.0F,1.0F };
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,t); }
glPushMatrix();
switch (obj) {
case 0 :
{ float t[4] = { 6.40F };
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,t); }
glRotatef(57.295826F,1.10F,-1.0F,0.0F);
glutSolidCube(6.0);
break;
case 1 :
{ float t[4] = { 32.0F };
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,t); }
glutSolidSphere(4.0,180,180);
break; }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClearColor(0.6F,0.6F,0.6F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
/* Affichage de la scene */
glPushMatrix();
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
scene(obj);
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
/* Configuration d'une camera de visualisation */
/* en projection en perspective */
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float ratio =(float) tx/ty;
gluPerspective(6.302541F,ratio,1.0,1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(-0.0F,-0.0F,-100.0F);
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void keyboard(unsigned char key,int x,int y) {
switch ( key ) {
case ' ' : obj = (obj+1)%2;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Lumieres et materiel");
init();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}