Le source : LumieresAnimees.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Animation de lumieres en OpenGL */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <math.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleFont.h"
static int image = 0;
static int f1;
static int f2;
static int l = 0;
void myinit(void) {
static float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
static float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
glClearColor(0.5F,0.5F,0.5F,1.0F) ;
glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
glLightfv(GL_LIGHT0,GL_SPECULAR,bleu);
glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune);
glLightfv(GL_LIGHT1,GL_SPECULAR,magenta);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void scene(void) {
static float gris[] = { 0.6F,0.6F,0.6F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,gris);
glMaterialfv(GL_FRONT,GL_SPECULAR,gris);
glMaterialf(GL_FRONT,GL_SHININESS,120);
glPushMatrix();
glutSolidSphere(4,180,180);
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
switch (l) {
case 0 : glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
break;
case 1 : glDisable(GL_LIGHT0);
glEnable(GL_LIGHT1);
break;
case 2 : glEnable(GL_LIGHT0);
glDisable(GL_LIGHT1);
break; }
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glPushMatrix() ;
float pxl0 = -10.0F + fabs((image%200)-100)/5.0F;
GLfloat l0_pos[] = { pxl0,0.0F,10.0F,1.0F };
glLightfv(GL_LIGHT0,GL_POSITION,l0_pos);
float ax = (image*2)%360;
glRotatef(ax,1.0F,0.0F,0.0F);
GLfloat l1_dir[] = { 0.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT1,GL_POSITION,l1_dir);
glPopMatrix() ;
glPushMatrix() ;
scene();
glPopMatrix() ;
glPopMatrix();
glFlush();
glutSwapBuffers();
glutPostWindowRedisplay(f2);
}
void idle(void) {
image++ ;
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();
}
void display2() {
float pxl0 = -10.0F + fabs((image%200)-100)/5.0F;
float ax = (image*2)%360;
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glPushMatrix();
glColor4fv(couleurBlanc());
float pos = 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"POSITION : %7.3f %6.3f %6.3f",pxl0,0.0F,10.0F) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"ANGLE : %6.3f",ax) ;
glPopMatrix();
glutSwapBuffers();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostWindowRedisplay(f1);
else
switch ( key ) {
case ' ' : { static int anim = 1;
anim = !anim;
glutIdleFunc(( anim ) ? idle : NULL); }
break;
case 0x0D : l = (l+1)%3;
glutPostWindowRedisplay(f1);
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
f1 = glutCreateWindow("Des lumières animées");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-5.0,5.0,-5.0,5.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutIdleFunc(idle);
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutInitWindowSize(330,50);
glutInitWindowPosition(60,330);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutMainLoop();
return(0);
}