/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Octobre 2005 */ /* Calculs matriciels */ #include #include #include #include #include #include #include "Position.h" #include "Direction.h" #include "Translation.h" #include "Rotation.h" #include "Scale.h" #include "ModuleCouleurs.h" #include "ModuleManipulateur.h" #include "ModuleAxes.h" #include "ModuleFleche.h" #include "ModuleMenus.h" #include "ModuleReshape.h" #include "ModuleFont.h" static int question = 0; static int f1 ; static int f2 ; static int fv ; static float dx1 = -0.3F; static float dy1 = 1.6F; static float dz1 = -1.5F; static float dx2 = 2.2F; static float dy2 = -1.2F; static float dz2 = 1.8F; static Direction *n = new Direction(); static Direction *v = new Direction(); static Position *p1 = new Position(); static Position *p2 = new Position(); static float tx = 0.0F; static float ty = 0.0F; static float tz = 0.0F; static float a = 0.0F; static float ax = 0.0F; static float ay = 0.0F; static float az = 1.0F; static float rx = 1.0F; static float ry = 1.0F; static float rz = 1.0F; static Translation *tr = new Translation(tx,ty,tz); static Rotation *rt = new Rotation(a,ax,ay,az); static Scale *sc = new Scale(rx,ry,rz); static Transformation *m = new Transformation(); void dessine(Position *p1,Position *p2,Direction *v,Direction *n) { glPushMatrix(); glTranslatef(p1->x,p1->y,p1->z); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurVert()); flecheEnVolume(v->x,v->y,v->z,0.1F,0.5F,0.025F); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()); flecheEnVolume(n->x,n->y,n->z,0.1F,0.5F,0.04F); glPopMatrix(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurJaune()); p1->dessine(); glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()); p2->dessine(); } void scene1f1(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); p1 = new Position(dx1,dy1,dz1); p2 = new Position(dx2,dy2,dz2); v = new Direction(p1,p2); n = new Direction(p1,p2); n->normalise(); dessine(p1,p2,v,n); switch ( question ) { case 0 : tr = new Translation(tx,ty,tz); tr->transforme(p1); tr->transforme(p2); tr->transforme(n); tr->transforme(v); break; case 1 : rt = new Rotation(a,ax,ay,az); rt->transforme(p1); rt->transforme(p2); rt->transforme(n); rt->transforme(v); break; case 2 : sc = new Scale(rx,ry,rz); sc->transforme(p1); sc->transforme(p2); sc->transforme(n); sc->transforme(v); break; case 3 : tr = new Translation(tx,ty,tz); rt = new Rotation(a,ax,ay,az); sc = new Scale(rx,ry,rz); m = new Transformation(); m->compose(tr); m->compose(rt); m->compose(sc); m->transforme(p1); m->transforme(p2); m->transforme(n); m->transforme(v); break; } v = new Direction(p1,p2); n = new Direction(p1,p2); n->normalise(); dessine(p1,p2,v,n); } void display1f1() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); scene1f1(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(fv); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void scene1f2(void) { glDisable(GL_LIGHTING); axes(); glEnable(GL_LIGHTING); Position *p1 = new Position(dx1,dy1,dz1); Position *p2 = new Position(dx2,dy2,dz2); Direction *v = new Direction(p1,p2); Direction *n = new Direction(p1,p2); glPushMatrix(); switch ( question ) { case 0 : glTranslatef(tx,ty,tz); break; case 1 : glRotatef(a,ax,ay,az); break; case 2 : glScalef(rx,ry,rz); break; case 3 : glTranslatef(tx,ty,tz); glRotatef(a,ax,ay,az); glScalef(rx,ry,rz); break; } n->normalise(); dessine(p1,p2,v,n); glPopMatrix(); dessine(p1,p2,v,n); } void display1f2() { glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); manipulateurSouris(); manipulateurClavier(); scene1f2(); glPopMatrix(); glFlush(); glutSwapBuffers(); glutPostWindowRedisplay(fv); int error = glGetError(); if ( error != GL_NO_ERROR ) printf("Attention, erreur OpenGL %d\n",error); } void displayV1(void) { glClearColor(0.0F,0.0F,0.0F,1.0F) ; glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); glPushMatrix(); float pos = 1.0F; glColor4fv(couleurBlanc()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; switch ( question ) { case 0 : simpleBitmapOutput(1,REGULAR8x13, "TR : %8.4f %8.4f %8.4f", tx,ty,tz); break; case 1 : simpleBitmapOutput(1,REGULAR8x13, "RT : %8.4f %8.4f %8.4f %8.4f", a,ax,ay,az); break; case 2 : simpleBitmapOutput(1,REGULAR8x13, "SC : %8.4f %8.4f %8.4f", rx,ry,rz); break; case 3 : simpleBitmapOutput(1,REGULAR8x13, "TR : %8.4f %8.4f %8.4f", tx,ty,tz); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "RT : %8.4f %8.4f %8.4f %8.4f", a,ax,ay,az); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "SC : %8.4f %8.4f %8.4f", rx,ry,rz); break; } pos += 1.5F; glColor4fv(couleurGrisClair()); Transformation *t; switch ( question ) { case 0 : t = tr; break; case 1 : t = rt; break; case 2 : t = sc; break; case 3 : t = m; break; } placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "%8.4f %8.4f %8.4f %8.4f", t->m[0][0],t->m[0][1],t->m[0][2],t->m[0][3]); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "%8.4f %8.4f %8.4f %8.4f", t->m[1][0],t->m[1][1],t->m[1][2],t->m[1][3]); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "%8.4f %8.4f %8.4f %8.4f", t->m[2][0],t->m[2][1],t->m[2][2],t->m[2][3]); pos += 1.0F; placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13, "%8.4f %8.4f %8.4f %8.4f", t->m[3][0],t->m[3][1],t->m[3][2],t->m[3][3]); pos += 1.5F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; (new Position(dx1,dy1,dz1))->print("P1 : %8.4f %8.4f %8.4f"); pos += 1.0F; glColor4fv(couleurJaune()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; p1->print("P1' : %8.4f %8.4f %8.4f"); pos += 1.0F; glColor4fv(couleurBleu()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; (new Position(dx2,dy2,dz2))->print("P2 : %8.4f %8.4f %8.4f"); pos += 1.0F; glColor4fv(couleurBleu()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; p2->print("P2' : %8.4f %8.4f %8.4f"); pos += 1.0F; glColor4fv(couleurVert()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; v->print("V : %8.4f %8.4f %8.4f"); pos += 1.0F; glColor4fv(couleurBlanc()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; simpleBitmapOutput(1,REGULAR8x13,"NORME : %8.4f",v->norme()); pos += 1.0F; glColor4fv(couleurRouge()); placeFontCursor(5.0F,-pos*20.0F,0.0F) ; n->print("N : %8.4f %8.4f %8.4f"); glPopMatrix(); glFlush(); glutSwapBuffers(); } void postRedisplays(void) { glutPostWindowRedisplay(f1); glutPostWindowRedisplay(f2); } void key(unsigned char k,int x,int y) { if ( keyManipulateur(k,x,y) ) { postRedisplays(); } else switch ( k ) { case 0x0D : { question = (question+1)%4; int win = glutGetWindow(); glutSetWindow(fv); switch ( question ) { case 0 : case 2 : glutReshapeWindow(370,270); break; case 1 : glutReshapeWindow(450,270); break; case 3 : glutReshapeWindow(450,310); break; } glutSetWindow(win); postRedisplays(); } break; case '2' : tx += 0.02; postRedisplays(); break; case '1' : tx -= 0.02; postRedisplays(); break; case '5' : ty += 0.02; postRedisplays(); break; case '4' : ty -= 0.02; postRedisplays(); break; case '8' : tz += 0.02; postRedisplays(); break; case '7' : tz -= 0.02; postRedisplays(); break; case 'a' : a += 1.0F; postRedisplays(); break; case 'A' : a -= 1.0F; postRedisplays(); break ; } } void special(int key,int x,int y) { if ( specialManipulateur(key,x,y) ) { postRedisplays(); } else switch (key) { case GLUT_KEY_F1 : rx *= 1.02; postRedisplays(); break; case GLUT_KEY_F2 : rx /= 1.02; postRedisplays(); break; case GLUT_KEY_F3 : ry *= 1.02; postRedisplays(); break; case GLUT_KEY_F4 : ry /= 1.02; postRedisplays(); break; case GLUT_KEY_F5 : rz *= 1.02; postRedisplays(); break; case GLUT_KEY_F6 : rz /= 1.02; postRedisplays(); break; case GLUT_KEY_F7 : ax = 1.0F; ay = 0.0F; az = 0.0F; postRedisplays(); break; case GLUT_KEY_F8 : ax = 0.0F; ay = 1.0F; az = 0.0F; postRedisplays(); break; case GLUT_KEY_F9 : ax = 0.0F; ay = 0.0F; az = 1.0F; postRedisplays(); break; case GLUT_KEY_F10 : ax = 1.0F; ay = 1.0F; az = 0.0F; postRedisplays(); break; case GLUT_KEY_F11 : ax = 1.0F; ay = 1.0F; az = 1.0F; postRedisplays(); break; case GLUT_KEY_F12 : ax = 1.0F; ay = -1.0F; az = 1.0F; postRedisplays(); break; } } void myInit() { glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1); glShadeModel(GL_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.5F,0.5F,0.5F,1.0F); } void reshapeV(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(); } int main(int argc,char **argv) { glutInit(&argc,argv); { glutInitWindowSize(270,270); glutInitWindowPosition(50,50); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f1 = glutCreateWindow("Mathematiques des matrices"); myInit(); creationMenuBasique(); setParametresOrthoBasique(-2.5,2.5,-2.5,2.5,-50.0,50.0); setManipulateurDistance(1.0F); setManipulateurSourisAngle(10.0F,-20.0F,0.0F); glutReshapeFunc(reshapeOrthoBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutKeyboardFunc(key); glutDisplayFunc(display1f1); glutSpecialFunc(special); } { glutInitWindowSize(270,270); glutInitWindowPosition(350,50); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); f2 = glutCreateWindow("Avec transformations OpenGL"); myInit(); creationMenuBasique(); setParametresOrthoBasique(-2.5,2.5,-2.5,2.5,-50.0,50.0); setManipulateurDistance(1.0F); setManipulateurSourisAngle(10.0F,-20.0F,0.0F); glutReshapeFunc(reshapeOrthoBasique); glutMotionFunc(motionBasique); glutMouseFunc(sourisBasique); glutKeyboardFunc(key); glutDisplayFunc(display1f2); glutSpecialFunc(special); } { glutInitWindowSize(370,270); glutInitWindowPosition(60,360); glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); fv = glutCreateWindow("Valeurs"); creationMenuBasique(); glutDisplayFunc(displayV1); glutReshapeFunc(reshapeV); glutKeyboardFunc(key); glutSpecialFunc(special); } glutMainLoop(); return(0); }