L'exécutable

Terre-256-256.raw

Mars-256-128.raw

 

Le source : TexturageSphere2.cpp

/* Auteur: Nicolas JANEY              */
/* nicolas.janey@univ-fcomte.fr       */
/* Mars 2003                          */
/* Placage de texture sur une sphere  */

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

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

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif

/* Variables et constantes globales             */
/* pour les angles et les couleurs utilises     */

static float ry = 0.0F;
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static const float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
static const float vert[] = { 0.0F,1.0F,0.0F,1.0F };
static const float bleu[] = { 0.0F,0.0F,1.0F,1.0F };

static GLubyte *image; 
static char *filename = "Terre-256-256.raw";
  
GLubyte *makeImage(void) {
  GLubyte *tab =(GLubyte *) malloc(256*256*3*sizeof(GLubyte));
  FILE *f = fopen(filename,"rb");
  fread(tab,sizeof(GLubyte),256*256*3,f);
  fclose(f);
  return(tab);
}
  
void myinit(void) { 
  glClearColor(0.8F,0.8F,0.8F,0.0);
  glEnable(GL_DEPTH_TEST); 
  glEnable(GL_LIGHTING); 
  glEnable(GL_LIGHT0); 
  glDepthFunc(GL_LESS); 
  image = makeImage(); 
  glPixelStorei(GL_UNPACK_ALIGNMENT,1); 
  glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGB,GL_UNSIGNED_BYTE,image); 
  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); 
  glEnable(GL_TEXTURE_2D);
  glShadeModel(GL_SMOOTH); 
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_NORMALIZE);


void solidSphere(float rayon,int nlat,int nlon) {
  for ( int i = 0 ; i < nlat ; i++ ) {
    float a1 = -M_PI/2.0F + i*M_PI/nlat ;
    float a2 = a1 + M_PI/nlat ;
    float cs1 = cos(a1);
    float cs2 = cos(a2);
    float sn1 = sin(a1);
    float sn2 = sin(a2);
    float s1 = 1.0F -(float) i/nlat;
    float s2 = 1.0F -(float) (i+1)/nlat;
    glBegin(GL_QUAD_STRIP);
    for ( int j = 0 ; j <= nlon ; j++ ) {
      float a = j*2*M_PI/nlon;
      float cs = cos(a);
      float sn = sin(a);
      float x1 = cs1*cs;
      float z1 = cs1*sn;
      float x2 = cs2*cs;
      float z2 = cs2*sn;
      float t =(float) j/nlon;
      glNormal3f(x1,sn1,z1);
      glTexCoord2f(t,s1);
      glVertex3f(rayon*x1,rayon*sn1,rayon*z1);
      glNormal3f(x2,sn2,z2);
      glTexCoord2f(t,s2);
      glVertex3f(rayon*x2,rayon*sn2,rayon*z2); }
    glEnd(); }
}
  
void display(void) { 
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
  glPushMatrix();
  GLfloat dir[4] = { 1.0F,0.0F,1.0F,0.0F };
  glLightfv(GL_LIGHT0,GL_POSITION,dir);
  glRotatef(-23.6F,0.0F,0.0F,1.0F);
  glRotatef(ry,0.0F,1.0F,0.0F);
  solidSphere(3.0,18,36);
  glPopMatrix();
  glFlush(); 
  glutSwapBuffers();
  int error = glGetError();
  if ( error != GL_NO_ERROR )
    printf("Erreur OpenGL\n",error);


/* Fonction executee lorsqu'aucun evenement     */
/* n'est en file d'attente                      */

void idle(void) {
  ry += 0.2117F;
  glutPostRedisplay() ;
}

/* Fonction executee lors d'un changement       */
/* de la taille de la fenetre OpenGL            */

void reshape(int x,int y) {
  glViewport(0,0,x,y); 
  glMatrixMode(GL_PROJECTION) ;
  glLoadIdentity() ;
  gluPerspective(20.0F,(float) x/y,1.0,40.0) ;
  glMatrixMode(GL_MODELVIEW) ;
  glLoadIdentity() ;
  gluLookAt(0.0,0.0,20.0,0.0,0.0,0.0,0.0,1.0,0.0);
}

/* Fonction executee lors de l'appui            */
/* d'une touche alphanumerique du clavier       */

void keyboard(unsigned char key,int x,int y) {
  switch (key) {
    case 0x0D :
      { static int anim = 0;
        anim = !anim;
        glutIdleFunc(( anim ) ? idle : NULL); }
      break;
    case 0x1B :
      free(image);
      exit(0);
      break; }
}

/* Fonction executee lors de l'appui            */
/* d'une touche de curseur ou d'une touche      */
/* page up ou page down                         */

void special(int key,int x,int y) {
  switch(key) {
    case GLUT_KEY_LEFT :
      ry++;
      glutPostRedisplay() ;
      break;
    case GLUT_KEY_RIGHT :
      ry--;
      glutPostRedisplay() ;
      break; }
}
  
int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(260,260); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Une sphere avec texture"); 
  myinit(); 
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutSpecialFunc(special);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

RETOUR