L'exécutable

 

Le source : TP-CameraDansToreCarres.cpp

/* Auteur: Nicolas JANEY               */
/* nicolas.janey@univ-fcomte.fr        */
/* Fevrier 2003                        */
/* Deplacement dans un tore de carres  */

#include <stdlib.h>
#include <math.h>

#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>

static int f1;
static int f2;
static float angle = 0.0F;
static float v = 2.0F;
static int ns = 36;
static int traj = 0;

void scene(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 redraw1() {
  glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
  glPushMatrix();
  gluLookAt(0.0F,8.0F,20.0F,0.0F,-3.0F,0.0F,0.0F,1.0F,0.0F);
  glPushMatrix();
  glRotatef(angle,0.0F,1.0F,0.0F);
  glTranslatef(10.0F,0.0F,0.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 a = -3.14159F/180.0F*angle;
  float x = 10.0F*cos(a);
  float z = 10.0F*sin(a);
  gluLookAt(x,0.0F,z,x+z,0.0F,z-x,0.0F,1.0F,0.0F);
  scene();
  glPopMatrix();
  glutSwapBuffers();
}

void reshape(int w,int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(65.,(float)w/(float)h,1.0,120.0); 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void idle(void) {
  angle += v;
  if ( angle > 180.0F )
    angle -= 360.0F;
  glutPostWindowRedisplay(f1);
  glutPostWindowRedisplay(f2);
}

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 ; }
}

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; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitWindowPosition(50,50);
  glutInitWindowSize(250,200);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  f1 = glutCreateWindow("Vue exterieure");
  glutDisplayFunc(redraw1);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutInitWindowPosition(70,300);
  f2 = glutCreateWindow("Vue interieure");
  glutDisplayFunc(redraw2);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(key);
  glutSpecialFunc(special);
  glutIdleFunc(idle);
  glutMainLoop();
  return(0);
}

RETOUR