
Le source : TD-SpheresAnimees.cpp
#include "windows.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <stdio.h>
#include <math.h>
#include "ModuleFont.h"
static GLfloat view_rotx=25.0F ;
static GLfloat view_roty=20.0F ;
static GLfloat view_rotz=0.0F ;
static float a = 0.0f ;
static int type = 0 ;
static int anim = 1 ;
static int maille = 0 ;
static float noir[] = { 0.0F,0.0F,0.0F,0.5F };
static float rouge[] = { 1.0F,0.0F,0.0F,0.5F };
static float vert[] = { 0.0F,1.0F,0.0F,0.5F };
static float bleu[] = { 0.0F,0.0F,1.0F,0.5F };
static float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
static float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
static float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
static float blanc[] = { 1.0F,1.0F,1.0F,0.5F };
void myinit(void) {
GLfloat light_ambient[] = { 0.0F,0.0F,0.0F,1.0F };
GLfloat light_diffuse[] = { 1.0F,1.0F,1.0F,1.0F };
GLfloat light_specular[] = { 0.0F,0.0F,0.0F,1.0F };
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,light_ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,light_specular);
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);
makeRasterFont();
}
void axes() {
glPushMatrix() ;
glColor4fv(rouge) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(1.0F,0.0F,0.0F) ;
glEnd() ;
glRasterPos3f(1.05F,0.05F,0.05F);
printString("x");
glColor4fv(vert) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,1.0F,0.0F) ;
glEnd() ;
glRasterPos3f(0.05F,1.05F,0.05F);
printString("y");
glColor4fv(bleu) ;
glBegin(GL_LINES) ;
glVertex3f(0.0F,0.0F,0.0F) ;
glVertex3f(0.0F,0.0F,1.0F) ;
glEnd() ;
glRasterPos3f(0.05F,0.05F,1.05F);
printString("z");
if ( maille ) {
float gris[] = { 0.5F,0.5F,0.5F,1.0F };
glColor4fv(gris) ;
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 displayCotesSpheres(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx,1.0,0.0,0.0);
glRotatef(view_roty,0.0,1.0,0.0);
glRotatef(view_rotz,0.0,0.0,1.0);
glPushMatrix();
glRotatef(a,0.0f,1.0f,0.0f);
glTranslatef(4.0F,0.0F,0.0F) ;
glColor3fv(rouge) ;
auxWireSphere(1.0) ;
if ( type == 1 ) {
glRotatef(a*5,0.0f,1.0f,0.0f);
glTranslatef(2.0F,0.0F,0.0F) ;
glColor3fv(vert) ;
auxWireSphere(0.5) ; }
glPopMatrix();
axes() ;
glPopMatrix();
glAccum(GL_ACCUM,0.7F);
}
void displaySpheres(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(view_rotx,1.0,0.0,0.0);
glRotatef(view_roty,0.0,1.0,0.0);
glRotatef(view_rotz,0.0,0.0,1.0);
glEnable(GL_LIGHTING);
glRotatef(a,0.0f,1.0f,0.0f);
glTranslatef(4.0F,0.0F,0.0F) ;
glMaterialfv(GL_FRONT,GL_DIFFUSE,rouge);
auxSolidSphere(1.0) ;
if ( type == 1 ) {
glRotatef(a*5,0.0f,1.0f,0.0f);
glTranslatef(2.0F,0.0F,0.0F) ;
glMaterialfv(GL_FRONT,GL_DIFFUSE,vert);
auxWireSphere(0.5) ; }
glDisable(GL_LIGHTING);
glPopMatrix();
glAccum(GL_ACCUM,0.3F);
}
void CALLBACK display(void) {
glClear(GL_ACCUM_BUFFER_BIT);
glPushMatrix() ;
displaySpheres() ;
displayCotesSpheres() ;
glAccum(GL_RETURN,1.0);
glPopMatrix() ;
glFlush();
auxSwapBuffers() ;
}
void CALLBACK myReshape(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-7.0,7.0,-h*7.0/w,h*7.0/w,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void CALLBACK keyx(void) {
view_rotx += 4 ;
}
void CALLBACK keyX(void) {
view_rotx -= 4 ;
}
void CALLBACK keyy(void) {
view_roty += 4 ;
}
void CALLBACK keyY(void) {
view_roty -= 4 ;
}
void CALLBACK keyz(void) {
view_rotz += 4 ;
}
void CALLBACK keyZ(void) {
view_rotz -= 4 ;
}
void CALLBACK keyUp(void) {
type = (type+1)%2 ;
}
void CALLBACK keySpace(void) {
maille = 1 - maille ;
}
void CALLBACK idle(void) {
a += 1.0f;
display();
}
void CALLBACK keyReturn(void) {
anim = !anim ;
if ( anim )
auxIdleFunc(idle) ;
else
auxIdleFunc(NULL) ;
}
void main(void) {
auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH|AUX_ACCUM);
auxInitPosition(0,0,250,140);
auxInitWindow("Spheres animees");
myinit();
auxIdleFunc(idle) ;
auxKeyFunc(AUX_x,keyx) ;
auxKeyFunc(AUX_X,keyX) ;
auxKeyFunc(AUX_y,keyy) ;
auxKeyFunc(AUX_Y,keyY) ;
auxKeyFunc(AUX_z,keyz) ;
auxKeyFunc(AUX_Z,keyZ) ;
auxKeyFunc(AUX_UP,keyUp) ;
auxKeyFunc(AUX_SPACE,keySpace) ;
auxKeyFunc(AUX_RETURN,keyReturn) ;
auxReshapeFunc(myReshape);
auxMainLoop(display);
}
RETOUR