Le source : TD-Bezier-GL-Tuyau.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Utilisation d'une courbe de Bezier */
/* pour generer un objet (un tuyau) */
/* par revolution */
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleManipulateur.h"
#include "ModuleFont.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
static int cyl = 0 ;
static int coul = 0 ;
static int pt = 0 ;
static int ff = 0 ;
static int np1 = 20 ;
static int np2 = 20 ;
float p = 0.55F ;
float pts1[16][3] = { {1.0F,0.0F,1.0F},
{1.0F,p,1.0F},
{p,1.0F,1.0F},
{0.0F,1.0F,1.0F},
{1.0F,0.0F,0.3F},
{1.0F,p,0.3F},
{p,1.0F,0.3F},
{0.0F,1.0F,0.3F},
{1.0F,0.0F,-0.3F},
{1.0F,p,-0.3F},
{p,1.0F,-0.3F},
{0.0F,1.0F,-0.3F},
{1.0F,0.0F,-1.0F},
{1.0F,p,-1.0F},
{p,1.0F,-1.0F},
{0.0F,1.0F,-1.0F} } ;
void traceTuyau(float r,float h) {
int i;
glPushMatrix() ;
glScalef(r,r,h/2) ;
glColor4fv(couleurBlanc());
glBegin(GL_LINES) ;
for ( i = 0 ; i < 90 ; i++ ) {
float x =(float) cos(i*3.1459/45) ;
float y =(float) sin(i*3.1459/45) ;
glVertex3f(x,y,1.0F) ;
glVertex3f(x,y,-1.0F) ; }
glEnd() ;
glBegin(GL_LINE_LOOP) ;
for ( i = 0 ; i < 90 ; i++ ) {
float x =(float) cos(i*3.1459/45) ;
float y =(float) sin(i*3.1459/45) ;
glVertex3f(x,y,1.0F) ; }
glEnd() ;
glBegin(GL_LINE_LOOP) ;
for ( i = 0 ; i < 90 ; i++ ) {
float x =(float) cos(i*3.1459/45) ;
float y =(float) sin(i*3.1459/45) ;
glVertex3f(x,y,-1.0F) ; }
glEnd() ;
glPopMatrix() ;
}
void points(float *pts) {
glDisable(GL_LIGHTING) ;
glPointSize(5.0);
glColor4fv(couleurCyan());
glBegin(GL_POINTS);
for ( int i = 0 ; i < 16 ; i++ )
glVertex3fv(&pts[i*3]);
glEnd();
glEnable(GL_LIGHTING) ;
}
void traceQuartTuyauBezierGL(float r,
float h) {
glPushMatrix() ;
glScalef(r,r,h/2) ;
glMap2f(GL_MAP2_VERTEX_3,0.0F,1.0F,
3,4,
0.0F,1.0F,
12,4,
(float *) pts1);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glMapGrid2f(np1,0.0,1.0,np2,0.0,1.0);
if ( ff )
glEvalMesh2(GL_LINE,0,np1,0,np2);
else
glEvalMesh2(GL_FILL,0,np1,0,np2);
if ( pt )
points((float *) pts1) ;
glPopMatrix() ;
}
void traceTuyauBezierGL(float r,float h) {
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisMoyen());
glPushMatrix() ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
traceQuartTuyauBezierGL(r,h) ;
glRotatef(90.0F,0.0F,0.0F,1.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert());
traceQuartTuyauBezierGL(r,h) ;
glRotatef(90.0F,0.0F,0.0F,1.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu());
traceQuartTuyauBezierGL(r,h) ;
glRotatef(90.0F,0.0F,0.0F,1.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
traceQuartTuyauBezierGL(r,h) ;
glPopMatrix() ;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|
GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_DEPTH_TEST);
manipulateurSouris();
manipulateurClavier();
glRotatef(30.0F,1.0F,1.0F,1.0F);
glEnable(GL_LIGHTING);
traceTuyauBezierGL(4.0F,9.0F) ;
glDisable(GL_LIGHTING);
if ( cyl )
traceTuyau(4.0F,9.0F) ;
glPopMatrix();
glPushMatrix();
glDisable(GL_DEPTH_TEST);
glColor4fv(couleurJaune());
placeFontCursor(-6.5F,-6.5F,0.0F);
simpleBitmapOutput("p = %5.2f",p);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void initlights(void) {
GLfloat pos[] = { 0.0F,0.0F,2.0F,1.0F };
GLfloat shininess[] = { 50.0F };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_AMBIENT,couleurGrisFonce());
glLightfv(GL_LIGHT0,GL_POSITION,pos);
glMaterialfv(GL_FRONT,GL_SPECULAR,couleurBlanc());
glMaterialfv(GL_FRONT,GL_SHININESS,shininess);
}
void myinit(void) {
glClearColor(0.0,0.0,0.0,1.0);
glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LESS);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
initlights();
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 43 : { p += 0.01F ;
for ( int i = 0 ; i < 4 ; i++ ) {
pts1[1+i*4][1] = p ;
pts1[2+i*4][0] = p ; } }
glutPostRedisplay();
break;
case 45 : { p -= 0.01F ;
for ( int i = 0 ; i < 4 ; i++ ) {
pts1[1+i*4][1] = p ;
pts1[2+i*4][0] = p ; } }
glutPostRedisplay();
break;
case ' ' : cyl = 1-cyl;
glutPostRedisplay();
break;
case 0x0D : coul = 1-coul;
glutPostRedisplay();
break;
case 'p' : pt = 1-pt;
glutPostRedisplay();
break;
case 'a' : ff = 1-ff;
glutPostRedisplay();
break;
case 'q' : np1++;
glutPostRedisplay();
break;
case 'Q' : np1--;
if ( np1 < 3 )
np1 = 3;
glutPostRedisplay();
break;
case 's' : np2++;
glutPostRedisplay();
break;
case 'S' : np2--;
if ( np2 < 3 )
np2 = 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("Surface de Bezier : Tuyau");
myinit();
creationMenuBasique();
setParametresOrthoBasique(-7.0,7.0,-7.0,7.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}