/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Janvier 2004 */ /* Un hippodrome schematise */ /* au moyen de spheres */ #include #include #include #include #include #include #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #ifndef M_PI #define M_PI 3.14159 #endif static int n = 40; static float r = 1.0F; static float rc = 8.0F; static float dc = 37.0F; static float init = 0.0F; static float d = 0.2F; static float coul[] = { 1.0F,1.0F,1.0F,1.0F }; void myinit(void) { glClearColor(0.8F,0.8F,0.8F,0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); } void scene(int n,float r,float rc,float dc) { float l = 2*dc+2*M_PI*rc; float dist = l/n; glPushMatrix(); for ( int i = 0 ; i < n ; i++ ) { glPushMatrix(); float pos = init+i*dist; if ( pos > l ) pos -= l; if ( pos < dc ) glTranslatef(-dc/2+pos,rc,0.0F); else if ( pos < dc+M_PI*rc ) { pos -= dc ; float a = pos/rc; float x = dc/2+rc*sin(a); float y = rc*cos(a); glTranslatef(x,y,0.0F); } else if ( pos < 2*dc+M_PI*rc ) { pos -= dc+M_PI*rc ; glTranslatef(dc/2-pos,-rc,0.0F); } else { pos -= 2*dc+M_PI*rc ; float a = pos/rc; float x = -dc/2-rc*sin(a); float y = -rc*cos(a); glTranslatef(x,y,0.0F); } glutSolidSphere(r,36,36); glPopMatrix(); } glPopMatrix(); } void display(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); int i; glBegin(GL_LINE_LOOP); for ( i = 0 ; i < 60 ; i++ ) { float a = M_PI/2.0F - i*M_PI/60; float x = dc/2+rc*cos(a); float y = rc*sin(a); glVertex2f(x,y); } for ( i = 0 ; i < 60 ; i++ ) { float a = -M_PI/2.0F - i*M_PI/60; float x = -dc/2+rc*cos(a); float y = rc*sin(a); glVertex2f(x,y); } glEnd(); scene(n,r,rc,dc); glPopMatrix(); glFlush(); glutSwapBuffers(); } void idle(void) { float l = 2*dc+2*M_PI*rc; init += d; if ( init > l ) init -= l; glutPostRedisplay(); } void key(unsigned char key,int x,int y) { static int anim = 1; if ( keyManipulateur(key,x,y) ) glutPostRedisplay(); else switch ( key ) { case 'r' : rc *= 1.01F; glutPostRedisplay(); break; case 'R' : rc /= 1.01F; glutPostRedisplay(); break; case 'd' : dc *= 1.01F; glutPostRedisplay(); break; case 'D' : dc /= 1.01F; glutPostRedisplay(); break; case 'a' : r *= 1.01F; glutPostRedisplay(); break; case 'A' : r /= 1.01F; glutPostRedisplay(); break; case 'v' : d *= 1.01F; glutPostRedisplay(); break; case 'V' : d /= 1.01F; glutPostRedisplay(); break; case 45 : n--; if ( n < 1 ) n = 1; glutPostRedisplay(); break; case 43 : n++; glutPostRedisplay(); break; case ' ' : anim = !anim; glutIdleFunc((anim) ? idle : NULL); } } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(300,150); glutInitWindowPosition(50,50); glutCreateWindow("Un hippodrome schematise"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-15.0F,15.0F,-15.0F,15.0F,-100.0F,100.0F); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutIdleFunc(idle); glutDisplayFunc(display); glutMainLoop(); return(0); }