Le source : Animation2007-2008.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2007 */
/* Une animation en OpenGL */
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables et constantes globales */
/* pour les angles et les couleurs utilises */
static float r1 = 0.0F;
static float r2 = 0.0F;
static float r3 = 0.0F;
static float r4 = 0.0F;
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat mat_shininess[] = { 50.0 };
const GLfloat light0_position[] = { 1.0,1.0,1.0,0.0 };
const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 };
const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
glLightfv(GL_LIGHT1,GL_DIFFUSE,vert);
glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
}
/* Scene dessinee */
void scene(void) {
glPushMatrix();
glRotatef(r1,0.0F,1.0F,0.0F);
glutSolidTorus(0.7,10.0,18,72);
glRotatef(r2,1.0F,0.0F,0.0F);
glutSolidTorus(0.7,8.6F,18,72);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glRotatef(r3,1.0F,0.0F,0.0F);
glutSolidTorus(0.7,7.2F,18,72);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glRotatef(r4,1.0F,0.0F,0.0F);
glutSolidTorus(0.7,5.8F,18,72);
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
/* Fonction executee lors qu'aucun evenement */
/* n'est en file d'attente */
void idle(void) {
r1 += 0.21137F ;
r2 += 0.3174F ;
r3 += 0.4715F ;
r4 += 0.6433F ;
glutPostRedisplay() ;
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
void reshape(int x,int y) {
glViewport(0,0,x,y);
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
glOrtho(-12.0,12.0,(double) -12.0*y/x,(double) 12.0*y/x,-12.0,12.0) ;
glMatrixMode(GL_MODELVIEW) ;
glLoadIdentity() ;
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Des anneaux en rotation");
init();
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}