Le source : GestionLumiere02.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2006 */
/* Gestion des lumieres en OpenGL */
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
static int to = 1;
static int tl = 1;
static int cl = 1;
static float r = 0.0F;
static int anim = 1;
static float z = 1.5F;
static float rx = 0.0F;
static float rz = 0.0F;
static GLfloat bl[4] = { 1.0F,1.0F,1.0F,1.0F };
static GLfloat blanc[4] = { 3.0F,3.0F,3.0F,1.0F };
static GLfloat rouge[4] = { 3.0F,0.0F,0.0F,1.0F };
static GLfloat vert[4] = { 0.0F,3.0F,0.0F,1.0F };
void init(void) {
glMaterialfv(GL_FRONT,GL_DIFFUSE,bl);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glLightf(GL_LIGHT0,GL_CONSTANT_ATTENUATION,0);
glLightf(GL_LIGHT0,GL_LINEAR_ATTENUATION,0);
glLightf(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,1);
glLightf(GL_LIGHT1,GL_CONSTANT_ATTENUATION,0);
glLightf(GL_LIGHT1,GL_LINEAR_ATTENUATION,0);
glLightf(GL_LIGHT1,GL_QUADRATIC_ATTENUATION,1);
}
void scene(void) {
glPushMatrix();
if ( to ) {
glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
for ( int i = 0 ; i < 100 ; i++ )
for ( int j = 0 ; j < 100 ; j++ ) {
float x = -5.0F + i*0.1F;
float y = -5.0F + j*0.1F;
glVertex3f(x,y,0.0F);
glVertex3f(x+0.1F,y,0.0F);
glVertex3f(x+0.1F,y+0.1F,0.0F);
glVertex3f(x,y+0.1F,0.0F); }
glEnd(); }
else {
glScalef(1.0F,1.0F,0.2F);
glutSolidSphere(5.0,72,72); }
glPopMatrix();
}
void display(void) {
glClearColor(0.8F,0.8F,0.8F,1.0F) ;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ;
glPushMatrix();
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(rz,0.0F,0.0F,1.0F);
glPushMatrix();
glRotatef(r,0.0F,0.0F,1.0F);
GLfloat l_pos0[] = { 3.0F,0.0F,z,1.0F };
GLfloat l_pos1[] = { -3.0F,0.0F,z,1.0F };
GLfloat l_dir0[] = { 1.0F,0.0F,1.0F,0.0F };
GLfloat l_dir1[] = { -1.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,(tl) ? l_pos0 : l_dir0);
glLightfv(GL_LIGHT1,GL_POSITION,(tl) ? l_pos1 : l_dir1);
glLightfv(GL_LIGHT0,GL_DIFFUSE,(cl) ? blanc : rouge);
glLightfv(GL_LIGHT1,GL_DIFFUSE,(cl) ? blanc : vert);
glDisable(GL_LIGHTING);
if ( tl ) {
glPushMatrix();
glTranslatef(3.0F,0.0F,z);
glColor3fv((cl) ? blanc : rouge);
glutSolidSphere(0.2,18,18);
glPopMatrix();
glPushMatrix();
glTranslatef(-3.0F,0.0F,z);
glColor3fv((cl) ? blanc : vert);
glutSolidSphere(0.2,18,18);
glPopMatrix(); }
glPopMatrix();
glEnable(GL_LIGHTING);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(7.0,(double) tx/ty,70.0,130.0) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,100.0,0.0,0.0,0.0,0.0,1.0,0.0);
}
void idle(void) {
r += 1.1F;
glutPostRedisplay();
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 'z' : z += 0.025F ;
glutPostRedisplay();
break;
case 'Z' : z -= 0.025F ;
glutPostRedisplay();
break;
case 'c' :
case 'C' : cl = !cl;
break;
case 'o' :
case 'O' : to = !to;
break;
case ' ' : anim = !anim;
glutIdleFunc((anim) ? idle : NULL);
break;
case 0x0D : tl = !tl ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
void special(int key,int x,int y) {
switch ( key ) {
case GLUT_KEY_UP : rx -= 1.0F ;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN : rx += 1.0F ;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT : rz += 1.0F ;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT : rz -= 1.0F ;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Gestion lumiere");
init();
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}