Le source: ProjectionPerspective.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Juin 2001 */
/* Shema de realisation d'une projection */
/* en perspective */
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <math.h>
#include "ModuleCouleurs.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"
static int aff = 0;
static float pax = 6.0F;
static float pay = -4.0F;
static float paz = -14.0F;
static float pbx = -4.0F;
static float pby = 3.0F;
static float pbz = -9.0F;
static float poz = 15.0F;
void myinit(void) {
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE) ;
glEnable(GL_LIGHT0);
glClearColor(0.5F,0.5F,0.5F,0.0F);
}
void axeDeProjection(float x1,float y1,float z1,float x2,float y2,float z2,float fact) {
float dx = x2-x1;
float dy = y2-y1;
float dz = z2-z1;
glBegin(GL_LINES);
glVertex3f(x1-fact*dx,y1-fact*dy,z1-fact*dz);
glVertex3f(x1+fact*dx,y1+fact*dy,z1+fact*dz);
glEnd();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glColor4fv(couleurBlanc());
glBegin(GL_LINE_LOOP);
glVertex3f(8.0F,6.0F,-3.0F);
glVertex3f(8.0F,-6.0F,-3.0F);
glVertex3f(-8.0F,-6.0F,-3.0F);
glVertex3f(-8.0F,6.0F,-3.0F);
glEnd();
glColor4fv(couleurJaune());
glBegin(GL_LINES);
glVertex3f(pax,pay,paz);
glVertex3f(pbx,pby,pbz);
glEnd();
float fact1 = (poz+3.0F)/(poz-paz);
float xaprim = pax*fact1;
float yaprim = pay*fact1;
float fact2 = (poz+3.0F)/(poz-pbz);
float xbprim = pbx*fact2;
float ybprim = pby*fact2;
glColor4fv(couleurRouge());
glBegin(GL_LINES);
glVertex3f(xaprim,yaprim,-3.0F);
glVertex3f(xbprim,ybprim,-3.0F);
glEnd();
glColor4fv(couleurBlanc());
placeFontCursor(0.0F,-1.0F,poz+4.0F);
simpleBitmapOutput("%s","Centre de projection");
placeFontCursor(0.0F,-9.0F,0.0);
simpleBitmapOutput("%s","Plan de projection");
glEnable(GL_LIGHTING);
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRougeFonce());
glPushMatrix();
glTranslatef(0.0F,0.0F,-15.0F);
flecheEnVolume(0.0F,0.0F,25.0F,0.3F,1.5F,0.1F) ;
glPopMatrix();
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVertFonce());
glPushMatrix();
glTranslatef(pax,pay,paz);
flecheEnVolume(xaprim-pax,yaprim-pay,-3.0F-paz,0.3F,1.5F,0.1F) ;
glPopMatrix();
glPushMatrix();
glTranslatef(pbx,pby,pbz);
flecheEnVolume(xbprim-pbx,ybprim-pby,-3.0F-pbz,0.3F,1.5F,0.1F) ;
glPopMatrix();
glDisable(GL_LIGHTING);
glColor4fv(couleurJaune());
placeFontCursor(pax+0.1F,pay+0.1F,paz+0.1F);
simpleBitmapOutput("%s","A");
placeFontCursor(pbx+0.1F,pby+0.1F,pbz+0.1F);
simpleBitmapOutput("%s","B");
glColor4fv(couleurRouge());
placeFontCursor(xaprim+0.1F,yaprim+0.1F,-2.9F);
simpleBitmapOutput("%s","A'");
placeFontCursor(xbprim+0.1F,ybprim+0.1F,-2.9F);
simpleBitmapOutput("%s","B'");
axeDeProjection(0.0F,0.0F,1.0F,0.0F,0.0F,-1.0F,50);
glColor4fv(couleurVert());
axeDeProjection(pax,pay,paz,xaprim,yaprim,-3.0F,50);
axeDeProjection(pbx,pby,pbz,xbprim,ybprim,-3.0F,50);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
/* -------------------------------------------------- */
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 'e' : pax += 0.2F;
if ( pax > 8.0F )
pax = 8.0F;
glutPostRedisplay();
break;
case 'E' : pax -= 0.2F;
if ( pax < -8.0F )
pax = -8.0F;
glutPostRedisplay();
break;
case 'd' : pbx += 0.2F;
if ( pbx > 8.0F )
pbx = 8.0F;
glutPostRedisplay();
break;
case 'D' : pbx -= 0.2F;
if ( pbx < -8.0F )
pbx = -8.0F;
glutPostRedisplay();
break;
case 'r' : pay += 0.2F;
if ( pay > 8.0F )
pay = 8.0F;
glutPostRedisplay();
break;
case 'R' : pay -= 0.2F;
if ( pay < -8.0F )
pay = -8.0F;
glutPostRedisplay();
break;
case 'f' : pby += 0.2F;
if ( pby > 8.0F )
pby = 8.0F;
glutPostRedisplay();
break;
case 'F' : pby -= 0.2F;
if ( pby < -8.0F )
pby = -8.0F;
glutPostRedisplay();
break;
case 't' : paz += 0.2F;
glutPostRedisplay();
break;
case 'T' : paz -= 0.2F;
glutPostRedisplay();
break;
case 'g' : pbz += 0.2F;
glutPostRedisplay();
break;
case 'G' : pbz -= 0.2F;
glutPostRedisplay();
break;
case 32 : aff = (aff+1)%2 ;
glutPostRedisplay();
break; }
}
/* -------------------------------------------------- */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowPosition(10,10);
glutInitWindowSize(400,250);
glutCreateWindow("Schema: Projection en perspective");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-10.0,10.0,-10.0,10.0,-500.0,500.0);
setManipulateurDistance(1.0F);
setManipulateurClavierAngle(22.0F,-40.0F,0.0F) ;
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}
/* ************************************************** */