/* Gestion de camera et animation automatique */ /* d'une sphere par rebond sur les bords */ /* de la fenetre d'affichage */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2015 */ #include #include #include #include #include /* Variables et constantes globales */ /* pour les angles et les couleurs utilises */ static int aff = 1; static int lum = 1; static int posx = 250; static int posy = 150; static int dx = 1; static int dy = 1; static int n = 100; static int tx = 500; static int ty = 300; static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F }; static const float jaune[] = { 1.0F,1.0F,0.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 shininess[] = { 50.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,blanc); glMaterialfv(GL_FRONT,GL_SHININESS,shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge); glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune); glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu); 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(); glTranslatef(posx,posy,0.0F); glutSolidSphere(20.0,n,n); glPopMatrix(); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ void reshape(int x,int y) { tx = x; ty = y; glViewport(0,0,x,y); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,x,0.0,y,-30.0,30.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ void display(void) { glClearColor(0.5F,0.5F,0.5F,0.5F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); const GLfloat light0_position[] = { 0.0,0.0,0.0,1.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 }; glLightfv(GL_LIGHT0,GL_POSITION,light0_position); glLightfv(GL_LIGHT1,GL_POSITION,light1_position); glLightfv(GL_LIGHT2,GL_POSITION,light2_position); if ( lum ) glEnable(GL_LIGHTING); else glDisable(GL_LIGHTING); glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE); glPushMatrix(); scene(); glPopMatrix(); glFlush(); glutSwapBuffers(); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Erreur : %d\n",error); } /* Fonction executee quand aucun evenement */ /* n'est en file d'attente */ void idle(void) { /* Deplacement */ posx += dx; posy += dy; /* Gestion du fenetrage */ if ( posx+20 > tx ) posx = tx-20; if ( posy+20 > ty ) posy = ty-20; /* Gestion des rebonds */ if ( ( posx-20 == 0 ) || ( posx+20 == tx ) ) dx = -dx; if ( ( posy+20 == ty ) || ( posy-20 == 0 ) ) dy = -dy; /* Rafraichissement */ glutPostRedisplay(); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier: */ /* - lettres minuscules et majuscules */ /* - chiffres */ /* - /+-*,.;: */ /* - autres touches a code ASCII: */ /* - espace */ /* - enter */ /* - escape */ void keyboard(unsigned char key,int x,int y) { switch (key) { case 0x0D : { static int anim = 1; anim = !anim; glutIdleFunc(( anim ) ? idle : NULL); } break; case 'm' : aff = !aff; glutPostRedisplay(); break; case 'l' : lum = !lum; glutPostRedisplay(); break; case 43 : n++; glutPostRedisplay(); break; case 45 : n--; if ( n == 2 ) n = 3; 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(tx,ty); glutInitWindowPosition(50,50); glutCreateWindow("Boule rebondissante"); init(); glutKeyboardFunc(keyboard); glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return(0); }