L'exécutable

Le source: Gears.cpp

#include "windows.h"

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

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

#ifndef M_PI
#define M_PI 3.14159265F
#endif

static float cosF(double v) {
  return((float) cos(v)) ;
}

static float sinF(double v) {
  return((float) sin(v)) ;
}

static void gear(GLfloat inner_radius,GLfloat outer_radius,GLfloat width,GLint teeth,GLfloat tooth_depth ) {
  GLint i;
  GLfloat r0,r1,r2;
  GLfloat angle,da;
  GLfloat u,v,len;
  r0 = inner_radius;
  r1 = outer_radius-tooth_depth/2.0F;
  r2 = outer_radius+tooth_depth/2.0F;
  da = 2.0F*M_PI/teeth/4.0F;
  glShadeModel(GL_FLAT);
  glNormal3f(0.0,0.0,1.0);
  glBegin(GL_QUAD_STRIP);
  for ( i = 0 ; i <= teeth ; i++ ) {
    angle = i * 2.0F*M_PI / teeth;
    glVertex3f(r0*cosF(angle),r0*sinF(angle),width*0.5F);
    glVertex3f(r1*cosF(angle),r1*sinF(angle),width*0.5F);
    glVertex3f(r0*cosF(angle),r0*sinF(angle),width*0.5F);
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),width*0.5F); }
  glEnd();
  glBegin(GL_QUADS);
  da = 2.0F*M_PI/teeth/4.0F;
  for ( i = 0 ; i < teeth ; i++ ) {
    angle = i * 2.0F*M_PI / teeth;
    glVertex3f(r1*cosF(angle),r1*sinF(angle),width*0.5F);
    glVertex3f(r2*cosF(angle+da),r2*sinF(angle+da),width*0.5F);
    glVertex3f(r2*cosF(angle+2*da),r2*sinF(angle+2*da),width*0.5F);
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),width*0.5F); }
  glEnd();
  glNormal3f(0.0,0.0,-1.0);
  glBegin(GL_QUAD_STRIP);
  for ( i = 0 ; i <= teeth ; i++ ) {
    angle = i * 2.0F*M_PI / teeth;
    glVertex3f(r1*cosF(angle),r1*sinF(angle),-width*0.5F);
    glVertex3f(r0*cosF(angle),r0*sinF(angle),-width*0.5F);
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),-width*0.5F);
    glVertex3f(r0*cosF(angle),r0*sinF(angle),-width*0.5F); }
  glEnd();
  glBegin(GL_QUADS);
  da = 2.0F*M_PI/teeth/4.0F;
  for (i=0;i<teeth;i++) {
    angle = i * 2.0F*M_PI / teeth;
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),-width*0.5F);
    glVertex3f(r2*cosF(angle+2*da),r2*sinF(angle+2*da),-width*0.5F);
    glVertex3f(r2*cosF(angle+da),r2*sinF(angle+da),-width*0.5F);
    glVertex3f(r1*cosF(angle),r1*sinF(angle),-width*0.5F); }
  glEnd();
  glBegin(GL_QUAD_STRIP);
  for ( i = 0 ; i < teeth ; i++ ) {
    angle = i * 2.0F*M_PI / teeth;
    glVertex3f(r1*cosF(angle),r1*sinF(angle),width*0.5F );
    glVertex3f(r1*cosF(angle),r1*sinF(angle),-width*0.5F );
    u = r2*cosF(angle+da) - r1*cosF(angle);
    v = r2*sinF(angle+da) - r1*sinF(angle);
    len =(float) sqrt(u*u + v*v);
    u /= len;
    v /= len;
    glNormal3f(v,-u,0.0);
    glVertex3f(r2*cosF(angle+da),r2*sinF(angle+da),width*0.5F);
    glVertex3f(r2*cosF(angle+da),r2*sinF(angle+da),-width*0.5F);
    glNormal3f(cosF(angle),sinF(angle),0.0 );
    glVertex3f(r2*cosF(angle+2*da),r2*sinF(angle+2*da),width*0.5F);
    glVertex3f( r2*cosF(angle+2*da),r2*sinF(angle+2*da),-width*0.5F);
    u = r1*cosF(angle+3*da) - r2*cosF(angle+2*da);
    v = r1*sinF(angle+3*da) - r2*sinF(angle+2*da);
    glNormal3f(v,-u,0.0);
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),width*0.5F);
    glVertex3f(r1*cosF(angle+3*da),r1*sinF(angle+3*da),-width*0.5F);
    glNormal3f(cosF(angle),sinF(angle),0.0); }
  glVertex3f(r1*cosF(0),r1*sinF(0),width*0.5F);
  glVertex3f(r1*cosF(0),r1*sinF(0),-width*0.5F);
  glEnd();
  glShadeModel(GL_SMOOTH);
  glBegin(GL_QUAD_STRIP);
  for ( i = 0 ; i <= teeth ; i++ ) {
    angle = i * 2.0F*M_PI / teeth;
    glNormal3f(-cosF(angle),-sinF(angle),0.0);
    glVertex3f(r0*cosF(angle),r0*sinF(angle),-width*0.5F);
    glVertex3f(r0*cosF(angle),r0*sinF(angle),width*0.5F); }
  glEnd();
}

static GLfloat view_rotx=20.0 ;
static GLfloat view_roty=30.0 ;
static GLfloat view_rotz=0.0;
static GLint gear1 ;
static GLint gear2;
static GLint gear3;
static GLfloat angle = 0.0;
static GLfloat da = 2.0;
static GLuint limit;

static void CALLBACK draw(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();
  glTranslatef(-3.0,-2.0,0.0);
  glRotatef(angle,0.0,0.0,1.0);
  glCallList(gear1);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(3.1F,-2.0F,0.0F);
  glRotatef(-2.0F*angle-9.0F,0.0F,0.0F,1.0F);
  glCallList(gear2);
  glPopMatrix();
  glPushMatrix();
  glTranslatef(-3.1F,4.2F,0.0F);
  glRotatef(-2.0F*angle-25.0F,0.0F,0.0F,1.0F);
  glCallList(gear3);
  glPopMatrix();
  glPopMatrix();
  auxSwapBuffers();
}

static void CALLBACK idle(void) {
  angle += da;
  draw();
}

static void CALLBACK reshape(int width,int height) {
  GLfloat  h = (GLfloat) height/(GLfloat) width;
  glViewport(0,0,(GLint)width,(GLint)height);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-1.0,1.0,-h,h,5.0,60.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0F,0.0F,-40.0F);
}

static void init(void) {
  static GLfloat pos[4] = {5.0F,5.0F,10.0F,0.0F };
  static GLfloat red[4] = {0.8F,0.1F,0.0F,1.0F };
  static GLfloat green[4] = {0.0F,0.8F,0.2F,1.0F };
  static GLfloat blue[4] = {0.2F,0.2F,1.0F,1.0F };
  glLightfv(GL_LIGHT0,GL_POSITION,pos);
  glEnable(GL_CULL_FACE);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glEnable(GL_DEPTH_TEST);
  gear1 = glGenLists(1);
  glNewList(gear1,GL_COMPILE);
  glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,red);
  gear(1.0F,4.0F,1.0F,20,0.7F);
  glEndList();
  gear2 = glGenLists(1);
  glNewList(gear2,GL_COMPILE);
  glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,green);
  gear(0.5F,2.0F,2.0F,10,0.7F);
  glEndList();
  gear3 = glGenLists(1);
  glNewList(gear3,GL_COMPILE);
  glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,blue);
  gear(1.3F,2.0F,0.5F,10,0.7F);
  glEndList();
  glEnable(GL_NORMALIZE);
}

void CALLBACK up(void) {
  view_rotx++ ;
}

void CALLBACK down(void) {
  view_rotx-- ;
}

void CALLBACK left(void) {
  view_roty++ ;
}

void CALLBACK right(void) {
  view_roty-- ;
}

void CALLBACK keyz(void) {
  view_rotz++ ;
}

void CALLBACK keyZ(void) {
  view_rotz-- ;
}

void CALLBACK space(void) {
  da *= -1 ;
}

void main(void) {
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGB|AUX_DEPTH);
  auxInitPosition(0,0,300,300);
  auxInitWindow("Gears");
  init();
  auxKeyFunc(AUX_UP,up) ;
  auxKeyFunc(AUX_DOWN,down) ;
  auxKeyFunc(AUX_LEFT,left) ;
  auxKeyFunc(AUX_RIGHT,right) ;
  auxKeyFunc(AUX_z,keyz) ;
  auxKeyFunc(AUX_Z,keyZ) ;
  auxKeyFunc(AUX_SPACE,space) ;
  auxIdleFunc(idle);
  auxReshapeFunc(reshape);
  auxMainLoop(draw);
}
 RETOUR