/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2010 */ /* */ /* Un programme OpenGL d'animation automatique */ /* d'un ensemble de particules ponctuelles */ /* modelisant une cascade */ #include #include #include #include #include #include #include "Position3D.h" #include "Direction3D.h" #include "Particule.h" static GLint win = 0; static int program = 2; static GLint cascadeArraySize = 25000; static double inc = 0.02; static double time = 0.0; class Particule *particules; 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); } } static void createCascade(GLint size) { particules = new Particule[cascadeArraySize](); for ( int i = 0 ; i < cascadeArraySize ; i++ ) { particules[i].init(i*15.0/cascadeArraySize);} } static void drawCascade(void) { glPointSize(1.0); glBegin(GL_POINTS); for ( int i = 0 ; i < cascadeArraySize ; i++ ) { if ( time >= particules[i].startIni ) { glVertex3dv((double *) &(particules[i].p)); } } glEnd(); static int img = 0; printf("%8.2lf : %5d\n",time,img); img++; } /* 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(); gluPerspective(60.0F,(float) x/y,15.0,5000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(10.0,-20.0,50.0,10.0,-20.0,0.0,0.0,1.0,0.0); } static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); drawCascade(); glTranslatef(0.0F,-20.0F,0.0F); glPopMatrix(); glFlush(); checkError(__LINE__); glutSwapBuffers(); } static void cleanUp(void) { delete [] particules; } static void idle(void) { time += inc; for ( int i = 0 ; i < cascadeArraySize ; i++ ) { particules[i].update(time); } glutPostRedisplay(); } /* Fonction executee lors de l'appui */ /* d'une touche alphanumerique du clavier */ void key(unsigned char key,int x,int y) { static int anim = 1; switch ( key ) { case 0x0D : anim = !anim; glutIdleFunc(anim ? idle : NULL); break; case 43 : time += inc; for ( int i = 0 ; i < cascadeArraySize ; i++ ) { particules[i].update(time); } glutPostRedisplay(); break; case 45 : time -= inc; if ( time < 0.0F ) time = 0.0F; for ( int i = 0 ; i < cascadeArraySize ; i++ ) { particules[i].update(time); } glutPostRedisplay(); break; case 0x1B : cleanUp(); exit(0); break; } } static void init(void) { createCascade(cascadeArraySize); glClearColor(0.4F,0.4F,0.8F,0.0F); glEnable(GL_DEPTH_TEST); glColor3f(1.0F,1.0F,1.0F); } int main(int argc, char *argv[]) { glutInit(&argc,argv); glutInitWindowPosition(10,10); glutInitWindowSize(240,480); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); win = glutCreateWindow(argv[0]); glutReshapeFunc(reshape); glutKeyboardFunc(key); glutDisplayFunc(display); glutIdleFunc(idle); init(); glutMainLoop(); return(0); }