/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2004 */ /* Animation en OpenGL + GLUT */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleMenus.h" #include "ModuleFont.h" #include "ModuleReshape.h" static int image = 0; static int n = 600; static int f1; static int f2; static int carre = 0; void calculPosition(int image,int n,float *x,float *y) { float l = 20.0F*image/(float) n; if ( l < 5.0 ) { *x = 2.5F; *y = -2.5F + l; return; } if ( l < 10.0 ) { *x = 7.5F - l; *y = 2.5F; return; } if ( l < 15.0 ) { *x = -2.5F; *y = 12.5F - l; return; } *x = -17.5F + l; *y = -2.5F; } void dessineTrajectoireCarree(void) { glBegin(GL_LINE_LOOP); glVertex2f(-2.5F,-2.5F); glVertex2f(2.5F,-2.5F); glVertex2f(2.5F,2.5F); glVertex2f(-2.5F,2.5F); glEnd(); } void display1() { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); if ( carre ) dessineTrajectoireCarree(); float x; float y; calculPosition(image,n,&x,&y); glTranslatef(x,y,0.0F); glutSolidSphere(1.0,120,120); glPopMatrix(); glFlush(); glutSwapBuffers() ; } void display2() { glClearColor(0.5F,0.5F,0.5F,1.0F); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float x; float y; calculPosition(image,n,&x,&y); float pos = 1.0F; glColor4fv(couleurNoir()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Image %4d sur %4d",image,n) ; pos += 1; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"P : %7.3f %7.3f",x,y) ; glPopMatrix(); glutSwapBuffers(); } void reshape2(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,w,-h,0,-1.0,1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void idle(void) { image++; if ( image == n ) image = 0; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); } void key(unsigned char key,int x,int y) { switch ( key ) { case 43 : n++ ; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case 45 : n-- ; if ( n < 2 ) n = 2; if ( image >= n ) image = n-1; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case ' ' : carre = !carre; glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); break; case 0x0D : { static int anim = 1; anim = !anim; if ( anim ) glutIdleFunc(idle); else glutIdleFunc(NULL); } break; case 0x1B : exit(0); break; } } void myinit() { glClearColor(0.8F,0.8F,0.8F,1.0F); GLfloat mat_shininess[] = { 50.0 }; GLfloat light0_position[] = { 1.0,1.0,1.0,0.0 }; GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 }; GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 }; glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc()); glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurRouge()); glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurVert()); glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurBleu()); 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); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(250,250); glutInitWindowPosition(50,50); f1 = glutCreateWindow("Animation OpenGL + GLUT"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-4.0,4.0,-4.0,4.0,-50.0,50.0); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutIdleFunc(idle); glutDisplayFunc(display1); glutInitWindowSize(200,50); glutInitWindowPosition(60,340); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Position sphère"); creationMenuBasique(); glutDisplayFunc(display2); glutReshapeFunc(reshape2); glutKeyboardFunc(key); glutMainLoop(); return(0); }