/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Septembre 2007 */ /* Une animation en OpenGL */ #include #include #include #include #include "ModuleCubes.h" /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static float rotation = 0.0F; static int solution = 0; static int disc = 30; static float rayon = 2.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 }; /* Variables globales de gestion */ /* de l'interactivite clavier et souris */ static int clic = 0; static int mx; static int my; static float rx = 0.0F; static float ry = 0.0F; /* Fonction d'initialisation des parametres */ /* OpenGL ne changeant pas au cours de la vie */ /* du programme */ void init(void) { const GLfloat mat_shininess[] = { 80.0 }; const GLfloat light0_position[] = { 2.0, 2.0,2.0,1.0 }; const GLfloat light1_position[] = { -2.0, 2.0,2.0,1.0 }; const GLfloat light2_position[] = { 2.0,-2.0,2.0,1.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(float rayon) { glPushMatrix(); glRotatef(rotation,0.0F,0.0F,1.0F); glPushMatrix(); glTranslatef(-rayon,0.0F,0.0F); glutSolidSphere(1.0,disc,disc); glPopMatrix(); glPushMatrix(); glTranslatef(rayon,0.0F,0.0F); mySolidCube(2.0,disc); glPopMatrix(); glPopMatrix(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display2(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); scene(rayon); glPopMatrix(); glFlush(); glutSwapBuffers(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display1(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); scene(2.0F); glPopMatrix(); glFlush(); glutSwapBuffers(); } /* Fonction executee lors qu'aucun evenement */ /* n'est en file d'attente */ void idle2(void) { rotation += 1.0F; rayon -= 0.0005F; if ( rayon < 1.0F ) rayon = 1.0F; glutPostRedisplay(); } /* Fonction executee lors qu'aucun evenement */ /* n'est en file d'attente */ void idle1(void) { rotation += 1.0F; glutPostRedisplay(); } /* Fonction executee lors d'un clic de souris */ /* dans la fenetre */ void mouse(int bouton,int etat,int x,int y) { if ( bouton == GLUT_LEFT_BUTTON ) { if ( etat == GLUT_DOWN ) { clic = 1; mx = x; my = y; } if ( etat == GLUT_UP ) { clic = 0; } } } /* Fonction executee lors d'un deplacement */ /* de la souris sur la fenetre */ /* avec un bouton appuye */ void motion(int x,int y) { if ( clic ) { ry += (x-mx); rx += (y-my); mx = x; my = y; 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(-4.0,4.0,(double) -4.0*y/x,(double) 4.0*y/x,-4.0,4.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors de la frappe */ /* d'une touche alphanumerique du clavier */ void key(unsigned char key,int x,int y) { switch ( key ) { case 43 : disc++; glutPostRedisplay(); break; case 45 : disc--; if ( disc < 1 ) disc = 1; glutPostRedisplay(); break; case 0x20 : rayon = 2.0; glutPostRedisplay(); break; case 0x0D : solution = (solution+1)%2; glutDisplayFunc(( solution == 0 ) ? display1 : display2); glutIdleFunc(( solution == 0 ) ? idle1 : idle2); glutPostRedisplay(); 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(300,300); glutInitWindowPosition(50,50); glutCreateWindow("Un cube et une sphere en orbite"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMotionFunc(motion); glutMouseFunc(mouse); glutIdleFunc(idle1); glutDisplayFunc(display1); glutMainLoop(); return(0); }