En cyan la courbe générée en utilisant l'implantation OpenGL de calcul des courbes de Bézier
En magenta la courbe obtenue par notre implantation de la formule
Le source : BezierFormule.cppmule.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Fevrier 2019 */
/* Une courbe de Bezier */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleCouleurs.h"
#include "ModuleFont.h"
#include "ModuleManipulateur.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"
struct coord_3D {
GLfloat x = 0.0F;
GLfloat y = 0.0F;
GLfloat z = 0.0F;
GLfloat w = 1.0F; };
struct polygone {
int n = 0;
coord_3D *p = NULL; };
typedef struct coord_3D coord_3D;
typedef struct polygone polygone;
static GLfloat pts[6][4] = {
{ -3.0F,-3.0F,-3.0F, 1.0F },
{ -2.0F, 3.0F, 1.0F, 1.0F },
{ 2.0F,-3.0F,-2.0F, 1.0F },
{ 3.0F, 3.0F,-3.0F, 1.0F },
{ -2.0F,-1.0F, 2.0F, 1.0F },
{ 3.0F,-3.0F,-1.0F, 1.0F } };
static int aff = 2;
static polygone pl;
void point(float x,float y,float z) {
glVertex3f(x,y,z);
}
void bezier(polygone *p,int n) {
int i,j;
float t,mt;
float *cn,x,y,z,fac;
cn =(float *) calloc(p->n,sizeof(float));
cn[0] = 1;
cn[1] =(float) (p->n-1);
for ( i = 2 ; i < p->n ; i++ )
cn[i] = cn[i-1] * (p->n - i) / i;
for ( i = 0 ; i < n ; i++ ) {
t =(float) i/(n-1);
mt = 1-t;
x = y = z = 0.0F;
for ( j = 0 ; j < p->n ; j++ ) {
fac = cn[j]*(float) pow(t,j)*
(float) pow(mt,p->n-1-j);
x += fac * p->p[j].x;
y += fac * p->p[j].y;
z += fac * p->p[j].z; }
point(x,y,z); }
free(cn);
}
void display(void) {
int i;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_DEPTH_TEST);
manipulateurSouris();
manipulateurClavier();
glColor4fv(couleurCyan());
glBegin(GL_LINE_STRIP);
for ( i = 0 ; i <= 30 ; i++ )
glEvalCoord1f((GLfloat) i/30.0F);
glEnd();
glPointSize(5.0);
glColor4fv(couleurJaune());
glBegin(GL_POINTS);
for ( i = 0 ; i < aff ; i++ )
glVertex3fv(&pts[i][0]);
glEnd();
glColor4fv(couleurMagenta());
glPointSize(3.0);
glBegin(GL_POINTS);
bezier(&pl,40);
glEnd();
glDisable(GL_DEPTH_TEST);
glColor4fv(couleurJaune());
for ( i = 0 ; i < aff ; i++ ) {
placeFontCursor(pts[i][0]+0.3F,pts[i][1]+0.3F,pts[i][2]+0.3F);
simpleBitmapOutput("%d",i); }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void init(void) {
glClearColor(0.0,0.0,0.0,1.0);
glMap1f(GL_MAP1_VERTEX_4,0.0,1.0,4,aff,&pts[0][0]);
glEnable(GL_MAP1_VERTEX_4);
}
void key(unsigned char key,int x,int y) {
if ( keyManipulateur(key,x,y) )
glutPostRedisplay();
else
switch ( key ) {
case 0x0D :
aff++;
if ( aff == 7 )
aff = 2;
pl.n = aff;
glMap1f(GL_MAP1_VERTEX_4,0.0,1.0,4,aff,&pts[0][0]);
glutPostRedisplay();
break; }
}
int main(int argc,char **argv) {
pl.n = aff;
pl.p =(coord_3D *) &pts[0][0];
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(300,300);
glutInitWindowPosition(50,50);
glutCreateWindow("Courbe de Bezier");
init();
creationMenuBasique();
setParametresOrthoBasique(-5.0,5.0,-5.0,5.0,-500.0,500.0);
setManipulateurDistance(1.0F);
glutReshapeFunc(reshapeOrthoBasique);
glutKeyboardFunc(key);
glutSpecialFunc(specialBasique);
glutMotionFunc(motionBasique);
glutMouseFunc(sourisBasique);
glutDisplayFunc(display);
glutMainLoop();
return(0);
}