/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2002 */
/* 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"
#ifndef M_PI
#define M_PI 3.14159
#endif
static int np = 40;
void glutSolidCylinderSansNormales(float h,float r,int sa,int sh){
int i;
glPushMatrix();
glNormal3f(0.0F,0.0F,1.0F);
glBegin(GL_QUADS);
for( i = 0 ; i < sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float aj=(2*M_PI*(i+1))/sa;
float si=sin(ai);
float sj=sin(aj);
float ci=cos(ai);
float cj=cos(aj);
for( int j = 0 ; j < sh ; j++ ){
float yi=-h/2+j*h/sh;
float yj=yi+h/sh;
glVertex3f(r*cj,yi,r*sj);
glVertex3f(r*cj,yj,r*sj);
glVertex3f(r*ci,yj,r*si);
glVertex3f(r*ci,yi,r*si); } }
glEnd();
glBegin(GL_POLYGON);
for( i = 0 ; i <= sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float si=sin(ai);
float ci=cos(ai);
glVertex3f(r*ci,-h/2,r*si); }
glEnd();
glBegin(GL_POLYGON);
for( i = 0 ; i <= sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float si=sin(ai);
float ci=cos(ai);
glVertex3f(r*ci,h/2,r*si); }
glEnd();
glPopMatrix();
}
void glutSolidCylinderAvecNormales(float h,float r,int sa,int sh){
int i;
glPushMatrix();
glBegin(GL_QUADS);
for( i = 0 ; i < sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float aj=(2*M_PI*(i+1))/sa;
float si=sin(ai);
float sj=sin(aj);
float ci=cos(ai);
float cj=cos(aj);
for( int j = 0 ; j < sh ; j++ ){
float yi=-h/2+j*h/sh;
float yj=yi+h/sh;
glNormal3f(cj,0,sj);
glVertex3f(r*cj,yi,r*sj);
glVertex3f(r*cj,yj,r*sj);
glNormal3f(ci,0,si);
glVertex3f(r*ci,yj,r*si);
glVertex3f(r*ci,yi,r*si); } }
glEnd();
glNormal3f(0.0F,-1.0F,0.0F);
glBegin(GL_POLYGON);
for( i = 0 ; i <= sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float si=sin(ai);
float ci=cos(ai);
glVertex3f(r*ci,-h/2,r*si); }
glEnd();
glNormal3f(0.0F,1.0F,0.0F);
glBegin(GL_POLYGON);
for( i = 0 ; i <= sa ; i++ ){
float ai=(2*M_PI*i)/sa;
float si=sin(ai);
float ci=cos(ai);
glVertex3f(r*ci,h/2,r*si); }
glEnd();
glPopMatrix();
}
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);
glutSolidCylinderAvecNormales(0.8,0.4,np,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) ;
glutSolidCylinderSansNormales(0.8,0.4,np,np) ;
glPopMatrix();
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void myinit (void) {
glClearColor(0.5F,0.5F,0.5F,0.0F);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float dir[4] = { 1.0F,1.0F,1.0F,0.0F };
glLightfv(GL_LIGHT0,GL_POSITION,dir) ;
glLightfv(GL_LIGHT0,GL_SPECULAR,couleurJauneClair()) ;
}
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(250,250);
glutInitWindowPosition(50,50);
glutCreateWindow("Cylindres");
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);
}