L'exécutable

Le source : SchemaPlacageTextureQuadrilatere.cpp

/* Auteur: Nicolas JANEY           */
/* nicolas.janey@univ-fcomte.fr    */
/* Juillet 2001                    */
/* Placage de texture              */
/* Changement de repere rectangle  */
/* vers quadrilatere               */

#include <stdio.h>
#include <stdlib.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 "ModuleMatriceVecteur.h"
#include "ModuleMenus.h"
#include "ModuleFont.h"
#include "ModuleFleche.h"

struct coord3D {
  float x;
  float y;
  float z; } ;

struct facette4 {
  coord3D p[4]; } ;

static int pt = 0;
static int cpt = 0;
static int anim = 0;
static int max = 50;
static GLbyte *image1 ;
static GLbyte *image2 ;
static facette4 f = {{{ 0.0F,0.0F,0.0F},
                      { 1.0F,0.0F,0.0F},
                      { 1.0F,1.0F,0.0F},
                      { 0.0F,1.0F,0.0F}}};
static facette4 fp = {{{ 0.0F,0.0F,0.0F},
                       { 1.0F,0.0F,0.0F},
                       { 1.0F,1.0F,0.0F},
                       { 0.0F,1.0F,0.0F}}};
static coord3D pi = { 2.0F,-2.0F,0.0F };
static coord3D pf = { -5.5F,-1.5F,3.5F };
static float rx = 5.0F;
static float ry = 45.0F;
static float rz = 5.0F;
static float scale = 1.25F;

GLbyte *makeRasterImage1() {
  GLbyte *image =(GLbyte *) malloc(3072*sizeof(GLbyte));
  for ( int i = 0 ; i < 32 ; i++ )
    for ( int j = 0 ; j < 32 ; j++ ) {
      int p = 3*(j+i*32);
      image[p] = image[p+1] = image[p+2] = ((((i/4)+(j/4))%2) == 0 ) ? 0xFF : 0x00 ; }
  return(image);
}

GLbyte *makeRasterImage2() {
  GLbyte *image =(GLbyte *) malloc(4096*sizeof(GLbyte));
  for ( int i = 0 ; i < 32 ; i++ )
    for ( int j = 0 ; j < 32 ; j++ ) {
      int p = 4*(j+i*32);
      image[p] = image[p+1] = image[p+2] = ((((i/4)+(j/4))%2) == 0 ) ? 0xFF : 0x00 ;
      image[p+3] = 0x7F; }
  return(image);
}

void myinit(void) {
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_NORMALIZE) ;
  glEnable(GL_LIGHT0);
  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);
  image1 = makeRasterImage1();
  image2 = makeRasterImage2();
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
}

void axe3D() {
  setFont(GLUT_BITMAP_8_BY_13,CENTER);
  glColor4fv(couleurRouge());
  flecheEnVolume(5.0F,0.0F,0.0F,0.1F,0.3F,0.015F);
  glColor4fv(couleurVert());
  flecheEnVolume(0.0F,5.0F,0.0F,0.1F,0.3F,0.015F);
  glColor4fv(couleurBleu());
  flecheEnVolume(0.0F,0.0F,5.0F,0.1F,0.3F,0.015F);
  glPushAttrib(GL_DEPTH_TEST);
  glDisable(GL_DEPTH_TEST);
  glColor4fv(couleurRougeFonce());
  bitmapStringOutput(5.3F,0.0F,0.0F,"x");
  glColor4fv(couleurVertFonce());
  bitmapStringOutput(0.0F,5.3F,0.0F,"y");
  glColor4fv(couleurBleuFonce());
  bitmapStringOutput(0.0F,0.0F,5.3F,"z");
  glPopAttrib();
}

void axe2D() {
  setFont(GLUT_BITMAP_8_BY_13,CENTER);
  glColor4fv(couleurRouge());
  flecheEnVolume(5.0F,0.0F,0.0F,0.1F,0.3F,0.015F);
  glColor4fv(couleurVert());
  flecheEnVolume(0.0F,5.0F,0.0F,0.1F,0.3F,0.015F);
  glPushAttrib(GL_DEPTH_TEST);
  glDisable(GL_DEPTH_TEST);
  glColor4fv(couleurRougeFonce());
  bitmapStringOutput(5.3F,0.0F,0.0F,"x");
  glColor4fv(couleurVertFonce());
  bitmapStringOutput(0.0F,5.3F,0.0F,"y");
  glPopAttrib();
}

void afficheSommet(coord3D *p) {
  glPushMatrix();
  glTranslatef(p->x,p->y,p->z);
  glEnable(GL_LIGHTING);
  glutSolidSphere(0.1,10,10);
  glDisable(GL_LIGHTING);
  glPopMatrix();
}

void positionFinale(coord3D *p,coord3D *ps) {
  matrice n;
  toRotationY(n,ry);
  matrice nn;
  toRotationX(nn,rx);
  produitMatriceMatrice(n,nn,n);
  toRotationZ(nn,rz);
  produitMatriceMatrice(n,nn,n);
  toScale(nn,4*scale,4*scale,4*scale);
  produitMatriceMatrice(n,nn,n);
  vecteur v = { p->x,p->y,p->z,1.0F };
  produitMatriceVecteur(n,v,v);
  ps->x = v[0]+pf.x;
  ps->y = v[1]+pf.y;
  ps->z = v[2]+pf.z;
}

void positionInitiale(coord3D *p,coord3D *ps) {
  ps->x = p->x*4+pi.x;
  ps->y = p->y*4+pi.y;
  ps->z = p->z*4+pi.z;
}

void positionIntermediaire(coord3D *pi,coord3D *pf,coord3D *ps,int cpt,int max) {
  float fact =(float) cpt/max;
  ps->x = pi->x+(pf->x-pi->x)*fact;
  ps->y = pi->y+(pf->y-pi->y)*fact;
  ps->z = pi->z+(pf->z-pi->z)*fact;
}

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glTexImage2D(GL_TEXTURE_2D,0,3,32,32,0,GL_RGB,GL_UNSIGNED_BYTE,image1);
  glPushMatrix();
  manipulateurSouris();
  manipulateurClavier();
  glPushMatrix();
  coord3D pt1,pt2,pt3,pt4;
  positionInitiale(&f.p[0],&pt1);
  positionInitiale(&f.p[1],&pt2);
  positionInitiale(&f.p[2],&pt3);
  positionInitiale(&f.p[3],&pt4);
  coord3D pp1,pp2,pp3,pp4;
  positionFinale(&fp.p[0],&pp1);
  positionFinale(&fp.p[1],&pp2);
  positionFinale(&fp.p[2],&pp3);
  positionFinale(&fp.p[3],&pp4);
  { glPushMatrix();
    glColor4fv(couleurBlanc());
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex3fv((float *) &pt1);
    glTexCoord2f(1.0f,0.0f);
    glVertex3fv((float *) &pt2);
    glTexCoord2f(1.0f,1.0f);
    glVertex3fv((float *) &pt3);
    glTexCoord2f(0.0f,1.0f);
    glVertex3fv((float *) &pt4);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glColor4fv(couleurGrisMoyen());
    glBegin(GL_LINE_LOOP);
    glVertex3fv((float *) &pt1);
    glVertex3fv((float *) &pt2);
    glVertex3fv((float *) &pt3);
    glVertex3fv((float *) &pt4);
    glEnd();
    glColor4fv(couleurRouge());
    glBegin(GL_LINE_LOOP);
    glVertex3fv((float *) &pt1);
    glVertex3fv((float *) &pt2);
    glVertex3fv((float *) &pt3);
    glVertex3fv((float *) &pt4);
    glEnd();
    glLineWidth(1.0);
    glColor4fv(couleurBlanc());
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex3fv((float *) &pp1);
    glTexCoord2f(1.0f,0.0f);
    glVertex3fv((float *) &pp2);
    glTexCoord2f(1.0f,1.0f);
    glVertex3fv((float *) &pp3);
    glTexCoord2f(0.0f,1.0f);
    glVertex3fv((float *) &pp4);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glColor4fv(couleurGrisMoyen());
    glBegin(GL_LINE_LOOP);
    glVertex3fv((float *) &pp1);
    glVertex3fv((float *) &pp2);
    glVertex3fv((float *) &pp3);
    glVertex3fv((float *) &pp4);
    glEnd();
    glPopMatrix(); }
  afficheSommet(&pt1);
  afficheSommet(&pt2);
  afficheSommet(&pt3);
  afficheSommet(&pt4);
  afficheSommet(&pp1);
  afficheSommet(&pp2);
  afficheSommet(&pp3);
  afficheSommet(&pp4);
  glEnable(GL_LINE_STIPPLE);
  glLineStipple(1,0x5555);
  glBegin(GL_LINES);
  glVertex3fv((float *) &pt1);
  glVertex3fv((float *) &pp1);
  glVertex3fv((float *) &pt2);
  glVertex3fv((float *) &pp2);
  glVertex3fv((float *) &pt3);
  glVertex3fv((float *) &pp3);
  glVertex3fv((float *) &pt4);
  glVertex3fv((float *) &pp4);
  glEnd();
  glDisable(GL_LINE_STIPPLE);
  if ( anim ) { 
    glTexImage2D(GL_TEXTURE_2D,0,4,32,32,0,GL_RGBA,GL_UNSIGNED_BYTE,image2);
    glPushMatrix();
    coord3D pint1,pint2,pint3,pint4;
    positionIntermediaire(&pt1,&pp1,&pint1,cpt,max);
    positionIntermediaire(&pt2,&pp2,&pint2,cpt,max);
    positionIntermediaire(&pt3,&pp3,&pint3,cpt,max);
    positionIntermediaire(&pt4,&pp4,&pint4,cpt,max);
    glColor4fv(couleurBlanc());
    glEnable(GL_TEXTURE_2D);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex3fv((float *) &pint1);
    glTexCoord2f(1.0f,0.0f);
    glVertex3fv((float *) &pint2);
    glTexCoord2f(1.0f,1.0f);
    glVertex3fv((float *) &pint3);
    glTexCoord2f(0.0f,1.0f);
    glVertex3fv((float *) &pint4);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glPopMatrix(); }
  { glPushMatrix();
    glTranslatef(-6.0F,-2.0F,0.0F);
    axe3D();
    glPopMatrix(); }
  { glPushMatrix();
    glTranslatef(2.0F,-2.0F,0.0F);
    axe2D();
    glPopMatrix(); }
  glPopMatrix();
  glColor4fv(couleurRouge());
  placeFontCursor(pt1.x,pt1.y-0.3F,pt1.z) ;
  simpleBitmapOutput(REGULAR8x13,"A") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"ima") ;
  placeFontCursor(pt2.x,pt2.y-0.3F,pt2.z) ;
  simpleBitmapOutput(REGULAR8x13,"B") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"ima") ;
  placeFontCursor(pt3.x,pt3.y-0.3F,pt3.z) ;
  simpleBitmapOutput(REGULAR8x13,"C") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"ima") ;
  placeFontCursor(pt4.x,pt4.y-0.3F,pt4.z) ;
  simpleBitmapOutput(REGULAR8x13,"D") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"ima") ;
  placeFontCursor(pp1.x,pp1.y-0.3F,pp1.z) ;
  simpleBitmapOutput(REGULAR8x13,"A") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"fac") ;
  placeFontCursor(pp2.x,pp2.y-0.3F,pp2.z) ;
  simpleBitmapOutput(REGULAR8x13,"B") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"fac") ;
  placeFontCursor(pp3.x,pp3.y-0.3F,pp3.z) ;
  simpleBitmapOutput(REGULAR8x13,"C") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"fac") ;
  placeFontCursor(pp4.x,pp4.y-0.3F,pp4.z) ;
  simpleBitmapOutput(REGULAR8x13,"D") ;
  deplacementCursor(14,3,0) ;
  simpleBitmapOutput(REGULAR6x10,"fac") ;
  glColor4fv(couleurBleu());
  coord3D pi = { 0.5F,-0.2F,0.0F } ;
  coord3D pima ;
  coord3D pfac ;
  positionInitiale(&pi,&pima);
  positionFinale(&pi,&pfac);
  placeFontCursor(pima.x,pima.y,pima.z) ;
  simpleBitmapOutput(REGULAR8x13,"IMAGE") ;
  placeFontCursor(pfac.x,pfac.y,pfac.z) ;
  simpleBitmapOutput(REGULAR8x13,"FACETTE") ;
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
}

void idle(void) {
  cpt++;
  if ( cpt == max+1 ) {
    glutIdleFunc(NULL);
    anim = 0; }
  glutPostRedisplay();
}

void key(unsigned char key,int x,int y) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 43   : scale *= 1.02F;
                  glutPostRedisplay();
                  break;
      case 45   : scale /= 1.02F;
                  glutPostRedisplay();
                  break;
      case 'a'  : rx += 1.0F;
                  glutPostRedisplay();
                  break;
      case 'A'  : rx -= 1.0F;
                  glutPostRedisplay();
                  break;
      case 'b'  : ry += 1.0F;
                  glutPostRedisplay();
                  break;
      case 'B'  : rz -= 1.0F;
                  glutPostRedisplay();
                  break;
      case 'c'  : rz += 1.0F;
                  glutPostRedisplay();
                  break;
      case 'C'  : rz -= 1.0F;
                  glutPostRedisplay();
                  break;
      case 'f'  : fp.p[pt].x -= 0.01F; 
                  glutPostRedisplay();
                  break;
      case 'g'  : fp.p[pt].x += 0.01F; 
                  glutPostRedisplay();
                  break;
      case 'v'  : fp.p[pt].y -= 0.01F; 
                  glutPostRedisplay();
                  break;
      case 't'  : fp.p[pt].y += 0.01F; 
                  glutPostRedisplay();
                  break;
      case 0x0D : cpt = 0;
                  anim = 1;
                  glutIdleFunc(idle);
                  break;
      case 32   : pt = (pt+1)%4 ; 
                  glutPostRedisplay();
                  break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
  glutInitWindowPosition(10,10);
  glutInitWindowSize(450,240);
  glutCreateWindow("Rectangle vers quadrilatere plan en placage de texture");
  myinit();
  creationMenuBasique();
  setParametresOrthoBasique(-4.0,4.0,-4.0,4.0,-500.0,500.0);
  setManipulateurDistance(1.0F);
  setManipulateurClavierAngle(25.0F,-10.0F,0.0F) ;
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

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

Les modules utilitaires : Modules.zip

RETOUR