/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2003 */ /* Implantation d'un déplacement */ /* sur une trajectoire helicoidal */ #include #include #include #include #include #include "ModuleAxes.h" #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleFont.h" #ifndef M_PI #define M_PI 3.14159 #endif static float mat[4][4] ; static int image = 0; static int imagesParTour = 120; static int nbImages = 500; static int type = 0 ; static float ay = 2*M_PI/imagesParTour; static float dy = -2.0F/imagesParTour; static float x = 2.0F; static float y = 3.0F; static float z = 3.0F; static float px = -1.0F; static float py = 5.0F; static float pz = -1.0F; static float pobj[4] = { px,py,pz,1.0F }; static int f1; static int f2; void calculMatrice(float ay,float x,float y,float z,float dy) { float cy = cos(ay); float sy = sin(ay); mat[0][0] = cy; mat[0][1] = 0.0F; mat[0][2] = sy; mat[0][3] = -cy*x-sy*z+x; mat[1][0] = 0.0F; mat[1][1] = 1.0F; mat[1][2] = 0.0F; mat[1][3] = dy; mat[2][0] = -sy; mat[2][1] = 0.0F; mat[2][2] = cy; mat[2][3] = sy*x-cy*z+z; mat[3][0] = 0.0F; mat[3][1] = 0.0F; mat[3][2] = 0.0F; mat[3][3] = 1.0F; } void myinit(void) { GLfloat light_position0[] = { 0.0F,0.0F,1.0F,0.0F }; GLfloat light_position1[] = { 0.0F,0.0F,1.0F,0.0F }; glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir()); glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc()); glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc()); glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir()); glLightfv(GL_LIGHT0,GL_POSITION,light_position0); glLightfv(GL_LIGHT1,GL_POSITION,light_position1); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_BLEND); glEnable(GL_CULL_FACE); setManipulateurClavierAngle(35.0F,20.0F,0.0F); } void produitMatriceVecteur(float m[4][4],float v[4],float *r) { float rr[4]; int i; for ( i = 0 ; i < 4 ; i++ ) { rr[i] = 0.0F; for ( int j = 0 ; j < 4 ; j++ ) { rr[i] += m[i][j]*v[j]; } } for ( i = 0 ; i < 4 ; i++ ) { r[i] = rr[i]; } } void display(void) { glClearColor(0.25F,0.25F,0.25F,1.0F); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix() ; manipulateurSouris(); manipulateurClavier(); glDisable(GL_LIGHTING); axes(); glPushMatrix(); glTranslatef(x,y,z); glEnable(GL_LIGHTING); glutSolidSphere(0.15,36,36); glDisable(GL_LIGHTING); glColor4f(0.5F,0.5F,0.5F,0.5F); glBegin(GL_LINES); glVertex3f(0.0F,-100.0F,0.0F); glVertex3f(0.0F,100.0F,0.0F); glEnd(); glPopMatrix(); glEnable(GL_LIGHTING); float v[4] = { px,py,pz,1.0F }; for ( int i = 0 ; i < nbImages ; i++ ) { glPushMatrix(); glTranslatef(v[0],v[1],v[2]); glutSolidSphere(0.05,6,6); produitMatriceVecteur(mat,v,v); glPopMatrix(); } glPushMatrix(); glTranslatef(pobj[0],pobj[1],pobj[2]); glutSolidSphere(0.5,18,18); glPopMatrix(); glPopMatrix(); glFlush(); glutSwapBuffers(); } void postRedisplay() { glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); } void idle(void) { produitMatriceVecteur(mat,pobj,pobj); image++; if ( image > nbImages ) { image = 0; pobj[0] = px; pobj[1] = py; pobj[2] = pz; pobj[3] = 1.0F; } postRedisplay(); } void ajustementParametres(void) { ay = 2*M_PI/imagesParTour; dy = -2.0F/imagesParTour; if ( image > nbImages ) { image = 0; pobj[0] = px; pobj[1] = py; pobj[2] = pz; pobj[3] = 1.0F; } calculMatrice(ay,x,y,z,dy); } void key(unsigned char key,int mx,int my) { static int anim = 1; if ( keyManipulateur(key,mx,my) ) postRedisplay(); else switch ( key ) { case 0x0D : image = 0; pobj[0] = px; pobj[1] = py; pobj[2] = pz; pobj[3] = 1.0F; postRedisplay(); break; case 'a' : idle(); break; case 'n' : imagesParTour++; ajustementParametres(); postRedisplay(); break; case 'N' : imagesParTour--; if ( imagesParTour < 10 ) imagesParTour = 10; ajustementParametres(); postRedisplay(); break; case 'm' : nbImages++; ajustementParametres(); postRedisplay(); break; case 'M' : nbImages--; if ( nbImages < 10 ) nbImages = 10; ajustementParametres(); postRedisplay(); break; case '1' : x -= 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case '4' : x += 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case '2' : y -= 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case '5' : y += 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case '3' : z -= 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case '6' : z += 0.05; calculMatrice(ay,x,y,z,dy); postRedisplay(); break; case 32 : anim = !anim; glutIdleFunc((anim) ? idle : NULL); break; } } 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 display2() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurBlanc()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Nombre d'images par tour : %6d",imagesParTour) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Nombre total d'images : %6d",nbImages) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Position P : %6.3f %6.3f %6.3f",x,y,z) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Numero image : %6d",image) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Position mobile : %6.3f %6.3f %6.3f",pobj[0],pobj[1],pobj[2]) ; glPopMatrix(); glutSwapBuffers(); } int main(int argc,char **argv) { calculMatrice(ay,x,y,z,dy); glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowSize(400,300); glutInitWindowPosition(50,50); f1 = glutCreateWindow("Deplacement helicoidal"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-3.0,7.0,-4.0,6.0,-50.0,50.0); setManipulateurDistance(1.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutDisplayFunc(display); glutIdleFunc(idle); glutInitWindowSize(390,110); glutInitWindowPosition(60,390); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(display2); glutReshapeFunc(reshape2); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutMainLoop(); return(0); }