Le source : MathematiquesDesMatrices.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Decembre 2001 */
/* Calculs mathematiques sur les vecteurs */
/* et les matrices de transformation */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleManipulateur.h"
#include "ModuleMatriceVecteur.h"
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
#include "ModuleReshape.h"
static int type = 0 ;
static int affi = 0 ;
static int face = 18 ;
static float t[3][4];
static int tt[3] = { 0,0,0 };
static int f1;
static int f2;
static int param = 2;
static int pparam = 0;
static int nbparam[3] = { 1,1,1 };
static matrice mc;
void axes(void) {
setFont(GLUT_BITMAP_8_BY_13,CENTER);
glColor4fv(couleurRouge());
flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurVert());
flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurBleu());
flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F);
glColor4fv(couleurRougeFonce());
bitmapStringOutput(1.3F,0.0F,0.0F,"x");
glColor4fv(couleurVertFonce());
bitmapStringOutput(0.0F,1.3F,0.0F,"y");
glColor4fv(couleurBleuFonce());
bitmapStringOutput(0.0F,0.0F,1.3F,"z");
setFont(GLUT_BITMAP_8_BY_13,LEFT);
}
void identite(void) {
toIdentite(mc);
}
void translation(float tx,float ty,float tz) {
matrice m;
toTranslation(m,tx,ty,tz);
produitMatriceMatrice(mc,m,mc);
}
void rotation(float ang,float ax,float ay,float az) {
matrice m;
toRotation(m,ang,ax,ay,az);
produitMatriceMatrice(mc,m,mc);
}
void zoom(float rx,float ry,float rz) {
matrice m;
toScale(m,rx,ry,rz);
produitMatriceMatrice(mc,m,mc);
}
void sommet(void) {
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ;
vecteur v = { 0.0F,0.0F,0.0F,1.0F };
produitMatriceVecteur(mc,v,v);
glTranslatef(v[0],v[1],v[2]);
if ( affi == 0 )
glutSolidSphere(0.1,3*face,3*face) ;
else
glutWireSphere(0.1,face,face) ;
glPopMatrix();
glDisable(GL_LIGHTING);
}
void cube(float r,float *c) {
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,c) ;
vecteur v[8] = { { 0.5F,0.5F,0.5F,1.0F },
{ 0.5F,0.5F,-0.5F,1.0F },
{ 0.5F,-0.5F,-0.5F,1.0F },
{ 0.5F,-0.5F,0.5F,1.0F },
{ -0.5F,0.5F,0.5F,1.0F },
{ -0.5F,0.5F,-0.5F,1.0F },
{ -0.5F,-0.5F,-0.5F,1.0F },
{ -0.5F,-0.5F,0.5F,1.0F } };
int i ;
for ( i = 0 ; i < 8 ; i++ )
produitMatriceVecteur(mc,v[i],v[i]);
for ( i = 0 ; i < 8 ; i++ ) {
glPushMatrix();
glTranslatef(v[i][0],v[i][1],v[i][2]);
if ( affi == 0 )
glutSolidSphere(r,2*face,2*face) ;
else
glutWireSphere(r,face,face) ;
glPopMatrix(); }
glDisable(GL_LIGHTING);
glColor4fv(c) ;
glBegin(GL_LINE_LOOP);
glVertex3fv(v[0]);
glVertex3fv(v[1]);
glVertex3fv(v[2]);
glVertex3fv(v[3]);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3fv(v[4]);
glVertex3fv(v[5]);
glVertex3fv(v[6]);
glVertex3fv(v[7]);
glEnd();
glBegin(GL_LINES);
glVertex3fv(v[0]);
glVertex3fv(v[4]);
glVertex3fv(v[1]);
glVertex3fv(v[5]);
glVertex3fv(v[2]);
glVertex3fv(v[6]);
glVertex3fv(v[3]);
glVertex3fv(v[7]);
glEnd();
}
void display2() {
int pos;
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor4fv(couleurBlanc());
glBegin(GL_LINES);
glVertex2f(2.0F,0.0F);
glVertex2f(418.0F,0.0F);
glEnd();
glColor4fv(couleurJaune());
glBegin(GL_LINE_LOOP);
glVertex2f(2.0F,5.0F+param*20.0F);
glVertex2f(418.0F,5.0F+param*20.0F);
glVertex2f(418.0F,25.0F+param*20.0F);
glVertex2f(2.0F,25.0F+param*20.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(138.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(206.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(206.0F+70.0*pparam,24.0F+param*20.0F);
glVertex2f(138.0F+70.0*pparam,24.0F+param*20.0F);
glEnd();
glPopMatrix();
glPushMatrix();
for ( pos = 0 ; pos < 3 ; pos++ ) {
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
if ( tt[pos] == 0 ) {
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13,"IDENTITE"); }
else {
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
switch (tt[pos]) {
case 1 : simpleBitmapOutput(REGULAR8x13,"TRANSLATION : ");
break;
case 2 : simpleBitmapOutput(REGULAR8x13,"ROTATION : %6.2f",t[pos][3]);
break;
case 3 : simpleBitmapOutput(REGULAR8x13,"SCALE : ");
break; }
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13," %6.1f %6.2f %6.2f",t[pos][0],t[pos][1],t[pos][2]); } }
glColor4fv(couleurBlanc());
pos = -1 ;
placeFontCursor(5.0F,pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13,"POS INIT : 0.000 0.000 0.000 1.000");
pos = -2 ;
placeFontCursor(5.0F,-5.0F+pos*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13,"MATRICE :");
for ( int i = 0 ; i < 4 ; i++ ) {
placeFontCursor(5.0F,-5.0F+(-2-i)*20.0F,0.0F);
simpleBitmapOutput(REGULAR8x13," %6.3f %6.3f %6.3f %6.3f",mc[i][0],mc[i][1],mc[i][2],mc[i][3]); }
pos = -6 ;
placeFontCursor(5.0F,-10.0F+pos*20.0F,0.0F);
vecteur v = { 0.0F,0.0F,0.0F,1.0F };
produitMatriceVecteur(mc,v,v);
simpleBitmapOutput(REGULAR8x13,"POS FINALE : %6.3f %6.3f %6.3f %6.3f",v[0],v[1],v[2],v[3]);
glPopMatrix();
glutSwapBuffers();
}
void display() {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glShadeModel(GL_SMOOTH);
axes();
identite();
switch (type) {
case 0 : sommet();
break;
case 1 : cube(0.1F,couleurCyan());
break; }
for ( int i = 2 ; i >= 0 ; i-- )
switch ( tt[i] ) {
case 1 : translation(t[i][0],t[i][1],t[i][2]);
break;
case 2 : rotation(t[i][0],t[i][1],t[i][2],t[i][3]);
break;
case 3 : zoom(t[i][0],t[i][1],t[i][2]);
break; }
switch (type) {
case 0 : sommet();
break;
case 1 : cube(0.1F,couleurJaune());
break; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit() {
identite();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
}
void special(int k,int x,int y) {
switch (k) {
case GLUT_KEY_UP : param = (param+1)%3;
pparam = pparam%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_DOWN : param = (param+2)%3;
pparam = pparam%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_RIGHT : pparam = (pparam+1)%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_LEFT : pparam = (pparam+nbparam[param]-1)%nbparam[param];
glutPostWindowRedisplay(f2);
break; }
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '8' : special(GLUT_KEY_UP,x,y);
break;
case '2' : special(GLUT_KEY_DOWN,x,y);
break;
case '4' : special(GLUT_KEY_LEFT,x,y);
break;
case '6' : special(GLUT_KEY_RIGHT,x,y);
break;
case 'i' :
case 'I' : if ( tt[param] != 0 ) {
tt[param] = 0 ; }
nbparam[param] = 1;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 't' :
case 'T' : if ( tt[param] != 1 ) {
tt[param] = 1 ;
t[param][0] = t[param][1] = t[param][2] = 0.0F ; }
nbparam[param] = 3;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 'r' :
case 'R' : if ( tt[param] != 2 ) {
tt[param] = 2 ;
t[param][3] = 1.0F ;
t[param][0] = t[param][1] = t[param][2] = 0.0F ; }
nbparam[param] = 4;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 's' :
case 'S' : if ( tt[param] != 3 ) {
tt[param] = 3 ;
t[param][0] = t[param][1] = t[param][2] = 1.0F ; }
nbparam[param] = 3;
pparam = 0;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case ' ' : affi = 1 - affi;
glutPostWindowRedisplay(f1);
break;
case 'd' : face++;
glutPostWindowRedisplay(f1);
break;
case 'D' : face--;
if ( face < 3 )
face = 3;
glutPostWindowRedisplay(f1);
break;
case 43 : if ( ( tt[param] == 2 ) && ( pparam == 0 ) )
t[param][pparam] += 1.0F;
else
t[param][pparam] += 0.1F;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 45 : if ( ( tt[param] == 2 ) && ( pparam == 0 ) )
t[param][pparam] -= 1.0F;
else {
t[param][pparam] -= 0.1F;
if ( tt[param] == 3 )
if ( t[param][pparam] < 0.0F )
t[param][pparam] = 0.0F; }
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 0x0D : type = (type+1)%2;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0);
break; }
}
void special1(int k,int x,int y) {
if ( specialManipulateur(k,x,y) )
glutPostWindowRedisplay(f1);
else
special(k,x,y);
}
void key1(unsigned char ky,int x,int y) {
if ( keyManipulateur(ky,x,y) )
glutPostWindowRedisplay(f1);
else
key(ky,x,y);
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,70-h,70,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,200);
glutInitWindowPosition(50,50);
f1 = glutCreateWindow("Mathematiques des matrices");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-2.0F,2.0F,-2.0F,2.0F,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(20.0F,-45.0F,0.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutInitWindowSize(420,210);
glutInitWindowPosition(370,150);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
myinit();
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}