L'exécutable

Fichier source : TexturesBitmap.cpp

/* Fonction d'initialisation des parametres     */
/* OpenGL ne changeant pas au cours de la vie   */
/* du programme                                 */
/* Contient en particulier l'initialisation     */
/* de trois textures 2D                         */

static void chargeTexture(unsigned int textureID,char *filename) {
  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); } }
  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_LINEAR); 
  glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
}

static void init(void) {
  const GLfloat mat_shininess[] = { 50.0 };
  glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
  glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
  glLightfv(GL_LIGHT0,GL_DIFFUSE,gris);
  glLightfv(GL_LIGHT1,GL_DIFFUSE,gris);
  glLightfv(GL_LIGHT2,GL_DIFFUSE,gris);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHT1);
  glEnable(GL_LIGHT2);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_NORMALIZE);
  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
  glGenTextures(3,texID);
  chargeTexture(texID[0],"Test1.png");
  chargeTexture(texID[1],"Test2.png");
  chargeTexture(texID[2],"Test3.png");
  glEnable(GL_TEXTURE_2D);
}

/* Scene dessinee :                             */
/* Un carre texture                             */

static void facette(unsigned int textureID) {
  glBindTexture(GL_TEXTURE_2D,textureID);
  glPushMatrix();
  glBegin(GL_QUADS);
  glNormal3f(0.0F,0.0F,1.0F);
  glTexCoord2f(0.0F,0.0F);
  glVertex2f(-3.0,-3.0);
  glTexCoord2f(1.0F,0.0F);
  glVertex2f( 3.0,-3.0);
  glTexCoord2f(1.0F,1.0F);
  glVertex2f( 3.0, 3.0);
  glTexCoord2f(0.0F,1.0F);
  glVertex2f(-3.0, 3.0);
  glEnd();
  glPopMatrix();
}

/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin                      */

static void display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  const GLfloat light0_position[] = { 1.0,1.0,1.0,0.0 };
  const GLfloat light1_position[] = { -1.0,1.0,1.0,0.0 };
  const GLfloat light2_position[] = { 1.0,-1.0,1.0,0.0 };
  glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
  glLightfv(GL_LIGHT1,GL_POSITION,light1_position);
  glLightfv(GL_LIGHT2,GL_POSITION,light2_position);
  glPolygonMode(GL_FRONT_AND_BACK,(aff) ? GL_FILL : GL_LINE);
  glPushMatrix();
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  facette(texID[0]);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(9.0F,0.0F,0.0F);
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  facette(texID[1]);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-9.0F,0.0F,0.0F);
  glRotatef(rx,1.0F,0.0F,0.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  glRotatef(rz,0.0F,0.0F,1.0F);
  facette(texID[2]);
  glPopMatrix();
  glFlush();
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL: %d\n",error);
}

/* Fonction executee lors de la frappe          */
/* d'une touche du clavier                      */

static void keyboard(unsigned char key,int x,int y) {
  switch ( key ) {
    case 0x20 :
      aff = (aff+1)%2;
      glutPostRedisplay(); 
      break;
    case 0x0D :
      objet = (objet+1)%2;
      glutPostRedisplay(); 
      break;
    case 0x1B :
      glDeleteTextures(3,texID);
      exit(0); 
      break; }
}

RETOUR