Le source : ExemplesTextures.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Texturage OpenGL d'un teapot */
/* avec du marbre */
#include <math.h>
#include <stdio.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 iw 128
#define ih 128
#define tf "Marbre.raw"
static GLubyte im[3*iw*ih];
static int texture = 1;
void lectureTexture(char *fichier,int dx,int dy) {
FILE *f = fopen(fichier,"rb") ;
if ( f ) {
for ( int i = 0 ; i < dx ; i++ )
for ( int j = 0 ; j < dy ; j++ )
fread(&im[(j*dy+i)*3],1,3,f) ;
fclose(f) ; }
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|
GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
if ( texture )
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
glutSolidTeapot(5) ;
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void initlights(void) {
GLfloat pos[] = { 0.0F,0.0F,-1.0F,0.0F };
GLfloat shininess[] = { 50.0F };
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurGrisFonce());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc());
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
}
void myinit(void) {
glClearColor(0.5F,0.5F,0.5F,1.0F);
lectureTexture(tf,iw,ih) ;
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glEnable(GL_MAP2_TEXTURE_COORD_2);
glTexImage2D(GL_TEXTURE_2D,
0,3,iw,ih,0,
GL_RGB,
GL_UNSIGNED_BYTE,
im);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
initlights();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D : texture = 1 - texture;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Teapot avec texture");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-7.0,7.0,-7.0,7.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}