Fichier source : PlacageMonoTexture.cppp
#include "PNG/ChargePngFile.h"
/* Fonction de chargement d'une texture */
/* a partir d'un fichier image au format png */
/* filename : le nom du fichier */
/* @retour : le handle de la texture */
unsigned int chargementTexturePNG(char *filename) {
unsigned int textureID = 0;
glGenTextures(1,&textureID);
int rx;
int ry;
unsigned char *img = chargeImagePng(filename,&rx,&ry);
if ( img && textureID ) {
glBindTexture(GL_TEXTURE_2D,textureID);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,rx,ry,0,GL_RGB,GL_UNSIGNED_BYTE,img);
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);
free(img); }
else {
if ( img ) {
free(img); }
if ( textureID ) {
glDeleteTextures(1,&textureID);
textureID = 0; } }
return textureID;
}
/* Modelisation geometrique d'un cube */
/* avec placage d'une texture sur chacune */
/* de ses faces */
/* ct : la taille du cube */
/* textureID : l'id de la texture à utiliser */
static void mySolidCube(double ct,unsigned int textureID) {
float c =(float) ct/2.0F;
glBindTexture(GL_TEXTURE_2D,textureID);
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();
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();
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();
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();
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();
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();
}
/* 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);
textureID = chargementTexturePNG("Test.png");
}