Le source : AnimationRotation.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2006 */
/* Animation par rotation autour de l'axe Ox */
/* en C + OpenGL + GLUT */
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCylindres.h"
static float rx = 0.0F;
static int l = 1;
static int r = 1;
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 'r' : r = !r ;
glutPostRedisplay();
break;
case 0x0D : l = !l ;
glutPostRedisplay();
break;
case 0x1B : exit(0); }
}
void reshape(int tx,int ty) {
glViewport(0,0,tx,ty);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(4.5*(double) -tx/ty,4.5*(double) tx/ty,-4.5,4.5,-4.5,4.5) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void element() {
glPushMatrix();
glTranslatef(2.0F,0.0F,2.0F);
solidCylindre(0.25F,3.0F,40,20);
for ( int i = 0 ; i < 2 ; i++ ) {
glTranslatef(0.0F,2.0F,0.0F);
glutSolidCube(1.0F);
glRotatef(90.0F,0.0F,0.0F,1.0F);
glTranslatef(0.0F,2.0F,0.0F);
solidCylindre(0.25F,3.0F,40,20); }
glPopMatrix();
}
void scene() {
glPushMatrix();
if ( r )
glRotatef(45.0F,1.0F,1.0F,0.0F);
element();
for ( int i = 1 ; i < 4 ; i++ ) {
glPushMatrix();
glRotatef(90.0F*i,1.0F,0.0F,0.0F);
element();
glPopMatrix(); }
glPopMatrix();
}
void display(void) {
if ( l )
glEnable(GL_LIGHTING);
else
glDisable(GL_LIGHTING);
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);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
void init(void) {
GLfloat l_pos[] = { 0.0F,0.0F,1.0F,0.0F };
GLfloat c[4] = { 0.5F,0.2F,0.6F,1.0F };
glMaterialfv(GL_FRONT,GL_DIFFUSE,c);
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glEnable(GL_LIGHT0);
glColor4fv(c) ;
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
void idle(void) {
rx++;
glutPostRedisplay();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Animation par rotation");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}