/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2002 */ /* Transformations geometriques */ /* successives pour la visualisation */ #include #include #include #include #include #include #include "ModuleCouleurs.h" #include "ModuleDessinArc.h" #include "ModuleFleche.h" #include "ModuleMatriceVecteur.h" #include "ModuleFont.h" #include "ModuleManipulateur.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #ifndef M_PI #define M_PI 3.141459F #endif static int tscn = 0; static int scn = 1; static int vol = 0; static int f1; static int f2; static int f3; static vecteur pp1; static float ay = 0.0F; static float ax = 0.0F; static float dist = -5.0F; static float ratio = 1.0F; static vecteur o = { 0.0F,0.0F,0.0F,1.0F }; static vecteur pv = { 0.0F,0.0F,0.0F,1.0F }; void glutSolidCylinder(float r,float h,int n,int m) { glPushMatrix(); glRotatef(90,1.0F,0.0F,0.0F); glTranslatef(0.0F,0.0F,-h/2); GLUquadricObj *qobj = gluNewQuadric(); gluQuadricDrawStyle(qobj,GLU_FILL); gluCylinder(qobj,r,r,h,n,m); gluDeleteQuadric(qobj); glPopMatrix(); } void legendesAxes(float lx1,float ly1,float lz1,char *x,char *y,char *z) { glColor4fv(couleurRougeFonce()); bitmapStringOutput(lx1+0.3F,0.0F,0.0F,x); glColor4fv(couleurVertFonce()); bitmapStringOutput(0.0F,ly1+0.3F,0.0F,y); glColor4fv(couleurBleuFonce()); bitmapStringOutput(0.0F,0.0F,lz1+0.3F,z); setFont(GLUT_BITMAP_8_BY_13,LEFT); glPopAttrib(); } void axes(float lx1,float lx2,float ly1,float ly2,float lz1,float lz2,char *x,char *y,char *z) { glPushAttrib(GL_LIGHTING); glEnable(GL_LIGHTING); setFont(GLUT_BITMAP_8_BY_13,CENTER); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge()); flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert()); flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu()); flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F); glDisable(GL_LIGHTING); glColor4fv(couleurRougeFonce()); bitmapStringOutput(1.3F,0.0F,0.0F,x); glColor4fv(couleurRougeFonce(0.5F)); glBegin(GL_LINES); glVertex3f(lx1,0.0F,0.0F); glVertex3f(-lx2,0.0F,0.0F); glEnd(); glColor4fv(couleurVertFonce()); bitmapStringOutput(0.0F,1.3F,0.0F,y); glColor4fv(couleurVertFonce(0.5F)); glBegin(GL_LINES); glVertex3f(0.0F,ly1,0.0F); glVertex3f(0.0F,-ly2,0.0F); glEnd(); glColor4fv(couleurBleuFonce()); bitmapStringOutput(0.0F,0.0F,1.3F,z); glColor4fv(couleurBleuFonce(0.5F)); glBegin(GL_LINES); glVertex3f(0.0F,0.0F,lz1); glVertex3f(0.0F,0.0F,-lz2); glEnd(); setFont(GLUT_BITMAP_8_BY_13,LEFT); glPopAttrib(); } void myinit(void) { GLfloat light_position0[] = { 1.0F,0.0F,1.0F,0.0F }; GLfloat light_position1[] = { -1.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_AUTO_NORMAL); glEnable(GL_NORMALIZE); glDepthFunc(GL_LESS); glEnable(GL_ALPHA_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); } void dessineSphere(vecteur p) { glPushMatrix(); glPushAttrib(GL_LIGHTING); glEnable(GL_LIGHTING); glTranslatef(p[0],p[1],p[2]); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisMoyen()); glutSolidSphere(0.08,20,20); glPopAttrib(); glPopMatrix(); } void scene(void) { glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); glEnable(GL_AUTO_NORMAL); if ( tscn ) { glPushMatrix(); glTranslatef(2.5F,-1.0F,-4.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge(0.5F)); glutSolidSphere(0.5F,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(2.0F,-1.0F,-3.0F); glRotatef(50.0F,1.0F,1.0F,-1.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu(0.5F)); glutSolidCone(0.6F,1.0F,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(1.5F,-2.0F,-4.0F); glRotatef(50.0F,1.0F,1.0F,-1.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurMagenta(0.5F)); glutSolidCube(0.8F); glPopMatrix(); glPushMatrix(); glTranslatef(3.0F,-2.0F,-3.0F); glRotatef(50.0F,0.0F,0.0F,1.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert(0.5F)); glutSolidTorus(0.25F,0.4F,30,30); glPopMatrix(); } else { glPushMatrix(); glTranslatef(2.5F,-1.0F,-4.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge(0.5F)); glutSolidCylinder(0.1F,2.0F,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(2.0F,-1.0F,-3.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu(0.5F)); glutSolidCylinder(0.1F,2.0F,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(1.5F,-2.0F,-4.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurMagenta(0.5F)); glutSolidCylinder(0.1F,2.0F,30,30); glPopMatrix(); glPushMatrix(); glTranslatef(3.0F,-2.0F,-3.0F); glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert(0.5F)); glutSolidCylinder(0.1F,2.0F,30,30); glPopMatrix(); } glDisable(GL_NORMALIZE); glDisable(GL_AUTO_NORMAL); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); } void volumeDeVisualisation(float vouv,float ratio,float cmin,float cmax) { glEnable(GL_CULL_FACE); float y1 = cmin * tan(vouv*M_PI/360.0F); float x1 = y1*ratio; glBegin(GL_LINE_LOOP); glVertex3f(x1,y1,-cmin); glVertex3f(x1,-y1,-cmin); glVertex3f(-x1,-y1,-cmin); glVertex3f(-x1,y1,-cmin); glEnd(); float y2 = cmax * tan(vouv*M_PI/360.0F); float x2 = y2*ratio; glBegin(GL_LINE_LOOP); glVertex3f(x2,y2,-cmax); glVertex3f(x2,-y2,-cmax); glVertex3f(-x2,-y2,-cmax); glVertex3f(-x2,y2,-cmax); glEnd(); glBegin(GL_LINES); glVertex3f(x1,y1,-cmin); glVertex3f(x2,y2,-cmax); glVertex3f(x1,-y1,-cmin); glVertex3f(x2,-y2,-cmax); glVertex3f(-x1,-y1,-cmin); glVertex3f(-x2,-y2,-cmax); glVertex3f(-x1,y1,-cmin); glVertex3f(-x2,y2,-cmax); glEnd(); glBegin(GL_QUADS); glColor4fv(couleurRose(0.3F)); glVertex3f(-x1,y1,-cmin); glVertex3f(-x1,-y1,-cmin); glVertex3f(x1,-y1,-cmin); glVertex3f(x1,y1,-cmin); glVertex3f(x2,y2,-cmax); glVertex3f(x2,-y2,-cmax); glVertex3f(-x2,-y2,-cmax); glVertex3f(-x2,y2,-cmax); glVertex3f(-x2,y2,-cmax); glVertex3f(-x2,-y2,-cmax); glVertex3f(-x1,-y1,-cmin); glVertex3f(-x1,y1,-cmin); glVertex3f(x1,y1,-cmin); glVertex3f(x1,-y1,-cmin); glVertex3f(x2,-y2,-cmax); glVertex3f(x2,y2,-cmax); glVertex3f(x2,y2,-cmax); glVertex3f(-x2,y2,-cmax); glVertex3f(-x1,y1,-cmin); glVertex3f(x1,y1,-cmin); glVertex3f(x1,-y1,-cmin); glVertex3f(-x1,-y1,-cmin); glVertex3f(-x2,-y2,-cmax); glVertex3f(x2,-y2,-cmax); glEnd(); glEnable(GL_CULL_FACE); } void display(void) { glClearColor(0.85F,0.85F,0.65F,1.0F); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix() ; glTranslatef(-2.0F,-0.3F,0.0F); manipulateurSouris(); manipulateurClavier(); axes(5.0F,1.5F,2.5F,2.0F,5.0F,9.0F,"","",""); legendesAxes(5.0F,2.5F,5.0F,"x","y","z"); glColor4fv(couleurNoir()); bitmapStringOutput(0.2F,-0.2F,0.2F,"O"); vecteur pi1 = { 0.0F,0.0F,-2.0F,1.0F }; vecteur pi2 = { 0.0F,0.0F,-2.5F,1.0F }; vecteur pf1; vecteur pf2; matrice m; toRotation(m,ay,0.0F,1.0F,0.0F); produitMatriceVecteur(m,pi1,pf1); matrice r; toRotation(r,ax,1.0F,0.0F,0.0F); produitMatriceMatrice(m,r,m); produitMatriceVecteur(m,pi2,pf2); vecteur p1 = { 0.0F,1.5F,0.0F,1.0F }; produitMatriceVecteur(m,p1,p1); matrice m1; toTranslation(r,0.0F,0.0F,dist); produitMatriceMatrice(m,r,m1); pv[0] = pv[1] = pv[2] = 0.0F; pv[3] = 1.0F; produitMatriceVecteur(m1,pv,pv); glColor4fv(couleurNoir()); bitmapStringOutput(pv[0]+0.3F,pv[1]+0.3F,pv[2]+0.3F,"Pv"); glColor4fv(couleurGrisFonce(0.5F)); glBegin(GL_LINES); glVertex3f(pv[0],pv[1],pv[2]); glVertex3f(pv[0],0.0F,pv[2]); glVertex3f(pv[0],pv[1],pv[2]); glVertex3f(pv[0],pv[1],0.0F); glVertex3f(pv[0],pv[1],pv[2]); glVertex3f(0.0F,pv[1],pv[2]); glVertex3f(pv[0],0.0F,pv[2]); glVertex3f(pv[0],0.0F,0.0F); glVertex3f(pv[0],pv[1],0.0F); glVertex3f(pv[0],0.0F,0.0F); glVertex3f(0.0F,pv[1],pv[2]); glVertex3f(0.0F,pv[1],0.0F); glVertex3f(pv[0],pv[1],0.0F); glVertex3f(0.0F,pv[1],0.0F); glVertex3f(pv[0],0.0F,pv[2]); glVertex3f(0.0F,0.0F,pv[2]); glVertex3f(0.0F,pv[1],pv[2]); glVertex3f(0.0F,0.0F,pv[2]); glVertex3f(p1[0]*2,p1[1]*2,p1[2]*2); glVertex3f(-p1[0]*2,-p1[1]*2,-p1[2]*2); glEnd(); glBegin(GL_LINE_LOOP); glVertex3f(1.3F*pv[0],0.0F,1.3F*pv[2]); glVertex3f(1.3F*pv[0],pv[1],1.3F*pv[2]); glVertex3f(-0.3F*pv[0],pv[1],-0.3F*pv[2]); glVertex3f(-0.3F*pv[0],0.0F,-0.3F*pv[2]); glEnd(); dessineSphere(pv); glColor4fv(couleurNoir()); if ( ay != 0.0F ) { traceAngleFleche(pi1,pf1,30,1.0F,1,2,5.0F,0.07F) ; vecteur pint; positionIntermediaire(pi1,pf1,1.0F,pint); placeFontCursor(pint[0],pint[1],pint[2]) ; deplacementCursor(0,-10,0) ; simpleBitmapOutput(1,REGULAR8x13,"Ry") ; } if ( ax != 0.0F ) { traceAngleFleche(pf1,pf2,30,1.0F,1,2,5.0F,0.07F) ; vecteur pint; positionIntermediaire(pf1,pf2,1.0F,pint); placeFontCursor(pint[0],pint[1],pint[2]) ; deplacementCursor(5,5,0) ; simpleBitmapOutput(1,REGULAR8x13,"Rx") ; } glPushMatrix() ; pp1[0] = p1[0]; pp1[1] = p1[1]; pp1[2] = p1[2]; glRotatef(ay,0.0F,1.0F,0.0F); glRotatef(ax,1.0F,0.0F,0.0F); glColor4fv(couleurGrisFonce()); axes(0.0F,0.0F,3.0,2.0,2.0F,6.0F,"xo","yo","zo"); glPopMatrix() ; if ( scn ) scene(); glColor4fv(couleurVert(0.2F)); glBegin(GL_QUADS); glVertex3f(1.3F*pv[0],0.0F,1.3F*pv[2]); glVertex3f(1.3F*pv[0],pv[1],1.3F*pv[2]); glVertex3f(-0.3F*pv[0],pv[1],-0.3F*pv[2]); glVertex3f(-0.3F*pv[0],0.0F,-0.3F*pv[2]); glEnd(); if ( vol ) { glClear(GL_DEPTH_BUFFER_BIT); glPushMatrix() ; glRotatef(ay,0.0F,1.0F,0.0F); glRotatef(ax,1.0F,0.0F,0.0F); glColor4fv(couleurNoir(0.5F)); volumeDeVisualisation(anglePerspectiveBasique(),ratioPerspectiveBasique(),3.0F,5.5F); glPopMatrix() ; } glPopMatrix() ; glDisable(GL_DEPTH_TEST); glFlush(); glutSwapBuffers() ; glutPostWindowRedisplay(f2); glutPostWindowRedisplay(f3); } void key(unsigned char key,int x,int y) { switch ( key ) { case 'Y' : ay += 1.0F; glutPostWindowRedisplay(f1); break; case 'y' : ay -= 1.0F; glutPostWindowRedisplay(f1); break; case 'X' : ax += 1.0F; glutPostWindowRedisplay(f1); break; case 'x' : ax -= 1.0F; glutPostWindowRedisplay(f1); break; case 'z' : dist += 0.1F; glutPostWindowRedisplay(f1); break; case 'Z' : dist -= 0.1F; glutPostWindowRedisplay(f1); break; case 's' : tscn = !tscn; glutPostWindowRedisplay(f1); break; case 0x0D : scn = !scn; glutPostWindowRedisplay(f1); break; case ' ' : vol = !vol; glutPostWindowRedisplay(f1); break; case 0x1B : exit(0); break; } } void special(int key,int x,int y) { if ( specialManipulateur(key,x,y) ) { glutPostWindowRedisplay(f1); } } 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); glClearColor(0.0F,0.0F,0.0F,1.0F) ; glPushMatrix(); float pos = 1.0F; glColor4fv(couleurBlanc()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Ry : %8.3f",ay) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Rx : %8.3f",ax) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Distance : %6.3f",dist) ; pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"Pv : %6.3f %6.3f %6.3f",pv[0],pv[1],pv[2]) ; glPopMatrix(); glutSwapBuffers(); } void display3() { glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glClearColor(0.25F,0.25F,0.25F,1.0F) ; glPushMatrix(); gluLookAt(0.0F,0.0F,0.0F,pv[0],pv[1],pv[2],pp1[0],pp1[1],pp1[2]); dessineSphere(pv); scene(); glPopMatrix(); glutSwapBuffers(); glDisable(GL_DEPTH_TEST); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowPosition(10,50); glutInitWindowSize(400,280); f1 = glutCreateWindow("Visualisation de Po vers Pv"); myinit(); creationMenuBasique(); setParametresOrthoBasique(-2.7,2.7,-2.7,2.7,-100.0,100.0); setManipulateurDistance(1.0F); setManipulateurClavierAngle(19.0F,-15.0F,0.0F); glutReshapeFunc(reshapeOrthoBasique); glutKeyboardFunc(key); glutSpecialFunc(specialBasique); glutDisplayFunc(display); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutInitWindowSize(340,90); glutInitWindowPosition(40,380); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(display2); glutReshapeFunc(reshape2); glutKeyboardFunc(key); glutSpecialFunc(special); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); glutInitWindowPosition(430,50); glutInitWindowSize(250,250); f3 = glutCreateWindow("Scene"); myinit(); creationMenuBasique(); setParametresPerspectiveBasique(35.0F,1.0F,3.0F,5.5F,0.0F,0.0F,0.0F,f1); glutReshapeFunc(reshapePerspectiveBasique); glutKeyboardFunc(key); glutSpecialFunc(special); glutDisplayFunc(display3); glutMainLoop(); return(0); }