Le source : TP-CubeSpheres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* L'animation d'une lumiere */
/* sur un cube composé de speres */
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
#include "ModuleManipulateur.h"
static float angle = 0.0F ;
static int nsp = 2 ;
void myinit(void) {
GLfloat shininess[] = { 50.0 };
glMaterialfv(GL_FRONT,GL_AMBIENT,couleurNoir());
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
void scene(void) {
glPushMatrix() ;
glScalef(15.0f/nsp,15.0f/nsp,15.0f/nsp);
for ( int i = 0 ; i < nsp ; i++ )
for ( int j = 0 ; j < nsp ; j++ )
for ( int k = 0 ; k < nsp ; k++ ) {
float x = -((nsp-1)*2.5) + i*5.0;
float y = -((nsp-1)*2.5) + j*5.0;
float z = -((nsp-1)*2.5) + k*5.0;
float r =(float) i/(nsp-1);
float v =(float) j/(nsp-1);
float b =(float) k/(nsp-1);
float coul[4] = { r,v,b,1.0 };
glMaterialfv(GL_FRONT,GL_SPECULAR,coul);
glMaterialfv(GL_FRONT,GL_DIFFUSE,coul);
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(1.0,36/nsp+4,36/nsp+4);
glPopMatrix(); }
glPopMatrix() ;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix() ;
manipulateurSouris();
manipulateurClavier();
glPushMatrix() ;
glRotatef(angle,1.0F,0.0F,0.0F) ;
GLfloat pos[4] = { 0.5F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glPopMatrix() ;
scene();
glPopMatrix() ;
glFlush();
glutSwapBuffers() ;
}
void idle(void) {
angle += 5.0F ;
glutPostRedisplay() ;
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key) {
case 45 : nsp--;
if ( nsp < 2 )
nsp = 2;
glutPostRedisplay();
break ;
case 43 : nsp++;
glutPostRedisplay();
break ;
case 0x1B : exit(0) ;
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Lumière mouvante");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-50.0,50.0,-50.0,50.0,-150.0,150.0);
glutReshapeFunc(reshapeOrthoBasique);
glutIdleFunc(idle);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
setManipulateurDistance(1.0F);
glutKeyboardFunc(key);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}