Objet.h | Vecteur.h | Position.h | Direction.h |
Objet.cpp | Vecteur.cpp | Position.cpp | Direction.cpp |
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de matrice 4x4 */
#ifndef MATRICE
#define MATRICE
#include "Objet.h"
#include "Vecteur.h"
class Matrice : public Objet {
public :
float m[4][4];
public :
Matrice(void);
Matrice(float m[4][4]);
~Matrice(void);
void multiplie(Vecteur *v);
};
#endif
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de matrice 4x4 */
#include "Matrice.h"
Matrice::Matrice(void) {
for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ )
m[i][j] = ( i == j ) ? 1.0F : 0.0F;
}
Matrice::Matrice(float m[4][4]) {
for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ )
this->m[i][j] = m[i][j];
}
Matrice::~Matrice(void) {
}
void Matrice::multiplie(Vecteur *v) {
Vecteur *nv = new Vecteur();
nv->x = m[0][0]*v->x+m[0][1]*v->y+m[0][2]*v->z+m[0][3]*v->t;
nv->y = m[1][0]*v->x+m[1][1]*v->y+m[1][2]*v->z+m[1][3]*v->t;
nv->z = m[2][0]*v->x+m[2][1]*v->y+m[2][2]*v->z+m[2][3]*v->t;
nv->t = m[3][0]*v->x+m[3][1]*v->y+m[3][2]*v->z+m[3][3]*v->t;
*v = *nv;
delete(nv);
}
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion */
/* de transformations geometriques */
#ifndef TRANSFORMATION
#define TRANSFORMATION
#include "Matrice.h"
#include "Vecteur.h"
class Transformation : public Matrice {
public :
Transformation(void);
Transformation(float m[4][4]);
~Transformation(void);
void transforme(Vecteur *v);
void compose(Transformation *t);
};
#endif
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion */
/* de transformations geometriques */
#include "Transformation.h"
Transformation::Transformation(void) : Matrice() {
}
Transformation::Transformation(float m[4][4]) : Matrice(m) {
}
Transformation::~Transformation(void) {
}
void Transformation::transforme(Vecteur *v) {
multiplie(v);
}
void Transformation::compose(Transformation *t) {
Transformation *nt = new Transformation();
{ for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ ) {
nt->m[i][j] = 0;
for ( int k = 0 ; k < 4 ; k++ )
nt->m[i][j] += m[i][k]*t->m[k][j]; } }
{ for ( int i = 0 ; i < 4 ; i++ )
for ( int j = 0 ; j < 4 ; j++ )
m[i][j] = nt->m[i][j]; }
delete(nt);
}
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de translations */
#ifndef TRANSLATION
#define TRANSLATION
#include "Transformation.h"
#include "Direction.h"
class Translation : public Transformation {
public :
Translation(float tx,float ty,float tz);
Translation(Direction *d);
};
#endif
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de translations */
#include <stdio.h>
#include "Translation.h"
Translation::Translation(float tx,float ty,float tz) : Transformation() {
m[0][3] = tx;
m[1][3] = ty;
m[2][3] = tz;
}
Translation::Translation(Direction *d) : Transformation() {
m[0][3] = d->x;
m[1][3] = d->y;
m[2][3] = d->z;
}
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de rotations */
#ifndef ROTATION
#define ROTATION
#include "Transformation.h"
#include "Direction.h"
class Rotation : public Transformation {
public :
Rotation(float a,float ax,float ay,float az);
Rotation(float a,Direction *d);
};
#endif
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de rotations */
#include <math.h>
#include "Direction.h"
#include "Rotation.h"
static void calculMatrice(float a,Direction *nd,float m[4][4]) {
a *= 0.017453292519943295769236907684886F;
float c =(float) cos(a);
float s =(float) sin(a);
float x2 = nd->x*nd->x;
float y2 = nd->y*nd->y;
float z2 = nd->z*nd->z;
float xy = nd->x*nd->y;
float xz = nd->x*nd->z;
float yz = nd->y*nd->z;
float unmoinsc = 1.0F-c;
m[0][0] = x2+c*(1-x2);
m[0][1] = xy*unmoinsc-nd->z*s;
m[0][2] = xz*unmoinsc+nd->y*s;
m[1][0] = xy*unmoinsc+nd->z*s;
m[1][1] = y2+c*(1-y2);
m[1][2] = yz*unmoinsc-nd->x*s;
m[2][0] = xz*unmoinsc-nd->y*s;
m[2][1] = yz*unmoinsc+nd->x*s;
m[2][2] = z2+c*(1-z2);
}
Rotation::Rotation(float a,float ax,float ay,float az) : Transformation() {
Direction *nd = new Direction(ax,ay,az);
nd->normalise();
calculMatrice(a,nd,m);
delete(nd);
}
Rotation::Rotation(float a,Direction *d) : Transformation() {
Direction *nd = new Direction(d);
nd->normalise();
calculMatrice(a,nd,m);
delete(nd);
}
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de scales */
#ifndef SCALE
#define SCALE
#include "Transformation.h"
#include "Direction.h"
class Scale : public Transformation {
public :
Scale(float rx,float ry,float rz);
};
#endif
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Classe de gestion de scales */
#include "Scale.h"
Scale::Scale(float rx,float ry,float rz) : Transformation() {
m[0][0] = rx;
m[1][1] = ry;
m[2][2] = rz;
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Octobre 2005 */
/* Calculs matriciels */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#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);
}