/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Fonctions de dessin de cylindre */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int np = 40;
struct point3D {
float x ;
float y ;
float z ; } ;
struct vecteur3D {
float dx ;
float dy ;
float dz ; } ;
void cylindre(double r,double h,int np) {
int i;
point3D *tab =(point3D *) malloc(2*np*sizeof(point3D)) ;
for ( i = 0 ; i < np ; i++ ) {
double a = 3.14159/np*2*i ;
double cs = cos(a) ;
double sn = sin(a) ;
tab[i].x = tab[i+np].x =(float) (r*cs) ;
tab[i].y =(float) (h/2.0) ;
tab[i+np].y =(float) (-h/2.0) ;
tab[i].z = tab[i+np].z =(float) (-r*sn) ; }
glBegin(GL_POLYGON) ;
for ( i = 0 ; i < np ; i++ ) {
glVertex3fv((float *) &tab[i]) ; }
glEnd() ;
glBegin(GL_POLYGON) ;
for ( i = 2*np-1 ; i >= np ; i-- ) {
glVertex3fv((float *) &tab[i]) ; }
glEnd() ;
glBegin(GL_QUAD_STRIP) ;
for ( i = 0 ; i <= np ; i++ ) {
glVertex3fv((float *) &tab[i%np]);
glVertex3fv((float *) &tab[(i%np)+np]); }
glEnd() ;
free(tab) ;
}
void cylindreAvecNormales(double r,double h,int np) {
float normale[3];
int i;
glGetFloatv(GL_CURRENT_NORMAL,normale) ;
point3D *tab =(point3D *) malloc(2*np*sizeof(point3D)) ;
vecteur3D *norm =(vecteur3D *) malloc(np*sizeof(vecteur3D)) ;
for ( i = 0 ; i < np ; i++ ) {
double a = 3.14159/np*2*i ;
double cs = cos(a) ;
double sn = sin(a) ;
tab[i].x = tab[i+np].x =(float) (r*cs) ;
tab[i].y =(float) (h/2.0) ;
tab[i+np].y =(float) (-h/2.0) ;
tab[i].z = tab[i+np].z =(float) (-r*sn) ;
norm[i].dx =(float) cs ;
norm[i].dy =(float) 0.0 ;
norm[i].dz =(float) -sn ; }
glBegin(GL_POLYGON) ;
for ( i = 0 ; i < np ; i++ ) {
glNormal3f(0.0f,1.0f,0.0f);
glVertex3fv((float *) &tab[i]) ; }
glEnd() ;
glBegin(GL_POLYGON) ;
for ( i = 2*np-1 ; i >= np ; i-- ) {
glNormal3f(0.0f,-1.0f,0.0f);
glVertex3fv((float *) &tab[i]) ; }
glEnd() ;
glBegin(GL_QUAD_STRIP) ;
for ( i = 0 ; i <= np ; i++ ) {
glNormal3fv((float *) &norm[i%np]);
glVertex3fv((float *) &tab[i%np]);
glVertex3fv((float *) &tab[(i%np)+np]); }
glEnd() ;
free(tab) ;
free(norm) ;
glNormal3fv(normale) ;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
manipulateurSouris();
manipulateurClavier();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurBleu()) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurJaune()) ;
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ;
glTranslatef(0.0F,0.65F,0.0F) ;
glNormal3f(0.0f,0.0f,1.0f);
cylindre(0.4,0.8,np) ;
glPopMatrix();
glPushMatrix();
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,couleurRouge()) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,couleurJaune()) ;
glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,90) ;
glTranslatef(0.0F,-0.65F,0.0F) ;
cylindreAvecNormales(0.4,0.8,np) ;
glPopMatrix();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurJauneClair()) ;
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 43 : np++ ;
glutPostRedisplay();
break ;
case 45 : np-- ;
if ( np < 3 )
np = 3 ;
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("Cylindre");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-1.3F,1.3F,-1.3F,1.3F,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}