typedef struct dir_3D {
float dx ;
float dy ;
float dz ; } dir_3D ;
float produitScalaire(dir_3D *d1,
dir_3D *d2) {
return(d1->dx*d2->dx +
d1->dy*d2->dy +
d1->dz*d2->dz) ;
}
void produitVectoriel(dir_3D *v1,dir_3D
*v2,dir_3D *n) {
n->dx = v1->dy*v2->dz - v1->dz*v2->dy;
n->dy = v1->dz*v2->dx - v1->dx*v2->dz;
n->dz = v1->dx*v2->dy - v1->dy*v2->dx;
}
void normalise(dir_3D *n) {
float d2 = n->dx*n->dx + n->dy*n->dy + n->dz*n->dz ;
d2 =(float) pow(d2,0.5) ;
n->dx /= d2 ;
n->dy /= d2 ;
n->dz /= d2 ;
}
void calculVecteur(coord_3D *pi,
coord_3D *pf,
dir_3D *n) {
n->dx = pf->x - pi->x ;
n->dy = pf->y - pi->y ;
n->dz = pf->z - pi->z ;
}
void produitVectoriel(dir_3D *v1,dir_3D *v2,dir_3D *n) {
n->dx = v1->dy*v2->dz - v1->dz*v2->dy;
n->dy = v1->dz*v2->dx - v1->dx*v2->dz;
n->dz = v1->dx*v2->dy - v1->dy*v2->dx;
}
void calculNormaleFacette(facette *f,dir_3D *n) {
dir_3D v1;
dir_3D v2;
calculVecteur(&f->pa,&f->pb,&v1) ;
calculVecteur(&f->pa,&f->pc,&v2) ;
produitVectoriel(&v1,&v2,n);
normalise(n);
}
Le source : TD-Mathematiques1.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Calculs mathematiques */
/* sur les vecteurs et les facettes */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleFont.h"
typedef struct dir_3D {
float dx ;
float dy ;
float dz ; } dir_3D ;
typedef struct coord_3D {
float x ;
float y ;
float z ; } coord_3D ;
typedef struct facette {
struct coord_3D pa ;
struct coord_3D pb ;
struct coord_3D pc ; } facette ;
enum {
VECTEUR1,
VECTEUR2 };
enum {
SOMMETA,
SOMMETB,
SOMMETC };
static int question = 0;
static GLfloat zoom = 3.0F;
static int modif = VECTEUR1;
static int sommet = SOMMETA;
static facette f = { {-1.2F,-1.5F,-0.5F},
{ 1.1F,-0.1F, 1.4F},
{ -0.2F, 1.1F, 0.4F} };
static dir_3D v1 = { 0.5F,0.8F,0.6F };
static dir_3D v2 = { -0.3F,-0.7F,0.7F };
float distance(coord_3D *p1,
coord_3D *p2) {
double x = p1->x - p2->x ;
double y = p1->y - p2->y ;
double z = p1->z - p2->z ;
double d2 = x*x + y*y + z*z ;
return((float) pow(d2,0.5)) ;
}
void calculVecteurNorme(coord_3D *pi,
coord_3D *pf,
dir_3D *n) {
double x = pf->x - pi->x ;
double y = pf->y - pi->y ;
double z = pf->z - pi->z ;
double d2 = distance(pi,pf) ;
n->dx =(float) (x / d2) ;
n->dy =(float) (y / d2) ;
n->dz =(float) (z / d2) ;
}
void normalise(dir_3D *n) {
float d2 = n->dx*n->dx + n->dy*n->dy + n->dz*n->dz ;
d2 =(float) pow((double) d2,0.5) ;
n->dx /= d2 ;
n->dy /= d2 ;
n->dz /= d2 ;
}
void calculVecteur(coord_3D *pi,
coord_3D *pf,
dir_3D *n) {
n->dx = pf->x - pi->x ;
n->dy = pf->y - pi->y ;
n->dz = pf->z - pi->z ;
}
float produitScalaire(dir_3D *d1,
dir_3D *d2) {
return(d1->dx*d2->dx +
d1->dy*d2->dy +
d1->dz*d2->dz) ;
}
void produitVectoriel(dir_3D *v1,dir_3D *v2,dir_3D *n) {
n->dx = v1->dy*v2->dz - v1->dz*v2->dy;
n->dy = v1->dz*v2->dx - v1->dx*v2->dz;
n->dz = v1->dx*v2->dy - v1->dy*v2->dx;
}
void calculNormaleFacette(facette *f,dir_3D *n) {
dir_3D v1;
dir_3D v2;
calculVecteur(&f->pa,&f->pb,&v1) ;
calculVecteur(&f->pa,&f->pc,&v2) ;
produitVectoriel(&v1,&v2,n);
normalise(n);
}
void fleche(float dx,float dy,float dz,float cr,float cl) {
glPushMatrix();
glBegin(GL_LINES);
glVertex3f(0.0F,0.0F,0.0F);
glVertex3f(dx,dy,dz);
glEnd();
glPopMatrix();
glPushMatrix();
double d = pow((double) (dx*dx+dy*dy+dz*dz),0.5);
dx /= d;
dy /= d;
dz /= d;
double a = acos(dz) * 360 / 3.14159;
glRotatef(a/2,-dy,dx,0);
glTranslatef(0.0F,0.0F,(float) d);
glutSolidCone(cr,cl,10,10);
glPopMatrix();
}
void centre(facette *f,coord_3D *p) {
p->x =(f->pa.x+f->pb.x+f->pc.x)/3.0F;
p->y =(f->pa.y+f->pb.y+f->pc.y)/3.0F;
p->z =(f->pa.z+f->pb.z+f->pc.z)/3.0F;
}
void traceFacette(facette *f) {
glBegin(GL_POLYGON);
glVertex3f(f->pa.x,f->pa.y,f->pa.z);
glVertex3f(f->pb.x,f->pb.y,f->pb.z);
glVertex3f(f->pc.x,f->pc.y,f->pc.z);
glEnd();
}
void normaleFacette() {
glEnable(GL_DEPTH_TEST);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glScalef(zoom,zoom,zoom);
glColor4fv(couleurBlanc());
glPushMatrix();
traceFacette(&f);
glPopMatrix();
dir_3D nm ;
calculNormaleFacette(&f,&nm);
coord_3D pcentre;
centre(&f,&pcentre);
glTranslatef(pcentre.x,pcentre.y,pcentre.z);
glColor4fv(couleurRouge());
fleche(nm.dx,nm.dy,nm.dz,0.05F,0.2F);
glColor4fv(couleurBleu());
fleche(-nm.dx,-nm.dy,-nm.dz,0.05F,0.2F);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
float xmin = getXmin();
float ymax = getYmax();
float tpix = getTaillePixel();
setAntialiased(1);
setBold(1);
setEcartementCaracteres(15.0F);
glPushMatrix();
glColor4fv(couleurRouge());
strokeOutput(xmin,-ymax+30*tpix,0.65F,"Normale : %f %f %f",nm.dx,nm.dy,nm.dz);
glColor4fv(couleurBleu());
strokeOutput(xmin,-ymax+10*tpix,0.65F,"Normale : %f %f %f",-nm.dx,-nm.dy,-nm.dz);
glColor4fv(couleurBlanc());
strokeOutput(xmin,ymax-16*tpix,0.65F,"Sommet A : %f %f %f",f.pa.x,f.pa.y,f.pa.z);
strokeOutput(xmin,ymax-36*tpix,0.65F,"Sommet B : %f %f %f",f.pb.x,f.pb.y,f.pb.z);
strokeOutput(xmin,ymax-56*tpix,0.65F,"Sommet C : %f %f %f",f.pc.x,f.pc.y,f.pc.z);
glPopMatrix();
setAntialiased(0);
setBold(0);
setEcartementCaracteres(0.0F);
}
void produitsScalaireVectoriel() {
glEnable(GL_DEPTH_TEST);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glScalef(zoom,zoom,zoom);
glColor4fv(couleurRouge());
fleche(v1.dx,v1.dy,v1.dz,0.05F,0.2F);
glColor4fv(couleurBleu());
fleche(v2.dx,v2.dy,v2.dz,0.05F,0.2F);
dir_3D v;
produitVectoriel(&v1,&v2,&v);
glColor4fv(couleurJaune());
fleche(v.dx,v.dy,v.dz,0.05F,0.2F);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
float xmin = getXmin();
float ymax = getYmax();
float tpix = getTaillePixel();
setAntialiased(1);
setBold(1);
setEcartementCaracteres(15.0F);
glPushMatrix();
glColor4fv(couleurJaune());
strokeOutput(xmin,-ymax+10*tpix,0.65F,"Produit vectoriel : %f %f %f",v.dx,v.dy,v.dz);
float scal = produitScalaire(&v1,&v2);
strokeOutput(xmin,-ymax+30*tpix,0.65F,"Produit scalaire : %f",scal);
glColor4fv(couleurRouge());
strokeOutput(xmin,ymax-16*tpix,0.65F,"Vecteur 1 : %f %f %f",v1.dx,v1.dy,v1.dz);
glColor4fv(couleurBleu());
strokeOutput(xmin,ymax-36*tpix,0.65F,"Vecteur 2 : %f %f %f",v2.dx,v2.dy,v2.dz);
glPopMatrix();
setAntialiased(0);
setBold(0);
setEcartementCaracteres(0.0F);
}
void display() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
switch ( question ) {
case 0 : produitsScalaireVectoriel();
break;
case 1 : normaleFacette();
break; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void key(unsigned char k,int x,int y) {
switch ( k ) {
case 'A' :
case 'a' : if ( question )
sommet = SOMMETA;
break;
case 'B' :
case 'b' : if ( question )
sommet = SOMMETB;
break;
case 'C' :
case 'c' : if ( question )
sommet = SOMMETC;
break;
case 'x' : if ( !question )
if ( modif )
v2.dx -= 0.05F;
else
v1.dx -= 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.x -= 0.05F;
break;
case SOMMETB : f.pb.x -= 0.05F;
break;
case SOMMETC : f.pc.x -= 0.05F;
break; }
break;
case 'X' : if ( !question )
if ( modif )
v2.dx += 0.05F;
else
v1.dx += 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.x += 0.05F;
break;
case SOMMETB : f.pb.x += 0.05F;
break;
case SOMMETC : f.pc.x += 0.05F;
break; }
break;
case 'y' : if ( !question )
if ( modif )
v2.dy -= 0.05F;
else
v1.dy -= 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.y -= 0.05F;
break;
case SOMMETB : f.pb.y -= 0.05F;
break;
case SOMMETC : f.pc.y -= 0.05F;
break; }
break;
case 'Y' : if ( !question )
if ( modif )
v2.dy += 0.05F;
else
v1.dy += 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.y += 0.05F;
break;
case SOMMETB : f.pb.y += 0.05F;
break;
case SOMMETC : f.pc.y += 0.05F;
break; }
break;
case 'z' : if ( !question )
if ( modif )
v2.dz -= 0.05F;
else
v1.dz -= 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.z -= 0.05F;
break;
case SOMMETB : f.pb.z -= 0.05F;
break;
case SOMMETC : f.pc.z -= 0.05F;
break; }
break;
case 'Z' : if ( !question )
if ( modif )
v2.dz += 0.05F;
else
v1.dz += 0.05F;
else
switch (sommet) {
case SOMMETA : f.pa.z += 0.05F;
break;
case SOMMETB : f.pb.z += 0.05F;
break;
case SOMMETC : f.pc.z += 0.05F;
break; }
break;
case 45 : zoom /= 1.02F;
break;
case 43 : zoom *= 1.02F;
break;
case 32 : if ( question )
sommet = (sommet+1)%3;
else
modif = (modif+1)%2;
break;
case 0x0D : question = (question+1)%2;
break;
case '\033' : exit(0);
break ; }
glutPostRedisplay();
}
void special(int key,int x,int y) {
if ( specialManipulateur(key,x,y) )
glutPostRedisplay();
else
switch (key) {
case GLUT_KEY_F1 : if ( question )
sommet = SOMMETA;
else
modif = VECTEUR1;
glutPostRedisplay();
break;
case GLUT_KEY_F2 : if ( question )
sommet = SOMMETB;
else
modif = VECTEUR2;
glutPostRedisplay();
break;
case GLUT_KEY_F3 : if ( question )
sommet = SOMMETC;
glutPostRedisplay();
break; }
}
void myInit() {
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(400,400);
glutInitWindowPosition(50,50);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
int f = glutCreateWindow("Mathematiques 1");
myInit();
creationMenuBasique();
setParametresOrthoBasique(-6.0,6.0,-6.0,6.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}