Fichier image de la terre au format png: Terre-02048.png
avec glutSolidSphere
sans texturage
en affichant les facettes avant en fil de fer et les facettes arrières en solide
Fichier librairie statique: Libpng.lib
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2007 */
/* Une sphere texturee avec une image */
/* de la terre */
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ChargePngFile.h"
#include "ModuleSpheres.h"
/* Variables globales */
static int disc = 72;
static int texturage = 1;
static int sphere = 0;
static int mode = 1;
static float angle = 0.0F;
/* Variables globales de gestion */
/* de l'interactivite clavier et souris */
static int clic = 0;
static int mx;
static int my;
static float rx = 0.0F;
static float ry = 0.0F;
void init(void) {
const GLfloat mat_shininess[] = { 100.0 };
const GLfloat blanc[] = { 1.0,1.0,1.0,1.0 };
int resx;
int resy;
GLubyte *image;
glLightfv(GL_LIGHT0,GL_DIFFUSE,blanc);
glMaterialfv(GL_FRONT,GL_DIFFUSE,blanc);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
image = chargeImagePng("Terre-02048.png",&resx,&resy);
if ( image ) {
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,3,
resx,resy,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);
free(image); }
}
/* Scene dessinee */
void scene(void) {
glPushMatrix();
const GLfloat light0_position[] = { 1.0,0.0,0.0,0.0 };
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glRotatef(-23.0F,0.0F,0.0F,1.0F);
glRotatef(angle,0.0F,1.0F,0.0F);
switch ( sphere ) {
case 0 : glutSolidSphere(4.0,disc,disc);
break;
case 1 : mySolidSphere(4.0,disc,disc);
break; }
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPolygonMode(GL_FRONT,(mode) ? GL_FILL : GL_LINE);
if ( texturage )
glEnable(GL_TEXTURE_2D);
else
glDisable(GL_TEXTURE_2D);
glRotatef(rx,1.0F,0.0F,0.0F);
glRotatef(ry,0.0F,1.0F,0.0F);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
int error = glGetError();
if ( error != GL_NO_ERROR )
printf("Attention, erreur OpenGL %d\n",error);
}
/* Fonction executee lors d'un clic de souris */
/* dans la fenetre */
void mouse(int bouton,int etat,int x,int y) {
if ( bouton == GLUT_LEFT_BUTTON ) {
if ( etat == GLUT_DOWN ) {
clic = 1;
mx = x;
my = y; }
if ( etat == GLUT_UP ) {
clic = 0; } }
}
/* Fonction executee lors d'un deplacement */
/* de la souris sur la fenetre */
/* avec un bouton appuye */
void motion(int x,int y) {
if ( clic ) {
ry += (x-mx);
rx += (y-my);
mx = x;
my = y;
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();
glOrtho(-5.0,5.0,(double) -5.0*y/x,(double) 5.0*y/x,-5.0,5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idle(void) {
angle += 0.5F;
glutPostRedisplay();
}
/* Fonction executee lors de la frappe */
/* d'une touche alphanumerique du clavier */
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 43 : disc++;
glutPostRedisplay();
break;
case 45 : disc--;
if ( disc < 1 )
disc = 1;
glutPostRedisplay();
break;
case 'f' : mode = !mode;
glutPostRedisplay();
break;
case 0x20 : sphere = !sphere;
glutPostRedisplay();
break;
case 0x0D : texturage = !texturage;
glutPostRedisplay();
break;
case 'a' : { static int anim = 1;
anim = !anim;
glutIdleFunc((anim) ? idle : NULL); }
break;
case 0x1B : exit(0);
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(260,260);
glutInitWindowPosition(50,50);
glutCreateWindow("La terre");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}
Fichier source: ModuleSpheres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2007 */
/* Module de dessin des spheres */
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleSpheres.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
void mySolidSphere(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 t1 = 1.0F-(float) i/nlat;
float t2 = 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 x1 = cs1*cos(a);
float z1 = cs1*sin(a);
float x2 = cs2*cos(a);
float z2 = cs2*sin(a);
float s = 1.0F-(float) j/nlon;
glTexCoord2f(s,t1);
glNormal3f(x1,sn1,z1);
glVertex3f(rayon*x1,rayon*sn1,rayon*z1);
glTexCoord2f(s,t2);
glNormal3f(x2,sn2,z2);
glVertex3f(rayon*x2,rayon*sn2,rayon*z2); }
glEnd(); }
}
Fichier source: ModuleSpheres.h
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2007 */
/* Module de dessin des spheres */
#ifndef MODULESPHERE
#define MODULESPHERE
void mySolidSphere(float rayon,int nlat,int nlon);
#endif
Fichier source: ChargePngFile.h
Fichier librairie statique: LibChargePngFile.lib