Le source : TP-CameraDansTores.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2003 */
/* Deplacement a travers */
/* un alignement de tores */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
static float z = 700.0F ;
static float dz = 1.0F ;
static int mode = 1;
void myinit(void) {
glClearColor(0.8F,0.8F,0.8F,1.0F) ;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_AUTO_NORMAL);
}
void scene(void) {
glPushMatrix();
for ( int i = 0 ; i < 10 ; i++ ) {
glPushMatrix();
glTranslatef(0.0F,0.0F,50.0F*i) ;
if ( mode )
glutSolidTorus(25.0,100.0,18,36);
else
glutWireTorus(25.0,100.0,18,36);
glPopMatrix(); }
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(0.0,0.0,z,0.0,0.0,z-1.0F,0.0,1.0,0.0);
scene();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void idle(void) {
z -= dz ;
if ( z <= 0.0F )
z = 700.0F ;
glutPostRedisplay() ;
}
void reshape(int w,int h) {
float xmin,xmax,ymin,ymax;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( w <= h) {
xmin = -2.5F;
xmax = 2.5F;
ymin = -2.5F*(double) h/w;
ymax = 2.5F*(double) h/w; }
else {
xmin = -2.5F*(double) w/h;
xmax = 2.5F*(double) w/h;
ymax = 2.5F;
ymin = -2.5F; }
glFrustum(xmin,xmax,ymin,ymax,3.0F,1000.0F);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void key(unsigned char k,int x,int y) {
static int anim = 1;
switch (k) {
case 0x0D : mode = !mode ;
glutPostRedisplay();
break;
case 43 : dz *= 1.1F;
glutPostRedisplay();
break;
case 45 : dz /= 1.1F;
glutPostRedisplay();
break;
case ' ' : anim = !anim;
glutIdleFunc((anim) ? idle : NULL);
break;
case 0x1B : exit(0); }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(450,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Deplacement camera");
myinit();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}