Le source : TD-Bezier-GL-Revolution.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2001 */
/* Utilisation d'une courbe de Bezier */
/* pour generer un objet (un vase) */
/* 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 "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[10][2] = { {0.0F,-5.0F},
{5.0F,-5.0},
{4.5F,-4.0F},
{2.0F,-2.0F},
{1.5F,0.0F},
{0.2F,1.0F},
{0.2F,2.0F},
{1.5F,4.0F},
{1.5F,5.0F},
{0.5F,5.0F} } ;
void points(float *pts) {
glDisable(GL_LIGHTING) ;
glPointSize(5.0);
glColor4fv(couleurCyan());
glBegin(GL_POINTS);
for ( int i = 0 ; i < 40 ; i++ )
glVertex3fv(&pts[i*3]);
glEnd();
glEnable(GL_LIGHTING) ;
}
void traceQuartVaseBezierGL(void) {
glPushMatrix() ;
float pts[10][4][3] ;
for ( int i = 0 ; i < 10 ; i++ ) {
pts[i][0][0] = pts1[i][0] ;
pts[i][0][1] = pts1[i][1] ;
pts[i][0][2] = 0.0F ;
pts[i][1][0] = pts1[i][0] ;
pts[i][1][1] = pts1[i][1] ;
pts[i][1][2] = p*pts1[i][0] ;
pts[i][2][0] = p*pts1[i][0] ;
pts[i][2][1] = pts1[i][1] ;
pts[i][2][2] = pts1[i][0] ;
pts[i][3][0] = 0.0F ;
pts[i][3][1] = pts1[i][1] ;
pts[i][3][2] = pts1[i][0] ; }
glMap2f(GL_MAP2_VERTEX_3,
0.0F,1.0F,
3,4,
0.0F,1.0F,
12,10,
(float *) pts);
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 *) pts) ;
glPopMatrix() ;
}
void traceVaseBezierGL(void) {
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurGrisMoyen());
glPushMatrix() ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurRouge());
traceQuartVaseBezierGL() ;
glRotatef(90.0F,0.0F,1.0F,0.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurVert());
traceQuartVaseBezierGL() ;
glRotatef(90.0F,0.0F,1.0F,0.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurBleu());
traceQuartVaseBezierGL() ;
glRotatef(90.0F,0.0F,1.0F,0.0F) ;
if ( coul )
glMaterialfv(GL_FRONT,GL_DIFFUSE,couleurJaune());
traceQuartVaseBezierGL() ;
glPopMatrix() ;
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|
GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_DEPTH_TEST);
manipulateurSouris();
manipulateurClavier();
glEnable(GL_LIGHTING);
traceVaseBezierGL() ;
glDisable(GL_LIGHTING);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void initlights(void) {
GLfloat pos[] = { 0.0F,0.0F,-1.0F,0.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 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("Vase par revolution");
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);
}