/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2007 */ /* Un programme OpenGL d'animation automatique */ /* d'une scene par rotation autour de l'axe Ox */ #include #include #include #include #include #include #include "ModuleCubes.h" /* Variables globales */ /* l : activation/desactivation de l'eclairage */ /* obj : type d'objet a afficher */ /* rx : valeur d'angle de rotation automatique */ static int l = 1; static int obj = 0; static float rx = 0.0F; /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection orthographique */ void reshape(int tx,int ty) { /* Configuration du viewport d'affichage */ glViewport(0,0,tx,ty); /* Choix et configuration de la camera */ /* de visualisation */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((double) -3.0*tx/ty,(double) 3.0*tx/ty,-3.0,3.0,-3.0,3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Scene dessinee */ void scene(int obj) { glPushMatrix(); glRotatef(20.0F,1.0F,1.0F,1.0F); switch ( obj ) { case 0 : mySolidCube(3.0,50); break; case 1 : glutSolidTorus(1.3,1.3,120,120); break; } glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.6F,0.6F,0.6F,1.0F) ; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) ; const GLfloat l_pos[] = { 1.0F,1.0F,1.0F,0.0F }; if ( l ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); glEnable(GL_NORMALIZE); glLightfv(GL_LIGHT0,GL_POSITION,l_pos); glPushMatrix(); /* Affichage de la scene */ glRotatef(rx,1.0F,0.0F,0.0F); scene(obj); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en attente de gestion */ void idle(void) { rx += 1.0F; glutPostRedisplay(); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void keyboard(unsigned char key,int x,int y) { switch ( key ) { case ' ' : { static int anim = 1; anim = !anim; glutIdleFunc((anim) ? idle : NULL); glutPostRedisplay(); } break; case 0x0D : obj = (obj+1)%2; glutPostRedisplay(); break; case 'L' : case 'l' : l = !l ; glutPostRedisplay(); break; case 0x1B : exit(0); } } /* Fonction principale */ int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Animation automatique"); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }