typedef enum TexId {
TexGrid = 0, TexChecker, TexPlate, TexOGL, TexPhoenix, TexFexp5, TexFrap, TexGeorge, TexLast
} TexId;
typedef struct globalContext {
/* tableau des Ids des textures */
GLuint texIds[TexLast];
Int32 texI; /* id de la teture courante */
GLuint wallTextures[WallLast];
} globalContext;
globalContext gc;
/* Fonction qui lit une texture */
loadImageTexture(TexId tid, char *fileName, Int32 pixelType)
{
UByte *bytes;
GLenum gluerr;
png_infop img;
/* On NOMME et on CREE un TEXTURE OBJECT */
glGenTextures(1, &gc.texIds[tid]);
glBindTexture(GL_TEXTURE_2D, gc.texIds[tid]);
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Chargement de l'image au format png
if (!(img = imageLoad(fileName, &bytes))) {
fprintf(stderr, "Error reading a texture (%s).\n", fileName);
exit(-1);
}
// Génération de la pyramide de textures pour le mip-mapping
if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, img->width, img->height, pixelType,
GL_UNSIGNED_BYTE, (GLvoid *) (bytes)))) {
fprintf(stderr, "GLULib%s\n", gluErrorString(gluerr));
exit(-1);
}
}
...
/* Type de mapping, comment vont-etre utilisées les couleurs de la texture */
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/* Chargement d'une texture bitmap*/
loadImageTexture(TexPhoenix, "textures/phoenix.png", GL_RGB);
/* Stockage de cette texture dans un tableau de textures */
gc.wallTextures[WallLeft] = gc.texIds[TexPhoenix];
...
/* Activation du texture mapping 2D */
glEnable(GL_TEXTURE_2D);
/* Choix de la texture courante */
glBindTexture(GL_TEXTURE_2D, gc.texIds[TexPhoenix]);
...
/* Dessin d'un quadrilatère avec la texture attachée dessus */
eval1 = c->eval;
eval2 = &(c->eval[3 * c->vRes]);
norm1 = c->norm;
norm2 = &(c->norm[3 * c->vRes]);
for (i = 0; i < c->uRes - 1; i++) {
glBegin(GL_QUAD_STRIP);
for (j = 0; j < c->vRes; j++) {
glNormal3fv(norm1);
s = (*eval1) * 4;
t = (*(eval1 + 1)) * 4;
glTexCoord2f(s, t);
glVertex3fv(eval1);
eval1 += 3;
norm1 += 3;
glNormal3fv(norm2);
s = (*eval2) * 4;
t = *(eval2 + 1) * 4;
glTexCoord2f(s, t);
glVertex3fv(eval2);
eval2 += 3;
norm2 += 3;
}
glEnd();
}