Lumières colorées et lumières blanches sur une sphère
Sphères avec beaucoup et peu de facettes
Le source : EnsembleFacettesSphere.cpp
/* Sphere modelisee par facettes */
/* */
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Septembre 2009 */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
/* Variables et constantes globales */
/* pour les angles et les couleurs utilises */
static float rayon = 2.0F;
static int n = 36;
static int m = 36;
static float rx = 0.0F;
static float ry = 0.0F;
static float rz = 0.0F;
static const float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
static const float gris[] = { 0.7F,0.7F,0.7F,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 };
/* Fonction d'initialisation des parametres */
/* OpenGL ne changeant pas au cours de la vie */
/* du programme */
void init(void) {
const GLfloat shininess[] = { 50.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,blanc);
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune);
glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
}
/* Scene dessinee */
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif
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);
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;
glNormal3f(x1,sn1,z1);
glVertex3f(rayon*x1,rayon*sn1,rayon*z1);
glNormal3f(x2,sn2,z2);
glVertex3f(rayon*x2,rayon*sn2,rayon*z2); }
glEnd(); }
}
void scene() {
glPushMatrix();
solidSphere(rayon,n,m);
glPopMatrix();
}
/* Fonction executee lors d'un rafraichissement */
/* de la fenetre de dessin */
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);
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);
scene();
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) {
rx += 0.4256F + (rand()%1000)/1000.0F ;
ry += 0.6117F ;
rz += 0.4174F ;
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(25.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 'l' :
{ static int lumiere = 0;
lumiere = !lumiere;
if ( lumiere ) {
glLightfv(GL_LIGHT0,GL_DIFFUSE,gris);
glLightfv(GL_LIGHT1,GL_DIFFUSE,gris);
glLightfv(GL_LIGHT2,GL_DIFFUSE,gris);
glEnable(GL_CULL_FACE); }
else {
glLightfv(GL_LIGHT0,GL_DIFFUSE,rouge);
glLightfv(GL_LIGHT1,GL_DIFFUSE,jaune);
glLightfv(GL_LIGHT2,GL_DIFFUSE,bleu);
glDisable(GL_CULL_FACE); } }
glutPostRedisplay();
break;
case 'c' :
{ static int culling = 0;
culling = !culling;
if ( culling )
glEnable(GL_CULL_FACE);
else
glDisable(GL_CULL_FACE); }
glutPostRedisplay();
break;
case 'f' :
{ static int face = 1;
face = !face;
glPolygonMode(GL_FRONT_AND_BACK,( face ) ? GL_FILL : GL_LINE); }
glutPostRedisplay();
break;
case 'n' :
n--;
if ( n < 2 )
n = 2;
glutPostRedisplay();
break;
case 'N' :
n++;
glutPostRedisplay();
break;
case 'm' :
m--;
if ( m < 3 )
m = 3;
glutPostRedisplay();
break;
case 'M' :
m++;
glutPostRedisplay();
break;
case '+' :
rayon += 0.1F;
glutPostRedisplay();
break;
case '-' :
rayon -= 0.1F;
if ( rayon < 0.1F )
rayon = 0.1F;
glutPostRedisplay();
break;
case 0x0D :
{ static int anim = 1;
anim = !anim;
glutIdleFunc(( anim ) ? idle : NULL); }
break;
case 0x1B :
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_UP :
rx++;
glutPostRedisplay() ;
break;
case GLUT_KEY_DOWN :
rx--;
glutPostRedisplay() ;
break;
case GLUT_KEY_LEFT :
ry++;
glutPostRedisplay() ;
break;
case GLUT_KEY_RIGHT :
ry--;
glutPostRedisplay() ;
break;
case GLUT_KEY_PAGE_UP :
rz++;
glutPostRedisplay() ;
break;
case GLUT_KEY_PAGE_DOWN :
rz--;
glutPostRedisplay() ;
break; }
}
/* Fonction principale */
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Une sphère modélisée par facettes");
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}