#include #include #include /* * bezier.c: cubic bezier curve * * Of necessity, this is 2D. * * All calcs are 3d, though. * * 3/31/03 pjf * */ #define NPMAX 10000 GLfloat p[NPMAX][3]; int np=0; // # of valid control points int curcp = -1; int ww,hh; float b0(float u) { return (1.0-u)*(1.0-u)*(1.0-u)/6.0; } float b1(float u) { return (4.0-6.0*u*u+3.0*u*u*u)/6.0;} float b2(float u) { return (1.0+3.0*u+3.0*u*u-3.0*u*u*u)/6.0; } float b3(float u) { return u*u*u/6.0; } /* display callback */ void display() { float u; int i; glClear(GL_COLOR_BUFFER_BIT); if (np>3) { glColor3f(1.0,1.0,1.0); glBegin(GL_LINE_STRIP); for(i=0;i<(np-3);i++) { for (u=0.0;u<=1.0;u+=0.01) { GLfloat x = b0(u)*p[i+0][0] + b1(u)*p[i+1][0] + b2(u)*p[i+2][0] + b3(u)*p[i+3][0]; GLfloat y = b0(u)*p[i+0][1] + b1(u)*p[i+1][1] + b2(u)*p[i+2][1] + b3(u)*p[i+3][1]; GLfloat z = b0(u)*p[i+0][2] + b1(u)*p[i+1][2] + b2(u)*p[i+2][2] + b3(u)*p[i+3][2]; glVertex3f(x,y,z); } } glEnd(); glColor3f(0.8,0.2,0.3); glPointSize(5); glBegin(GL_POINTS); for(i=0;i<(np-3);i++) { for (u=0.0;u<=1.0;u+=1.0) { GLfloat x = b0(u)*p[i+0][0] + b1(u)*p[i+1][0] + b2(u)*p[i+2][0] + b3(u)*p[i+3][0]; GLfloat y = b0(u)*p[i+0][1] + b1(u)*p[i+1][1] + b2(u)*p[i+2][1] + b3(u)*p[i+3][1]; GLfloat z = b0(u)*p[i+0][2] + b1(u)*p[i+1][2] + b2(u)*p[i+2][2] + b3(u)*p[i+3][2]; glVertex3f(x,y,z); } } glEnd(); } // now draw boxes around control points for (i=0;i