I'm trying to draw a B-Spline given 11 control points. I'm using freeglut and C++. I was able to display my control points easily. I then wrote the basis functions as follows:
float B0(float u){
return float(pow(u - 1, 3) / 6.0);
}
float B1(float u){
return float((3 * pow(u, 3) - 6 * pow(u, 2) + 4) / 6.0);
}
float B2(float u){
return float((-3 * pow(u, 3) + 3 * pow(u, 2) + 3 * u + 1) / 6.0);
}
float B3(float u){
return float(pow(u, 3) / 6.0);
}
Here's my algorithm that computes a 100 points per segment and stores the computed points in an array to be displayed, note that MAX_POINTS is a global and that points is an array of structs with floats x, y, and z for each:
void computeBSpline(){
int MAX_STEPS = 100;
int count = 0;
for (int i = 0; i < NUM_OF_POINTS - 3; i++)
{
cout << "Computing for P" << i << " P " << i + 1 << " P " << i + 2 << " P " << i + 3 << endl;
for (int j = 0; j <= MAX_STEPS; j++)
{
float u = float(j) / float(MAX_STEPS);
float Qx = B0(u) * points[i].x + B1(u) * points[i + 1].x + B2(u) * points[i + 2].x + B3(u) * points[i + 3].x;
float Qy = B0(u) * points[i].y + B1(u) * points[i + 1].y + B2(u) * points[i + 2].y + B3(u) * points[i + 3].y;
pointsBSpline[count].x = Qx;
pointsBSpline[count].y = Qy;
//cout << count << '(' << Qx << ", " << Qy << ")\n";
count++;
}
}
cout << "Computed " << count << " points!\n";
}
I later call this DrawBSpline() which will loop through the array and display all the points:
void DrawBSpline(){
glPointSize(1.5);
glColor3d(0.0, 0.0, 0.0);
int i = 0;
glBegin(GL_POINTS);
for (; i < NUM_OF_BSPLINE_POINTS; i++){
glVertex2d(pointsBSpline[i].x, pointsBSpline[i].y);
}
glEnd();
}
This is my program's output: B-Spline output
I'm just not sure how to join the segments and produce one smooth curve. Any help would be appreciated.