Le source : TP-CameraDansToresCarres.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2003 */
/* Deplacement dans un tore de carres */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
static float angle = 0.0F;
static int ns = 36;
static int traj = 0;
static int f1;
static int f2;
static int droite = 1;
static int tourneADroite = 1;
static float v = 2.0F;
void toreDeCarres(void) {
if ( traj ) {
glPushMatrix();
glRotatef(90.0F,1.0F,0.0F,0.0F);
glutWireTorus(0.05,10.0,20,90);
glPopMatrix(); }
glPushMatrix();
for ( int i = 0 ; i < ns ; i++ ) {
glPushMatrix();
glRotatef(i*360.0F/ns,0.0F,1.0F,0.0F);
glBegin(GL_LINE_LOOP);
glVertex3f(8.0F,2.0F,0.0F);
glVertex3f(8.0F,-2.0F,0.0F);
glVertex3f(12.0F,-2.0F,0.0F);
glVertex3f(12.0F,2.0F,0.0F);
glEnd();
glPopMatrix(); }
glPopMatrix();
}
void scene(void) {
glPushMatrix();
glTranslatef(10.0F,0.0F,0.0F);
glColor3f(1.0F,1.0F,0.0F);
toreDeCarres();
glPopMatrix();
glPushMatrix();
glTranslatef(-10.0F,0.0F,0.0F);
glColor3f(1.0F,1.0F,1.0F);
toreDeCarres();
glPopMatrix();
}
void redraw1() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
gluLookAt(0.0F,18.0F,50.0F,0.0F,-2.0F,0.0F,0.0F,1.0F,0.0F);
glPushMatrix();
if ( tourneADroite ) {
glTranslatef(10.0F,0.0F,0.0F);
glRotatef(angle,0.0F,1.0F,0.0F);
glTranslatef(10.0F,0.0F,0.0F); }
else {
glTranslatef(-10.0F,0.0F,0.0F);
glRotatef(-angle,0.0F,1.0F,0.0F);
glTranslatef(-10.0F,0.0F,0.0F); }
glColor3f(1.0F,1.0F,1.0F);
glutWireSphere(0.5,20,20);
glBegin(GL_LINES);
glVertex3f(0.0F,0.0F,0.0F);
glVertex3f(0.0F,0.0F,-3.0F);
glEnd();
glPopMatrix();
scene();
glPopMatrix();
glutSwapBuffers();
}
void redraw2() {
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
glPushMatrix();
float ox;
float oz;
float vx;
float vz;
if ( tourneADroite ) {
float a = -3.14159F/180.0F*angle;
float x = 10.0F*cos(a);
float z = 10.0F*sin(a);
ox = 10.0F+x;
oz = z;
vx = ox+z;
vz = oz-x; }
else {
float a = 3.14159F/180.0F*angle;
float x = -10.0F*cos(a);
float z = -10.0F*sin(a);
ox = -10.0F+x;
oz = z;
vx = ox-z;
vz = oz+x; }
gluLookAt(ox,0.0F,oz,vx,0.0F,vz,0.0F,1.0F,0.0F);
scene();
glPopMatrix();
glutSwapBuffers();
}
void reshape1(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.,(float)w/(float)h,0.2,120.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void reshape2(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0,(float)w/(float)h,1.0,120.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idle(void) {
angle += v;
if ( angle > 180.0F ) {
angle -= 360.0F;
tourneADroite = droite; }
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
}
void special(int k,int x,int y) {
switch(k) {
case GLUT_KEY_UP : v *= 1.1F;
break;
case GLUT_KEY_DOWN : v /= 1.1F;
break;
case GLUT_KEY_RIGHT : droite = 0;
break;
case GLUT_KEY_LEFT : droite = 1;
break; }
}
void key(unsigned char key,int x,int y) {
static int anim = 1;
switch ( key ) {
case 43 : ns++;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 45 : ns--;
if ( ns < 10 )
ns = 10;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case 't' :
case 'T' : traj = !traj;
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
break;
case ' ' : anim = !anim;
glutIdleFunc((anim) ? idle : NULL);
break;
case 0x1B : exit(0);
break ; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitWindowPosition(50,50);
glutInitWindowSize(550,200);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
f1 = glutCreateWindow("Vue exterieure");
glutDisplayFunc(redraw1);
glutReshapeFunc(reshape1);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutInitWindowPosition(70,300);
f2 = glutCreateWindow("Vue interieure");
glutDisplayFunc(redraw2);
glutReshapeFunc(reshape2);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutIdleFunc(idle);
glutMainLoop();
return(0);
}