L'exécutable

Image0001.gif (12088 octets) Image0004.gif (5153 octets)

Le source : TD-CylindreEtCone.cpp

#include <windows.h>

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

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

static float anglex = 0.0F ;
static float angley = 0.0F ;

void cylindre(double h,double r,int n) {
  double *cs =(double *) calloc(n+1,sizeof(double)) ;
  double *sn =(double *) calloc(n+1,sizeof(double)) ;
  double *ncs =(double *) calloc(n+1,sizeof(double)) ;
  double *nsn =(double *) calloc(n+1,sizeof(double)) ;
  cs[0] = cs[n] = r ;
  sn[0] = sn[n] = 0.0 ;
  ncs[0] = ncs[n] = 1.0 ;
  nsn[0] = nsn[n] = 0.0 ;
  for ( int i = 1 ; i < n ; i++ ) {
    double a = 3.14159/n*2*i ;
    ncs[i] = cos(a) ;
    nsn[i] = sin(a) ;
    cs[i] = r*ncs[i] ;
    sn[i] = r*nsn[i] ; }
  glBegin(GL_POLYGON) ;
  glNormal3f(0.0,1.0,0.0) ;
  for ( i = 0 ; i < n ; i++ ) {
    glVertex3d(cs[i],h/2,sn[i]); }
  glEnd() ;
  glBegin(GL_POLYGON) ;
  glNormal3f(0.0,-1.0,0.0) ;
  for ( i = 0 ; i < n ; i++ ) {
    glVertex3d(cs[i],-h/2,sn[i]); }
  glEnd() ;
  glBegin(GL_QUADS) ;
  for ( i = 0 ; i < n ; i++ ) {
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(cs[i],h/2,sn[i]);
    glNormal3d(ncs[i+1],0.0,nsn[i+1]) ;
    glVertex3d(cs[i+1],h/2,sn[i+1]);
    glVertex3d(cs[i+1],-h/2,sn[i+1]);
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(cs[i],-h/2,sn[i]); }
  glEnd() ;
  free(sn) ;
  free(cs) ;
  free(nsn) ;
  free(ncs) ;
}

void cone(double h,double r,int n) {
  double *cs =(double *) calloc(n+1,sizeof(double)) ;
  double *sn =(double *) calloc(n+1,sizeof(double)) ;
  double *ncs =(double *) calloc(n+1,sizeof(double)) ;
  double *nsn =(double *) calloc(n+1,sizeof(double)) ;
  cs[0] = cs[n] = r ;
  sn[0] = sn[n] = 0.0 ;
  ncs[0] = ncs[n] = 1.0 ;
  nsn[0] = nsn[n] = 0.0 ;
  for ( int i = 1 ; i < n ; i++ ) {
    double a = 3.14159/n*2*i ;
    ncs[i] = cos(a) ;
    nsn[i] = sin(a) ;
    cs[i] = r*ncs[i] ;
    sn[i] = r*nsn[i] ; }
  glBegin(GL_POLYGON) ;
  glNormal3f(0.0,-1.0,0.0) ;
  for ( i = 0 ; i < n ; i++ ) {
    glVertex3d(cs[i],-h/2,sn[i]); }
  glEnd() ;
  glBegin(GL_QUADS) ;
  for ( i = 0 ; i < n ; i++ ) {
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(0,h/2,0);
    glNormal3d(ncs[i+1],0.0,nsn[i+1]) ;
    glVertex3d(0,h/2,0);
    glVertex3d(cs[i+1],-h/2,sn[i+1]);
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(cs[i],-h/2,sn[i]); }
  glEnd() ;
/*  glBegin(GL_TRIANGLES) ;
  for ( i = 0 ; i < n ; i++ ) {
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(0.0,h/2,0.0);
    glNormal3d(ncs[i],0.0,nsn[i]) ;
    glVertex3d(cs[i],-h/2,sn[i]);
    glNormal3d(ncs[i+1],0.0,nsn[i+1]) ;
    glVertex3d(cs[i+1],-h/2,sn[i+1]);
  }*/
  glEnd() ;
  free(sn) ;
  free(cs) ;
  free(nsn) ;
  free(ncs) ;
}

void CALLBACK display(void) {
  glClearColor(0.5F,0.5F,0.5F,0.0F);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  glRotatef(angley,0.0F,1.0F,0.0F) ;
  glRotatef(anglex,1.0F,0.0F,0.0F) ;
  float jaune[] = { 1.0F,1.0F,0.5F,1.0F };
  float bleu[] = { 0.0F,0.0F,1.0F,1.0F };
  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,bleu) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,jaune) ;
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ;
  glTranslatef(0.0F,0.7F,0.0F) ;
  cylindre(0.8,0.4,40) ;
  float rouge[] = { 1.0F,0.2F,0.2F,1.0F };
  glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,rouge) ;
  glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,jaune) ;
  glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ;
  glTranslatef(0.0F,-1.1F,0.0F) ;
  cone(1.4,0.4,40) ;
  glPopMatrix();
  glFlush();
  auxSwapBuffers();
}

void myinit (void) {
  glShadeModel(GL_SMOOTH);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  float jaune[] = { 0.6F,0.6F,0.0F,1.0F };
  glLightfv(GL_LIGHT0,GL_SPECULAR,jaune) ;
}

void CALLBACK myReshape(int w, int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-1.3,1.3,-1.3/w*h,1.3/w*h,-3.0,3.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

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

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

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

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

void main(void) {
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH);
  auxInitPosition (0,0,300,300);
  auxInitWindow("Cylindre et cone") ;
  myinit();
  auxKeyFunc(AUX_UP,up) ;
  auxKeyFunc(AUX_DOWN,down) ;
  auxKeyFunc(AUX_LEFT,left) ;
  auxKeyFunc(AUX_RIGHT,right) ;
  auxReshapeFunc(myReshape);
  auxMainLoop(display);
}
WB01624_.gif (281 octets) RETOUR