Fichier source : PlacageMultiTexturesOptimise.cpp
#include "PNG/ChargePngFile.h"
static unsigned int textureID[6] = { 0,0,0,0,0,0 };
/* Fonction de chargement d'une texture */
/* a partir d'un fichier image au format png */
/* filename : le nom du fichier */
/* textureID : le handle de la texture */
static void chargementTexture(char *filename,unsigned int textureID) {
glBindTexture(GL_TEXTURE_2D,textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
{ int rx;
int ry;
unsigned char *img = chargeImagePng(filename,&rx,&ry);
if ( img ) {
glTexImage2D(GL_TEXTURE_2D,0,3,rx,ry,0,GL_RGB,GL_UNSIGNED_BYTE,img);
free(img);
printf("Texture chargée %d : %s\n",textureID,filename); }
else {
printf("Texture non chargée\n"); } }
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
}
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
static void init(void) {
glClearColor(0.25F,0.25F,0.25F,1.0F);
glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
glGenTextures(6,textureID);
chargementTexture("Emoji1.png",textureID[0]);
chargementTexture("Emoji2.png",textureID[1]);
chargementTexture("Emoji3.png",textureID[2]);
chargementTexture("Emoji4.png",textureID[3]);
chargementTexture("Emoji5.png",textureID[4]);
chargementTexture("Emoji6.png",textureID[5]);
}
/* Modelisation geometrique d'un cube */
/* avec placage de textures sur chacune */
/* de ses faces */
/* ct : la taille du cube */
/* texID : le tableau des 6 handles de texture */
/* a utiliser */
static void mySolidCube(double ct,unsigned int *texID) {
float c =(float) ct/2.0F;
/* Activation premiere texture */
glBindTexture(GL_TEXTURE_2D,texID[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();
/* Activation deuxieme texture */
glBindTexture(GL_TEXTURE_2D,texID[1]);
glBegin(GL_QUADS);
glNormal3f(0.0F,0.0F,1.0F);
glTexCoord2f(0.0F,0.0F);
glVertex3f(-c,-c, c);
glTexCoord2f(1.0F,0.0F);
glVertex3f( c,-c, c);
glTexCoord2f(1.0F,1.0F);
glVertex3f( c, c, c);
glTexCoord2f(0.0F,1.0F);
glVertex3f(-c, c, c);
glEnd();
/* Activation troisieme texture */
glBindTexture(GL_TEXTURE_2D,texID[2]);
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();
/* Activation quatrieme texture */
glBindTexture(GL_TEXTURE_2D,texID[3]);
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();
/* Activation cinquieme texture */
glBindTexture(GL_TEXTURE_2D,texID[4]);
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();
/* Activation sixieme texture */
glBindTexture(GL_TEXTURE_2D,texID[5]);
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();
}
/* Scene dessinee */
static void scene(void) {
glPushMatrix();
mySolidCube(5.0,textureID);
glPopMatrix();
}
/* Desallocation des textures */
static void clean(void) {
for ( int i = 0 ; i < 6 ; i++ )
if ( textureID[i] != 0 )
glDeleteTextures(1,&textureID[i]);
}