Fichier source : Animation.cpp
/* Une animation en OpenGL */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2021 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
static void idle1(void);
static void idle2(void);
static void idle3(void);
static void idle4(void);
/* Variables et constantes globales */
/* pour les angles et les couleurs utilises */
static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;
static float alphaz1 = 0.0F;
static float alphaz2 = 90.0F;
static float alphaz3 = 180.0F;
static float alphaz4 = 270.0F;
static float delta = 0.2F / (2.0F * 3.14159F * 0.75F) * 360.0F;
/* Affichage des informations relatives */
/* a OpenGL */
static void informationsOpenGL(void) {
printf("GL_VENDOR = %s\n",(const char *) glGetString(GL_VENDOR));
printf("GL_VERSION = %s\n",(const char *) glGetString(GL_VERSION));
printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
printf("GL_EXTENSIONS = %s\n",(const char *) glGetString(GL_EXTENSIONS));
}
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
static void init(void) {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
/* Scene dessinee */
static void sphere(float angle, const float *couleur) {
glPushMatrix();
glRotatef(angle, 0.0F, 0.0F, 1.0F);
glTranslatef(0.75F, 0.0F, 0.0F);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, couleur);
glutSolidSphere(0.1, 36, 36);
glPopMatrix();
}
static void scene(void) {
glPushMatrix();
const float gris[4] = { 0.8F, 0.8F, 0.8F, 1.0F };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gris);
glutSolidTorus(0.02, 0.75, 72, 180);
const float rouge[4] = { 1.0F, 0.0F, 0.0F, 1.0F };
sphere(alphaz1, rouge);
const float vert[4] = { 0.0F, 1.0F, 0.0F, 1.0F };
sphere(alphaz2, vert);
const float bleu[4] = { 0.0F, 0.0F, 1.0F, 1.0F };
sphere(alphaz3, bleu);
const float magenta[4] = { 1.0F, 0.0F, 1.0F, 1.0F };
sphere(alphaz4, magenta);
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
static void display(void) {
glClearColor(0.5F,0.5F,0.5F,0.5F);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rz,0.0F,0.0F,1.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
glRotatef(rx,1.0F,0.0F,0.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention erreur %d\n",error);
}
/* Fonction executee lorsqu'aucun evenement */
/* n'est en file d'attente */
static void idle1(void) {
//printf("I1\n");
alphaz1 += 0.11F;
if (alphaz1 + delta >= alphaz2) {
alphaz1 = alphaz2 - delta + 360.0F;
glutIdleFunc(idle2);
}
glutPostRedisplay();
}
static void idle2(void) {
//printf("I2\n");
alphaz2 += 0.13F;
if (alphaz2 + delta >= alphaz3) {
alphaz2 = alphaz3 - delta + 360.0F;
glutIdleFunc(idle3);
}
glutPostRedisplay();
}
static void idle3(void) {
//printf("I3\n");
alphaz3 += 0.17F;
if (alphaz3 + delta >= alphaz4) {
alphaz3 = alphaz4 - delta + 360.0F;
glutIdleFunc(idle4);
}
glutPostRedisplay();
}
static void idle4(void) {
//printf("I4\n");
alphaz4 += 0.19F;
if (alphaz4 + delta >= alphaz1) {
alphaz4 = alphaz1 - delta + 360.0F;
glutIdleFunc(idle1);
}
glutPostRedisplay();
}
/* Fonction executee lors d'un changement */
/* de la taille de la fenetre OpenGL */
static void reshape(int wx, int wy) {
glViewport(0, 0, wx, wy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Fonction executee lors de l'appui */
/* d'une touche non alphanumerique du clavier */
static 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 :
ry += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT :
ry -= 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_UP :
rz += 1.0F;
glutPostRedisplay();
break;
case GLUT_KEY_PAGE_DOWN :
rz -= 1.0F;
glutPostRedisplay();
break; }
}
/* Fonction executee lors de l'appui */
/* d'une touche alphanumerique du clavier */
static void keyboard(unsigned char key,int x,int y) {
switch (key) {
case 'f' :
case 'F' :
{ static int fullScreen = 0;
static int tx;
static int ty;
static int px;
static int py;
fullScreen = !fullScreen;
if ( fullScreen ) {
px = glutGet(GLUT_WINDOW_X);
py = glutGet(GLUT_WINDOW_Y);
tx = glutGet(GLUT_WINDOW_WIDTH);
ty = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen(); }
else
glutPositionWindow(px,py);
glutReshapeWindow(tx,ty); }
break;
case 0x0D :
{ static int anim = 0;
anim = !anim;
glutIdleFunc(( anim ) ? idle1 : NULL); }
break;
case ' ' :
{ informationsOpenGL(); }
break;
case 0x1B :
exit(0);
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(480,480);
glutInitWindowPosition(50,50);
glutCreateWindow("Une animation");
init();
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}