L'exécutable

Le source : ExemplesTextures2.cpp

#include <windows.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <GL/glut.h>

#include <math.h>
#include <stdio.h>

#define iw 128 
#define ih 128
#define tf "Marbre.raw"

static GLubyte im[3*iw*ih]; 
static GLfloat view_rotx=50.0F ;
static GLfloat view_roty=20.0F ;
static GLfloat view_rotz=10.0F ;
static float f = 1.0F ;

GLfloat texpts[2][2][2] = { 
  {{0.0,0.0},{0.0,1.0}}, 
  {{1.0,0.0},{1.0,1.0}}}; 
  
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 CALLBACK display(void) { 
  glClear(GL_COLOR_BUFFER_BIT|
          GL_DEPTH_BUFFER_BIT); 
  glPushMatrix();
  glEnable(GL_DEPTH_TEST);
  glRotatef(view_rotx,1.0,0.0,0.0);
  glRotatef(view_roty,0.0,1.0,0.0);
  glRotatef(view_rotz,0.0,0.0,1.0);
  glBegin(GL_QUADS) ;
  glTexCoord2f(0.0F,0.0F); 
  glVertex3f(-5.0F,5.0F,0.0F) ;
  glTexCoord2f(0.0F,f); 
  glVertex3f(-5.0F,-5.0F,0.0F) ;
  glTexCoord2f(f,f); 
  glVertex3f(5.0F,-5.0F,0.0F) ;
  glTexCoord2f(f,0.0F); 
  glVertex3f(5.0F,5.0F,0.0F) ;
  glEnd() ;
  glPopMatrix();
  glFlush();
  auxSwapBuffers();
} 

void CALLBACK myReshape(int w,int h) { 
  glViewport(0,0,w,h); 
  glMatrixMode(GL_PROJECTION); 
  glLoadIdentity(); 
  if( w <= h ) {
    float y = 7.F*(float)h/(float)w ;
    glOrtho(-7.,7.,-y,y,-17.,17.); }
    else  {
    float x = 7.F*(float)w/(float)h ;
    glOrtho(-x,x,-7.,7.,-17.,17.); }
  glMatrixMode(GL_MODELVIEW); 
  glLoadIdentity(); 
} 
  
void initlights(void) { 
  GLfloat ambient[] = { 0.2F,0.2F,0.2F,1.0F }; 
  GLfloat diffuse[] = { 3.2F,3.2F,3.2F,1.0F }; 
  GLfloat pos[] = { 0.0F,0.0F,-1.0F,0.0F }; 
  GLfloat spec[] = { 1.0F,1.0F,1.0F,1.0F }; 
  GLfloat shininess[] = { 50.0F }; 
  glEnable(GL_LIGHT0); 
  glEnable(GL_LIGHTING); 
  glLightfv(GL_LIGHT0,GL_AMBIENT,ambient); 
  glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse); 
  glMaterialfv(GL_FRONT,GL_SPECULAR,spec); 
  glMaterialfv(GL_FRONT,GL_SHININESS,shininess); 
} 
  
void myinit(void) { 
  glClearColor(0.2F,0.2F,0.2F,1.0F); 
  lectureTexture(tf,iw,ih) ;
  glMap2f(GL_MAP2_TEXTURE_COORD_2,
          0.0F,1.0F,2,2,
          0.0F,1.0F,4,2,
          &texpts[0][0][0]); 
  glTexParameterf(GL_TEXTURE_2D,
                  GL_TEXTURE_WRAP_S,
                  GL_REPEAT); 
  glTexParameterf(GL_TEXTURE_2D,
                  GL_TEXTURE_WRAP_T,
                  GL_CLAMP);
  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_TEXTURE_2D); 
  glEnable(GL_AUTO_NORMAL); 
  glEnable(GL_NORMALIZE); 
  glShadeModel(GL_SMOOTH); 
  glDepthFunc(GL_LESS);
  initlights(); 
} 
  
void CALLBACK keyx(void) {
  view_rotx += 2 ;
}

void CALLBACK keyX(void) {
  view_rotx -= 2 ;
}

void CALLBACK keyy(void) {
  view_roty += 2 ;
}

void CALLBACK keyY(void) {
  view_roty -= 2 ;
}

void CALLBACK keyz(void) {
  view_rotz += 2 ;
}

void CALLBACK keyZ(void) {
  view_rotz -= 2 ;
}

void CALLBACK keyRight(void) {
  f *= 1.01 ;
}

void CALLBACK keyLeft(void) {
  f /= 1.01 ;
}

void main(void) { 
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH); 
  auxInitPosition(0,0,300,300); 
  auxInitWindow("Facette avec texture"); 
  myinit(); 
  auxKeyFunc(AUX_x,keyx) ;
  auxKeyFunc(AUX_X,keyX) ;
  auxKeyFunc(AUX_y,keyy) ;
  auxKeyFunc(AUX_Y,keyY) ;
  auxKeyFunc(AUX_z,keyz) ;
  auxKeyFunc(AUX_Z,keyZ) ;
  auxKeyFunc(AUX_RIGHT,keyRight) ;
  auxKeyFunc(AUX_LEFT,keyLeft) ;
  auxReshapeFunc(myReshape); 
  auxMainLoop(display); 
} 
 RETOUR