Le source : TD-EllipsoideAnime2.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Un ellipsoide anime */
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleFont.h"
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int image = 0 ;
static int maille = 0 ;
static int anim = 0 ;
static int type = 0 ;
void myinit(void) {
GLfloat light_position0[] = { 0.0F,0.0F,1.0F,0.0F };
GLfloat light_position1[] = { 0.0F,0.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurNoir());
glLightfv(GL_LIGHT0,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT1,GL_DIFFUSE,couleurBlanc());
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurNoir());
glLightfv(GL_LIGHT0,GL_POSITION,light_position0);
glLightfv(GL_LIGHT1,GL_POSITION,light_position1);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_AUTO_NORMAL);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glClearAccum(0.0,0.0,0.0,0.0);
setManipulateurClavierAngle(25.0F,20.0F,0.0F);
}
void axes() {
glPushMatrix() ;
glColor4fv(couleurRouge()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(1.0F,0.0F,0.0F) ;
glEnd() ;
placeFontCursor(1.05F,0.05F,0.05F);
simpleBitmapOutput("x");
glColor4fv(couleurVert()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,1.0F,0.0F) ;
glEnd() ;
placeFontCursor(0.05F,1.05F,0.05F);
simpleBitmapOutput("y");
glColor4fv(couleurBleu()) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.0F,1.0F) ;
glEnd() ;
placeFontCursor(0.05F,0.05F,1.05F);
simpleBitmapOutput("z");
if ( maille ) {
glColor4fv(couleurGrisMoyen()) ;
for ( int i = -100 ; i < 100 ; i++ ) {
glBegin(GL_LINES) ;
glVertex3f((float) i,0.0F,200.0F) ;
glVertex3f((float) i,0.0F,-200.0F) ;
glEnd() ;
glBegin(GL_LINES) ;
glVertex3f(200.0F,0.0F,(float) i) ;
glVertex3f(-200.0F,0.0F,(float) i) ;
glEnd() ; } }
glPopMatrix() ;
}
void displayCotesEllipsoide(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glPushMatrix();
float rx;
float rz;
if ( type == 0 ) {
rx = 3.0f -(float) abs((image%100)-50)/25.0f;
rz = 1.0f; }
else {
int i = image%200;
if ( i < 100 ) {
rx = 3.0f -(float) abs(image-50)/25.0f;
rz = 1.0f; }
else {
rx = 1.0f;
rz = 3.0f -(float) abs(image-150)/25.0f; } }
glScalef(rx,1.0F,rz) ;
glColor4fv(couleurBlanc(0.5F)) ;
glutWireSphere(1.0,12,12) ;
glPopMatrix();
axes() ;
glPopMatrix();
glAccum(GL_ACCUM,0.7F);
}
void displayEllipsoide(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glEnable(GL_LIGHTING);
float rx;
float rz;
if ( type == 0 ) {
rx = 3.0f -(float) abs((image%100)-50)/25.0f;
rz = 1.0f; }
else {
int i = image%200;
if ( i < 100 ) {
rx = 3.0f -(float) abs(image-50)/25.0f;
rz = 1.0f; }
else {
rx = 1.0f;
rz = 3.0f -(float) abs(image-150)/25.0f; } }
glScalef(rx,1.0F,rz) ;
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBlanc(0.5F));
glutSolidSphere(1.0,12,12) ;
glDisable(GL_LIGHTING);
glPopMatrix();
glAccum(GL_ACCUM,0.3F);
}
void display(void) {
glClear(GL_ACCUM_BUFFER_BIT);
glPushMatrix() ;
displayEllipsoide() ;
displayCotesEllipsoide() ;
glAccum(GL_RETURN,1.0);
glPopMatrix() ;
glFlush();
glutSwapBuffers() ;
}
void idle(void) {
image++;
glutPostRedisplay() ;
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D : if ( anim )
glutIdleFunc(NULL) ;
else
glutIdleFunc(idle) ;
anim = 1-anim;
glutPostRedisplay();
break;
case 'a' : type = 1-type;
glutPostRedisplay();
break;
case ' ' : maille = 1-maille;
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_ACCUM);
glutInitWindowSize(250,140);
glutInitWindowPosition(50,50);
glutCreateWindow("Ellipsoide anime");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.8F,1.8F,-1.8F,1.8F,-500.0F,500.0F);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}