/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Calculs mathematiques sur les vecteurs */
#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 "ModuleManipulateur.h"
#include "ModuleMatriceVecteur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
#include "ModuleCouleurs.h"
static int aff = 0;
static int axe = 0;
static int f1;
static int f2;
static vecteur p1 = { 1.1F,-1.1F,0.8F,1.0F };
static vecteur p2 = { -0.9F,1.7F,-0.1F,1.0F };
static vecteur p3 = { 1.1F, 0.1F,0.8F,1.0F };
static vecteur p4 = { 1.1F, 2.1F,0.8F,1.0F };
static vecteur v1 = { 1.2F,1.3F,0.6F,0.0F };
static vecteur v2 = { -1.1F,-0.7F,1.4F,0.0F };
static int param = 1;
static int pparam = 0;
static int nbparam[2] = { 3,3 };
static float *tp[2] = { p2,p1 };
static float *tv[2] = { v2,v1 };
void axes(void) {
if ( axe ) {
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(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);
glPopAttrib(); }
}
void affichageSommet(vecteur p) {
glPushAttrib(GL_LIGHTING);
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(p[0],p[1],p[2]);
glutSolidSphere(0.1F,10,10);
glPopMatrix();
glPopAttrib();
}
void displayNormalisation(void) {
glPushAttrib(GL_LIGHTING);
glEnable(GL_LIGHTING);
affichageSommet(p1);
affichageSommet(p2);
vecteur v;
calculVecteur(p1,p2,v);
vecteur n;
calculVecteurNorme(p1,p2,n);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(p1[0],p1[1],p1[2]);
flecheEnVolume(v[0],v[1],v[2],0.1F,0.3F,0.02F);
glPopMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
glPushMatrix();
glTranslatef(p1[0],p1[1],p1[2]);
flecheEnVolume(n[0],n[1],n[2],0.1F,0.3F,0.04F);
glPopMatrix();
glPopAttrib();
}
void displayProduitScalaire(void) {
glPushAttrib(GL_LIGHTING);
glEnable(GL_LIGHTING);
affichageSommet(p3);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(p3[0],p3[1],p3[2]);
flecheEnVolume(v1[0],v1[1],v1[2],0.1F,0.3F,0.02F);
glPopMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
glPushMatrix();
glTranslatef(p3[0],p3[1],p3[2]);
flecheEnVolume(v2[0],v2[1],v2[2],0.1F,0.3F,0.03F);
glPopMatrix();
glPopAttrib();
}
void displayProduitVectoriel(void) {
glPushAttrib(GL_LIGHTING);
glEnable(GL_LIGHTING);
affichageSommet(p4);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(p4[0],p4[1],p4[2]);
flecheEnVolume(v1[0],v1[1],v1[2],0.1F,0.3F,0.02F);
glPopMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
glPushMatrix();
glTranslatef(p4[0],p4[1],p4[2]);
flecheEnVolume(v2[0],v2[1],v2[2],0.1F,0.3F,0.03F);
glPopMatrix();
vecteur pv;
produitVectoriel(v1,v2,pv);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
glPushMatrix();
glTranslatef(p4[0],p4[1],p4[2]);
flecheEnVolume(pv[0],pv[1],pv[2],0.1F,0.3F,0.03F);
glPopMatrix();
glPopAttrib();
}
void display1() {
glClearColor(0.5,0.5,0.5,1.0) ;
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
axes();
switch (aff) {
case 0 : displayNormalisation();
break;
case 1 : displayProduitScalaire();
break;
case 2 : displayProduitVectoriel();
break; }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void text0(void) {
glPushMatrix();
glColor4fv(couleurJaune());
glBegin(GL_LINE_LOOP);
glVertex2f(2.0F,5.0F+param*20.0F);
glVertex2f(358.0F,5.0F+param*20.0F);
glVertex2f(358.0F,25.0F+param*20.0F);
glVertex2f(2.0F,25.0F+param*20.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(148.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,24.0F+param*20.0F);
glVertex2f(148.0F+70.0*pparam,24.0F+param*20.0F);
glEnd();
glPopMatrix();
glPushMatrix();
float pos = 1;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"1ER SOMMET : %6.2f %6.2f %6.2f",p1[0],p1[1],p1[2]) ;
pos = 0;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"2EME SOMMET : %6.2f %6.2f %6.2f",p2[0],p2[1],p2[2]) ;
vecteur v;
calculVecteur(p1,p2,v);
pos = -1.5;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"VECTEUR : %6.2f %6.2f %6.2f",v[0],v[1],v[2]) ;
vecteur n;
calculVecteurNorme(p1,p2,n);
pos = -2.5;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"DISTANCE : %6.3f",distance(p1,p2)) ;
pos = -3.5;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"NORME : %6.3f %6.3f %6.3f",n[0],n[1],n[2]) ;
glPopMatrix();
}
void text1(void) {
glPushMatrix();
glColor4fv(couleurJaune());
glBegin(GL_LINE_LOOP);
glVertex2f(2.0F,5.0F+param*20.0F);
glVertex2f(358.0F,5.0F+param*20.0F);
glVertex2f(358.0F,25.0F+param*20.0F);
glVertex2f(2.0F,25.0F+param*20.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(148.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,24.0F+param*20.0F);
glVertex2f(148.0F+70.0*pparam,24.0F+param*20.0F);
glEnd();
glPopMatrix();
glPushMatrix();
float pos = 1;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"1ER VECTEUR : %6.2f %6.2f %6.2f",v1[0],v1[1],v1[2]) ;
pos = 0;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"2EME VECTEUR : %6.2f %6.2f %6.2f",v2[0],v2[1],v2[2]) ;
pos = -1.5;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"PRODUIT SCALAIRE : %6.3f",produitScalaire(v1,v2)) ;
glPopMatrix();
}
void text2(void) {
glPushMatrix();
glColor4fv(couleurJaune());
glBegin(GL_LINE_LOOP);
glVertex2f(2.0F,5.0F+param*20.0F);
glVertex2f(358.0F,5.0F+param*20.0F);
glVertex2f(358.0F,25.0F+param*20.0F);
glVertex2f(2.0F,25.0F+param*20.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(148.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,6.0F+param*20.0F);
glVertex2f(216.0F+70.0*pparam,24.0F+param*20.0F);
glVertex2f(148.0F+70.0*pparam,24.0F+param*20.0F);
glEnd();
glPopMatrix();
glPushMatrix();
float pos = 1;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"1ER VECTEUR : %6.2f %6.2f %6.2f",v1[0],v1[1],v1[2]) ;
pos = 0;
glColor4fv((param == pos) ? couleurJaune() : couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"2EME VECTEUR : %6.2f %6.2f %6.2f",v2[0],v2[1],v2[2]) ;
pos = -1.5;
glColor4fv(couleurBlanc());
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13,"PRODUIT VECTORIEL") ;
pos = -2.5;
vecteur pv;
produitVectoriel(v1,v2,pv);
placeFontCursor(5.0F,10.0F+pos*20.0F,0.0F) ;
simpleBitmapOutput(REGULAR8x13," : %6.2f %6.2f %6.2f",pv[0],pv[1],pv[2]) ;
glPopMatrix();
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushAttrib(GL_LIGHTING);
glDisable(GL_LIGHTING);
switch (aff) {
case 0 : text0();
break;
case 1 : text1();
break;
case 2 : text2();
break; }
glPopAttrib();
glutSwapBuffers();
}
void special(int k,int x,int y) {
switch (k) {
case GLUT_KEY_UP : param = (param+1)%2;
pparam = pparam%nbparam[param];
glutPostWindowRedisplay(f2);
break;
case GLUT_KEY_DOWN : param = (param+1)%2;
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 augmente(float *v,float dif,float max) {
*v += dif;
if ( *v > max )
*v = max;
}
void diminue(float *v,float dif,float min) {
*v -= dif;
if ( *v < min )
*v = min;
}
void key(unsigned char k,int x,int y) {
switch ( k ) {
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 43 : if ( aff == 0 )
augmente(&tp[param][pparam],0.05F,100.0F);
else
augmente(&tv[param][pparam],0.05F,100.0F);
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 45 : if ( aff == 0 )
diminue(&tp[param][pparam],0.05F,-100.0F);
else
diminue(&tv[param][pparam],0.05F,-100.0F);
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case ' ' : axe = (axe+1)%2;
glutPostWindowRedisplay(f1);
break;
case 0x0D : aff = (aff+1)%3;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case '\033' : exit(0);
break ; }
}
void myinit() {
GLfloat l_pos[] = { 1.0,1.0,1.0,0.0 };
glLightfv(GL_LIGHT0,GL_POSITION,l_pos);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,50-h,50,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
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);
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(250,250);
glutInitWindowPosition(20,50);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f1 = glutCreateWindow("Mathematiques");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-2.5,2.5,-2.5,2.5,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(25.0F,-45.0F,0.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutKeyboardFunc(key1);
glutDisplayFunc(display1);
glutSpecialFunc(special1);
glutInitWindowSize(360,115);
glutInitWindowPosition(300,150);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
myinit();
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutMainLoop();
return(0);
}