L'exécutable

Image102.gif (8135 octets)

Le source: Gears.cpp

/* $Id: gears.c,v 3.0 1998/02/14 18:42:29 brianp Exp $ */

/*
 * 3-D gear wheels.  This program is in the public domain.
 *
 * Brian Paul
 */


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

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

#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.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 GLint gear1 ;
static GLint gear2;
static GLint gear3;
static GLfloat angle = 0.0;
static GLfloat da = 2.0;
static GLuint limit;

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  manipulateurSouris();
  manipulateurClavier();
  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();
  glutSwapBuffers();
}

void idle(void) {
  angle += da;
  glutPostRedisplay() ;
}

void myinit(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_AUTO_NORMAL);
  glEnable(GL_NORMALIZE);
}

void key(unsigned char key,int x,int y) {
  if ( keyManipulateur(key,x,y) )
    glutPostRedisplay();
    else
    switch ( key ) {
      case 32   : da *= -1 ;
                  glutPostRedisplay();
                  break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Gears"); 
  myinit(); 
  creationMenuBasique();
  setParametresFrustumBasique(-1.0F,1.0F,-1.0,1.0F,5.0F,60.0F,0.0F,0.0F,-40.0F);
  setManipulateurDistance(40.0F);
  glutReshapeFunc(reshapeFrustumBasique);
  glutKeyboardFunc(key);
  glutSpecialFunc(specialBasique);
  glutMotionFunc(motionBasique);
  glutMouseFunc(sourisBasique);
  glutDisplayFunc(display);
  glutIdleFunc(idle);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

WB01624_.gif (281 octets) RETOUR