L'exécutable

Image050.gif (6207 octets)

Le source: SchemaGLUPerspective.cpp

/* Auteur: Nicolas JANEY                */
/* nicolas.janey@univ-fcomte.fr         */
/* Juin 2001                            */
/* Schema d'utilisation de la fonction  */
/* OpenGL gluPerspective                */

#include <stdio.h>
#include <math.h>

#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include "ModuleCouleurs.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"

typedef float vecteur[4];

static float g;
static float d;
static float b;
static float h;
static float cmin = 5.0F;
static float cmax = 10.0F;
static float ouverture = 30.0F;
static float ratio = 1.333F;

void myinit(void) {
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_ALPHA_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  glClearColor(1.0F,1.0F,0.9F,0.0F);
}

void axes() {
  setFont(GLUT_BITMAP_8_BY_13,CENTER);
  glColor4fv(couleurRouge());
  flecheEnVolume(1.0F,0.0F,0.0F,0.1F,0.3F,0.02F);
  glColor4fv(couleurVert());
  flecheEnVolume(0.0F,1.0F,0.0F,0.1F,0.3F,0.02F);
  glColor4fv(couleurBleu());
  flecheEnVolume(0.0F,0.0F,1.0F,0.1F,0.3F,0.02F);
  glPushAttrib(GL_DEPTH_TEST);
  glDisable(GL_DEPTH_TEST);
  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");
  glPopAttrib();
}

void recalculParametres() {
  float t = tan(ouverture*3.14159/360);
  h = t*cmin;
  b = -t*cmin;
  g = b*ratio;
  d = h*ratio;
}

void dessineOuvertureVerticale() {
  float r =(float) sqrt(cmin*cmin+h*h);
  glBegin(GL_LINE_STRIP);
  for ( int i = 0 ; i <= ouverture ; i++ ) {
    double angle = (-ouverture/2+i)*3.14159/180;
    glVertex3f(0.0F,r*sin(angle),6.0F-r*cos(angle)); }
  glEnd();
  setFont(GLUT_BITMAP_9_BY_15,LEFT);
  double angle = -ouverture*0.375*3.14159/180;
  bitmapStringOutput(0.0F,r*sin(angle),6.0F-r*cos(angle),"foc");
}

void dessineOuvertureHorizontale() {
  float r =(float) sqrt(cmin*cmin+g*g);
  double ouv = atan2(d,cmin);
  int n =(int) (2*ouv*180/3.141459);
  glBegin(GL_LINE_STRIP);
  for ( int i = 0 ; i <= n ; i++ ) {
    double angle = (-ouv+i*2*ouv/n);
    glVertex3f(r*sin(angle),0.0F,6.0F-r*cos(angle)); }
  glEnd();
  setFont(GLUT_BITMAP_9_BY_15,LEFT);
  double angle = ouv*0.375;
  bitmapStringOutput(r*sin(angle),0.0F,6.0F-r*cos(angle),"foc*ratio");
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  recalculParametres();
  glPushMatrix();
  manipulateurSouris();
  manipulateurClavier();
  glEnable(GL_CULL_FACE);
  glColor4fv(couleurRose(0.2F));
  float fact = cmax/cmin;
  glBegin(GL_QUADS);
  glVertex3f(g*fact,h*fact,6.0F-cmax);
  glVertex3f(d*fact,h*fact,6.0F-cmax);
  glVertex3f(d*fact,b*fact,6.0F-cmax);
  glVertex3f(g*fact,b*fact,6.0F-cmax);
  glEnd();
  glBegin(GL_QUADS);
  glVertex3f(g*fact,b*fact,6.0F-cmax);
  glVertex3f(g,b,6.0F-cmin);
  glVertex3f(g,h,6.0F-cmin);
  glVertex3f(g*fact,h*fact,6.0F-cmax);
  glEnd();
  glBegin(GL_QUADS);
  glVertex3f(d*fact,h*fact,6.0F-cmax);
  glVertex3f(d,h,6.0F-cmin);
  glVertex3f(d,b,6.0F-cmin);
  glVertex3f(d*fact,b*fact,6.0F-cmax);
  glEnd();
  glBegin(GL_QUADS);
  glVertex3f(g*fact,h*fact,6.0F-cmax);
  glVertex3f(g,h,6.0F-cmin);
  glVertex3f(d,h,6.0F-cmin);
  glVertex3f(d*fact,h*fact,6.0F-cmax);
  glEnd();
  glBegin(GL_QUADS);
  glVertex3f(d*fact,b*fact,6.0F-cmax);
  glVertex3f(d,b,6.0F-cmin);
  glVertex3f(g,b,6.0F-cmin);
  glVertex3f(g*fact,b*fact,6.0F-cmax);
  glEnd();
  glDisable(GL_CULL_FACE);
  glClear(GL_DEPTH_BUFFER_BIT);
  glColor4fv(couleurBleuCiel(0.5F));
  glBegin(GL_QUADS);
  glVertex3f(g,b,6.0F-cmin);
  glVertex3f(d,b,6.0F-cmin);
  glVertex3f(d,h,6.0F-cmin);
  glVertex3f(g,h,6.0F-cmin);
  glEnd();
  glClear(GL_DEPTH_BUFFER_BIT);
  glColor4fv(couleurBleu());
  setFont(GLUT_BITMAP_8_BY_13,CENTER);
  bitmapStringOutput(0.0F,h/2,6.0F-cmin,"ecran");
  bitmapStringOutput(0.0F,-0.5F,4.0F,"axe de visee");
  glPushMatrix();
  glTranslatef(0.0F,0.0F,6.0F);
  flecheEnVolume(0.0F,0.0F,-4.0F,0.1F,0.3F,0.03F);
  glPopMatrix();
  glColor4fv(couleurRouge());
  placeFontCursor(d/2,h*(1+fact)/2,6.0F-(cmin+cmax)/2) ;
  bitmapStringOutput("volume de");
  deplacementCursor(0,13,0);
  bitmapStringOutput("visualisation");
  glColor4fv(couleurNoir());
  setFont(GLUT_BITMAP_9_BY_15,CENTER);
  bitmapStringOutput((d*fact)+2.0F,-0.3F,6.0F-cmin,"-cmin");
  bitmapStringOutput((d*fact)+2.0F,-0.3F,6.0F-cmax,"-cmax");
  glColor4fv(couleurRouge());
  glLineWidth(2.0);
  glBegin(GL_LINE_LOOP);
  glVertex3f(g,b,6.0F-cmin);
  glVertex3f(d,b,6.0F-cmin);
  glVertex3f(d,h,6.0F-cmin);
  glVertex3f(g,h,6.0F-cmin);
  glEnd();
  glBegin(GL_LINE_LOOP);
  glVertex3f(g*fact,b*fact,6.0F-cmax);
  glVertex3f(d*fact,b*fact,6.0F-cmax);
  glVertex3f(d*fact,h*fact,6.0F-cmax);
  glVertex3f(g*fact,h*fact,6.0F-cmax);
  glEnd();
  glBegin(GL_LINES);
  glVertex3f(g,b,6.0F-cmin);
  glVertex3f(g*fact,b*fact,6.0F-cmax);
  glVertex3f(d,b,6.0F-cmin);
  glVertex3f(d*fact,b*fact,6.0F-cmax);
  glVertex3f(d,h,6.0F-cmin);
  glVertex3f(d*fact,h*fact,6.0F-cmax);
  glVertex3f(g,h,6.0F-cmin);
  glVertex3f(g*fact,h*fact,6.0F-cmax);
  glEnd();
  glLineWidth(1.0);
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1,0x5555);
  glBegin(GL_LINES);
  glVertex3f(0.0F,0.0F,6.0F);
  glVertex3f(1000*d,1000*b,6.0F-1000*cmin);
  glVertex3f(0.0F,0.0F,6.0F);
  glVertex3f(1000*d,1000*h,6.0F-1000*cmin);
  glVertex3f(0.0F,0.0F,6.0F);
  glVertex3f(1000*g,1000*b,6.0F-1000*cmin);
  glVertex3f(0.0F,0.0F,6.0F);
  glVertex3f(1000*g,1000*h,6.0F-1000*cmin);
  glColor4fv(couleurBleu());
  glVertex3f(0.0F,0.0F,-10000.0F);
  glVertex3f(0.0F,0.0F,10000.0F);
  glColor4fv(couleurNoir());
  glVertex3f(0.0F,0.0F,6.0F-cmin);
  glVertex3f((d*fact)+1.8F,0.0F,6.0F-cmin);
  glVertex3f((d*fact)+1.5F,-0.3F,6.0F-cmin);
  glVertex3f((d*fact)+1.5F,0.3F,6.0F-cmin);
  glVertex3f(0.0F,0.0F,6.0F-cmax);
  glVertex3f((d*fact)+1.8F,0.0F,6.0F-cmax);
  glVertex3f((d*fact)+1.5F,-0.3F,6.0F-cmax);
  glVertex3f((d*fact)+1.5F,0.3F,6.0F-cmax);
  glVertex3f((d*fact)+1.5F,0.0F,-10000.0F);
  glVertex3f((d*fact)+1.5F,0.0F,10000.0F);
  glVertex3f(1.0F,0.0F,6.0F);
  glVertex3f((d*fact)+2.8F,0.0F,6.0F);
  glEnd();
  dessineOuvertureVerticale();
  dessineOuvertureHorizontale();
  glDisable(GL_LINE_STIPPLE);
  glPushMatrix();
  glTranslatef(0.0F,0.0F,6.0F);
  axes();
  glPopMatrix();
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

void key(unsigned char key,int x,int y) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 'o'  : ouverture += 1.0F;
                  if ( ouverture > 88.0F )
                    ouverture = 88.0F ;
                  glutPostRedisplay();
                  break;
      case 'O'  : ouverture -= 1.0F;
                  if ( ouverture < 0.0F )
                    ouverture = 0.0F ;
                  glutPostRedisplay();
                  break;
      case 'r'  : ratio *= 1.02F;
                  glutPostRedisplay();
                  break;
      case 'R'  : ratio /= 1.02F;
                  glutPostRedisplay();
                  break;
      case 'i'  : cmin += 0.1F;
                  glutPostRedisplay();
                  break;
      case 'I'  : cmin -= 0.1F;
                  glutPostRedisplay();
                  break;
      case 'a'  : cmax += 0.1F;
                  glutPostRedisplay();
                  break;
      case 'A'  : cmax -= 0.1F;
                  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: Fonction OpenGL gluPerspective");
  myinit();
  creationMenuBasique();
  setParametresOrthoBasique(-4.5,4.5,-4.5,4.5,-500.0,500.0);
  setManipulateurDistance(1.0F);
  setManipulateurClavierAngle(16.0F,-48.0F,0.0F) ;
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

/* ************************************************** */

Les modules utilitaires : Modules.zip

WB01624_.gif (281 octets) RETOUR