Le source : SchemaPlacageTextureRectangle.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Juillet 2001 */
/* Placage de texture */
/* Changement de repere rectangle */
/* vers rectangle */
#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 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},
{ 0.5F,0.9F,0.0F},
{ 0.2F,0.6F,0.0F}}};
static coord3D pi = { 2.0F,-2.0F,0.0F };
static coord3D pf = { -7.5F,0.5F,3.5F };
static float rx = -20.0F;
static float ry = 40.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;
toTranslation(n,pi.x+pf.x,pi.y+pf.y,pi.z+pf.z);
matrice nn;
toRotationY(nn,ry);
produitMatriceMatrice(n,nn,n);
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];
ps->y = v[1];
ps->z = v[2];
}
void positionInitiale(coord3D *p,coord3D *ps) {
matrice n;
toTranslation(n,pi.x,pi.y,pi.z);
matrice nn;
toScale(nn,4.0F,4.0F,4.0F);
produitMatriceMatrice(n,nn,n);
vecteur v = { p->x,p->y,p->z,1.0F };
produitMatriceVecteur(n,v,v);
ps->x = v[0];
ps->y = v[1];
ps->z = v[2];
}
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();
{ glPushMatrix();
glTranslatef(pi.x,pi.y,pi.z);
glColor4fv(couleurBlanc());
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f);
glVertex2f(0.0F,0.0F);
glTexCoord2f(1.0f,0.0f);
glVertex2f(4.0F,0.0F);
glTexCoord2f(1.0f,1.0f);
glVertex2f(4.0F,4.0F);
glTexCoord2f(0.0f,1.0f);
glVertex2f(0.0F,4.0F);
glEnd();
glDisable(GL_TEXTURE_2D);
glColor4fv(couleurGrisMoyen());
glBegin(GL_LINE_LOOP);
glVertex2f(0.0F,0.0F);
glVertex2f(4.0F,0.0F);
glVertex2f(4.0F,4.0F);
glVertex2f(0.0F,4.0F);
glEnd();
glColor4fv(couleurRouge());
glBegin(GL_LINE_LOOP);
glVertex2f(f.p[0].x*4,f.p[0].y*4);
glVertex2f(f.p[1].x*4,f.p[1].y*4);
glVertex2f(f.p[2].x*4,f.p[2].y*4);
glVertex2f(f.p[3].x*4,f.p[3].y*4);
glEnd();
glLineWidth(1.0);
glTranslatef(pf.x,pf.y,pf.z);
glRotatef(ry,0.0F,1.0F,0.0F);
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(rz,0.0F,0.0F,1.0F);
glScalef(4*scale,4*scale,4*scale);
glColor4fv(couleurGrisMoyen());
glBegin(GL_LINE_LOOP);
glVertex2f(0.0F,0.0F);
glVertex2f(1.0F,0.0F);
glVertex2f(1.0F,1.0F);
glVertex2f(0.0F,1.0F);
glEnd();
glColor4fv(couleurBlanc());
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(f.p[0].x,f.p[0].y);
glVertex2f(f.p[0].x,f.p[0].y);
glTexCoord2f(f.p[1].x,f.p[1].y);
glVertex2f(f.p[1].x,f.p[1].y);
glTexCoord2f(f.p[2].x,f.p[2].y);
glVertex2f(f.p[2].x,f.p[2].y);
glTexCoord2f(f.p[3].x,f.p[3].y);
glVertex2f(f.p[3].x,f.p[3].y);
glEnd();
glDisable(GL_TEXTURE_2D);
glColor4fv(couleurGrisMoyen());
glBegin(GL_LINE_LOOP);
glVertex2f(f.p[0].x,f.p[0].y);
glVertex2f(f.p[1].x,f.p[1].y);
glVertex2f(f.p[2].x,f.p[2].y);
glVertex2f(f.p[3].x,f.p[3].y);
glEnd();
glPopMatrix(); }
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(&f.p[0],&pp1);
positionFinale(&f.p[1],&pp2);
positionFinale(&f.p[2],&pp3);
positionFinale(&f.p[3],&pp4);
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();
glTranslatef(pi.x,pi.y,pi.z);
coord3D pfa;
pfa.x = pf.x*cpt/(float) max;
pfa.y = pf.y*cpt/(float) max;
pfa.z = pf.z*cpt/(float) max;
float rxa = rx*cpt/(float) max;
float rya = ry*cpt/(float) max;
float rza = rz*cpt/(float) max;
float scalea = (scale-1.0F)*cpt/(float) max + 1.0F;
glTranslatef(pfa.x,pfa.y,pfa.z);
glRotatef(rya,0.0F,1.0F,0.0F);
glRotatef(rxa,1.0F,0.0F,0.0F);
glRotatef(rza,0.0F,0.0F,1.0F);
glScalef(4*scalea,4*scalea,4*scalea);
glColor4fv(couleurBlanc());
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glTexCoord2f(f.p[0].x,f.p[0].y);
glVertex2f(f.p[0].x,f.p[0].y);
glTexCoord2f(f.p[1].x,f.p[1].y);
glVertex2f(f.p[1].x,f.p[1].y);
glTexCoord2f(f.p[2].x,f.p[2].y);
glVertex2f(f.p[2].x,f.p[2].y);
glTexCoord2f(f.p[3].x,f.p[3].y);
glVertex2f(f.p[3].x,f.p[3].y);
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 0x0D : cpt = 0;
anim = 1;
glutIdleFunc(idle);
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 rectangle 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);
}
/* ************************************************** */