Le source : TP-DeTransparent.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Mars 2004 */
/* Dessin d'un de a jouer */
/* avec texture transparente */
#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 "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#define LI 16
#define LH 16
static float anglex = 0.0F;
static float angley = 0.0F;
static float anglez = 0.0F;
static GLubyte un[LI][LH][4];
static GLubyte deux[LI][LH][4];
static GLubyte trois[LI][LH][4];
static GLubyte quatre[LI][LH][4];
static GLubyte cinq[LI][LH][4];
static GLubyte six[LI][LH][4];
static int anim = 1;
void setNoir(GLubyte *p) {
p[0] = p[1] = p[2] = 0x00;
p[3] = 0xFF;
}
void pointUn(GLubyte img[LI][LH][4]) {
setNoir(img[7][7]);
setNoir(img[7][8]);
setNoir(img[8][7]);
setNoir(img[8][8]);
}
void pointDeux(GLubyte img[LI][LH][4]) {
setNoir(img[3][3]);
setNoir(img[3][4]);
setNoir(img[4][3]);
setNoir(img[4][4]);
}
void pointTrois(GLubyte img[LI][LH][4]) {
setNoir(img[11][11]);
setNoir(img[11][12]);
setNoir(img[12][11]);
setNoir(img[12][12]);
}
void pointQuatre(GLubyte img[LI][LH][4]) {
setNoir(img[3][11]);
setNoir(img[3][12]);
setNoir(img[4][11]);
setNoir(img[4][12]);
}
void pointCinq(GLubyte img[LI][LH][4]) {
setNoir(img[11][3]);
setNoir(img[11][4]);
setNoir(img[12][3]);
setNoir(img[12][4]);
}
void pointSix(GLubyte img[LI][LH][4]) {
setNoir(img[11][7]);
setNoir(img[11][8]);
setNoir(img[12][7]);
setNoir(img[12][8]);
}
void pointSept(GLubyte img[LI][LH][4]) {
setNoir(img[3][7]);
setNoir(img[3][8]);
setNoir(img[4][7]);
setNoir(img[4][8]);
}
void makeImages(void) {
int i,j;
for( i = 0 ; i < LI ; i++ ) {
for( j = 0 ; j < LH ; j++ ) {
un[i][j][0] = un[i][j][1] = un[i][j][2] =(GLubyte) 255;
un[i][j][3] =(GLubyte) 30;
deux[i][j][0] = deux[i][j][1] = deux[i][j][2] =(GLubyte) 255;
deux[i][j][3] =(GLubyte) 30;
trois[i][j][0] = trois[i][j][1] = trois[i][j][2] =(GLubyte) 255;
trois[i][j][3] =(GLubyte) 30;
quatre[i][j][0] = quatre[i][j][1] = quatre[i][j][2] =(GLubyte) 255;
quatre[i][j][3] =(GLubyte) 30;
cinq[i][j][0] = cinq[i][j][1] = cinq[i][j][2] =(GLubyte) 255;
cinq[i][j][3] =(GLubyte) 30;
six[i][j][0] = six[i][j][1] = six[i][j][2] =(GLubyte) 255;
six[i][j][3] =(GLubyte) 30; } }
pointUn(un);
pointUn(trois);
pointUn(cinq);
pointDeux(deux);
pointDeux(trois);
pointDeux(quatre);
pointDeux(cinq);
pointDeux(six);
pointTrois(deux);
pointTrois(trois);
pointTrois(quatre);
pointTrois(cinq);
pointTrois(six);
pointQuatre(quatre);
pointQuatre(cinq);
pointQuatre(six);
pointCinq(quatre);
pointCinq(cinq);
pointCinq(six);
pointSix(six);
pointSept(six);
}
void de(float c){
c /= 2.0F;
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&un[0][0][0]);
glPushMatrix();
glBegin(GL_QUADS);
glNormal3f(0.0F,0.0F,-1.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(c,c,-c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(c,-c,-c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(-c,-c,-c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(-c,c,-c);
glEnd();
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&six[0][0][0]);
glBegin(GL_QUADS);
glNormal3f(0.0F,0.0F,1.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(c,c,c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(-c,c,c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(-c,-c,c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(c,-c,c);
glEnd();
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&deux[0][0][0]);
glBegin(GL_QUADS);
glNormal3f(-1.0F,0.0F,0.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(-c,c,-c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(-c,-c,-c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(-c,-c,c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(-c,c,c);
glEnd();
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&cinq[0][0][0]);
glBegin(GL_QUADS);
glNormal3f(1.0F,0.0F,0.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(c,c,c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(c,-c,c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(c,-c,-c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(c,c,-c);
glEnd();
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&trois[0][0][0]);
glBegin(GL_QUADS);
glNormal3f(0.0F,-1.0F,0.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(-c,-c,c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(-c,-c,-c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(c,-c,-c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(c,-c,c);
glEnd();
glTexImage2D(GL_TEXTURE_2D,0,4,LI,LH,0,GL_RGBA,GL_UNSIGNED_BYTE,&quatre[0][0][0]);
glBegin(GL_QUADS);
glNormal3f(0.0F,1.0F,0.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(c,c,c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(c,c,-c);
glTexCoord2f(1.0F,1.0F);
glVertex3f(-c,c,-c);
glTexCoord2f(1.0F,0.0F);
glVertex3f(-c,c,c);
glEnd();
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glRotatef(anglex,1.0F,0.0F,0.0F);
glRotatef(angley,0.0F,1.0F,0.0F);
glRotatef(anglez,0.0F,0.0F,1.0F);
de(3.6F) ;
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
//glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
float dir0[4] = { 1.0F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,dir0) ;
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurVert()) ;
float dir1[4] = { -1.0F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT1,GL_POSITION,dir1) ;
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBleu()) ;
float dir2[4] = { 0.0F,-1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT2,GL_POSITION,dir2) ;
glLightfv(GL_LIGHT2,GL_DIFFUSE,couleurRouge()) ;
makeImages();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void idle(void) {
anglex += 0.31F;
angley += 0.37F;
anglez += 0.41F;
glutPostRedisplay();
}
void key(unsigned char key,int x,int y) {
static int aff = 1;
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case ' ' : anim = !anim;
glutIdleFunc((anim) ? idle : NULL);
break;
case 0x0D : aff = !aff;
glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
glutPostRedisplay(); }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("De transparent");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-3.5F,3.5F,-3.5F,3.5F,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}