Le source : NormaleVersPlan.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Decembre 2004 */
/* Calcul de l'equation cartesienne d'un plan */
/* a partir de la normale a ce plan */
/* et de la position d'un point */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ModuleCouleurs.h"
#include "ModuleFleche.h"
#include "ModuleFont.h"
#include "ModuleMatriceVecteur.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
typedef struct Vecteur {
float x;
float y;
float z;
float t; } Vecteur;
typedef struct Position {
float x;
float y;
float z;
float t; } Position;
static Vecteur n ;
static int f1;
static int f2;
static float nRx = 0.0F;
static float nRy = 0.0F;
static float nRz = 0.0F;
static Position p = { -0.1F,0.1F,0.1F,1.0F } ;
static Position pt = { -0.3F,0.1F,0.3F,1.0F } ;
static float a;
static float b;
static float c;
static float d;
void myinit(void) {
GLfloat light_Position0[] = { 1.0F,0.0F,1.0F,0.0F };
GLfloat light_Position1[] = { -1.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
glLightfv(GL_LIGHT0,GL_POSITION,light_Position0);
glLightfv(GL_LIGHT1,GL_POSITION,light_Position1);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_ALPHA_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);
}
void display(void) {
vecteur N = { 0.0F,1.0F,0.0F,0.0F };
matrice mRx;
toRotationX(mRx,nRx) ;
produitMatriceVecteur(mRx,N,N) ;
matrice mRy;
toRotationY(mRy,nRy) ;
produitMatriceVecteur(mRy,N,N) ;
matrice mRz;
toRotationZ(mRz,nRz) ;
produitMatriceVecteur(mRz,N,N) ;
a = n.x = N[0];
b = n.y = N[1];
c = n.z = N[2];
d = -a*p.x-b*p.y-c*p.z;
n.t = 0.0F;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glPushMatrix() ;
glTranslatef(0.0F,-0.2F,0.0F);
manipulateurSouris();
manipulateurClavier();
glPushMatrix();
glTranslatef(pt.x,pt.y,pt.z) ;
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge()) ;
glEnable(GL_CULL_FACE);
glutSolidSphere(0.05,10,10);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glPopMatrix();
glPushMatrix();
glTranslatef(p.x,p.y,p.z) ;
glEnable(GL_LIGHTING);
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune()) ;
glEnable(GL_CULL_FACE);
glutSolidSphere(0.05,10,10);
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glPopMatrix();
glColor4fv(couleurMagenta()) ;
placeFontCursor(n.x/2.0F,n.y/2.0F,n.z/2.0F) ;
deplacementCursor(10,0,0) ;
simpleBitmapOutput(1,REGULAR8x13,"N") ;
deplacementCursor(7,-5,0) ;
simpleBitmapOutput(1,DESSIN,"TF") ;
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurMagenta()) ;
flecheEnVolume(n.x,n.y,n.z,0.04F,0.15F,0.01F);
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(0.5F)) ;
glTranslatef(p.x,p.y,p.z) ;
glRotatef(nRz,0.0F,0.0F,1.0F);
glRotatef(nRy,0.0F,1.0F,0.0F);
glRotatef(nRx,1.0F,0.0F,0.0F);
glScalef(1.6F,0.00001F,1.4F) ;
glEnable(GL_CULL_FACE);
glutSolidCube(1.0) ;
glDisable(GL_CULL_FACE);
glPopMatrix();
glDisable(GL_LIGHTING);
glPopMatrix() ;
glDisable(GL_DEPTH_TEST);
glFlush();
glutSwapBuffers() ;
glutPostWindowRedisplay(f2);
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '1' : p.x -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case '4' : p.x += 0.02F ;
glutPostWindowRedisplay(f1);
break;
case '2' : p.y -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case '5' : p.y += 0.02F ;
glutPostWindowRedisplay(f1);
break;
case '3' : p.z -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case '6' : p.z += 0.02F ;
glutPostWindowRedisplay(f1);
break;
case 'x' : nRx += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'X' : nRx -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'y' : nRy += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'Y' : nRy -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'z' : nRz += 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 'Z' : nRz -= 2.0F ;
glutPostWindowRedisplay(f1);
break;
case 0x1B : exit(0);
break; }
}
void special2(int key,int x,int y) {
switch (key) {
case GLUT_KEY_F1 : pt.x -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F2 : pt.x += 0.02F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F3 : pt.y -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F4 : pt.y += 0.02F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F5 : pt.z -= 0.02F ;
glutPostWindowRedisplay(f1);
break;
case GLUT_KEY_F6 : pt.z += 0.02F ;
glutPostWindowRedisplay(f1);
break; }
}
void special(int key,int x,int y) {
if ( specialManipulateur(key,x,y) ) {
glutPostWindowRedisplay(f1); }
else
special2(key,x,y);
}
void reshape2(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();
}
void display2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glClearColor(0.0F,0.0F,0.0F,1.0F) ;
glPushMatrix();
glColor4fv(couleurMagenta());
float pos = 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"NORMALE : %6.3f %6.3f %6.3f",n.x,n.y,n.z) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
glColor4fv(couleurJaune());
simpleBitmapOutput(1,REGULAR8x13,"POINT : %6.3f %6.3f %6.3f",p.x,p.y,p.z) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
glColor4fv(couleurBlanc());
simpleBitmapOutput(1,REGULAR8x13,"PLAN : %6.3f %6.3f %6.3f %6.3f",a,b,c,d) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
glColor4fv(couleurRouge());
simpleBitmapOutput(1,REGULAR8x13,"POS : %6.3f %6.3f %6.3f",pt.x,pt.y,pt.z) ;
float val = a*pt.x+b*pt.y+c*pt.z+d;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
simpleBitmapOutput(1,REGULAR8x13,"VALEUR : %6.3f",val) ;
pos += 1.0F;
placeFontCursor(5.0F,-pos*20.0F,0.0F) ;
if ( fabs(val) < 0.000001F )
simpleBitmapOutput(1,REGULAR8x13,"-> SUR LE PLAN") ;
else {
if ( val < 0.0F )
simpleBitmapOutput(1,REGULAR8x13,"-> EN DESSOUS DU PLAN") ;
if ( val > 0.0F )
simpleBitmapOutput(1,REGULAR8x13,"-> AU DESSUS DU PLAN") ; }
glPopMatrix();
glutSwapBuffers();
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowPosition(50,50);
glutInitWindowSize(300,240);
f1 = glutCreateWindow("Calcul de l'équation d'un plan");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.0,1.0,-1.0,1.0,-5.0,5.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(25.0F,15.0F,0.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutInitWindowSize(380,130);
glutInitWindowPosition(60,320);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f2 = glutCreateWindow("Valeurs");
creationMenuBasique();
glutDisplayFunc(display2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special2);
glutMainLoop();
return(0);
}