/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2007 */ /* Un programme OpenGL d'animation automatique */ /* d'un ensemble de particules ponctuelles */ /* selon des orbites circulaires de periodes */ /* en pow(r,1.5) (orbite keplerienne) */ /* ou r est le rayon de l'orbite et est defini */ /* aleatoirement entre 0.1 et 10.0 */ #include #include #include #include #include #include /* Variables globales */ static GLint nombreEtoiles = 5000; static int image = 0; static float vitesse = 0.1F; /* 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; static float zoom = 1.0F; /* Fonction d'affichage d'une eventuelle */ /* erreur OpenGL */ static void checkError(int line) { GLenum err = glGetError(); if (err) { printf("GL Error %s (0x%x) at line %d\n", gluErrorString(err), (int) err, line); } } /* Fonction de calcul d'un nombre reel */ /* aleatoire compris entre 0.0 et 1.0 */ static float random(void) { float v = ((float) rand()/RAND_MAX); return(v); } /* Fonction de dessin du systeme de particules */ /* blurees */ static void drawVoieLactee(void) { srand(0); glBegin(GL_LINES); for ( int i = 0 ; i < nombreEtoiles ; i++ ) { float r = 0.1F+random()*7.0F; float a = random()*3.14159*2+vitesse*image/pow((double) r,1.5); float inc = vitesse/pow((double) r,1.5); int ite = 1+(int) (inc/0.05); float inca = inc/ite; for ( int j = 0 ; j < ite ; j++ ) { float aa = a+j*inca; { float x = r*cos(aa); float y = r*sin(aa); glVertex3f(x,y,0.0F); } { float x = r*cos(aa+inca); float y = r*sin(aa+inca); glVertex3f(x,y,0.0F); } } } glEnd(); } /* Fonction executee lors d'un rafraichissement */ /* de la fenetre de dessin */ static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0F,0.0F,-6.0F); glScalef(zoom,zoom,zoom); glRotatef(rx,1.0F,0.0F,0.0F); glRotatef(ry,0.0F,1.0F,0.0F); drawVoieLactee(); glPopMatrix(); glFlush(); checkError(__LINE__); glutSwapBuffers(); } /* Fonction executee lorsqu'aucun evenement */ /* n'est en attente de gestion */ static void idle(void) { image++; glutPostRedisplay(); } /* Fonction executee lors d'un changement */ /* de la taille de la fenetre OpenGL */ /* Configuration d'une camera de visualisation */ /* en projection en perspective */ void reshape(int tx,int ty) { glViewport(0,0,tx,ty); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50,(double) tx/ty,0.1,200.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 ' ' : { static int anim = 0; anim = !anim; glutIdleFunc(anim ? idle : NULL); } break; case 0x1B : exit(0); break; } } /* Fonction executee lors de la frappe */ /* d'une touche non alphanumerique du clavier */ void special(int key,int x,int y) { switch ( key ) { case GLUT_KEY_UP : zoom *= 0.99F; glutPostRedisplay(); break; case GLUT_KEY_DOWN : zoom /= 0.99F; glutPostRedisplay(); break; case GLUT_KEY_RIGHT : vitesse += 0.001F; glutPostRedisplay(); break; case GLUT_KEY_LEFT : vitesse -= 0.001F; glutPostRedisplay(); break; } } /* Fonction d'initialisation de certains */ /* parametres de l'environnement OpenGL */ static void init(void) { glClearColor(0.0F,0.0F,0.0F,0.0F); glEnable(GL_DEPTH_TEST); } /* 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 principale */ int main(int argc,char *argv[]) { glutInit(&argc,argv); glutInitWindowPosition(10,10); glutInitWindowSize(480,240); glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("Voie lactee avec motion blur"); init(); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutMouseFunc(mouse); glutMotionFunc(motion); glutSpecialFunc(special); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }