| |
|
|
||||
![]() |
||||||
| |
|
|||||
|
Rendering Bézier Curves Now that we have defined Bézier curves, we would like to display them. There are a couple of ways to do this. The most straightforward way, shown in Listing 1, is to evaluate the curve equation along constant steps in t between 0 and 1. The finer the steps that are taken, the higher quality the curve will be. Since the basis functions are polynomials, this process can be sped up by using forward differencing. Figure 6 shows the same curve rendered with different step sizes. /* * Calculates a Bezier curve directly by iterating along the curve * for the desired number of steps. * * Point is an object that has the x, y, and z coordinates and defines * some mathematical operations for points. See the project accompanying * the article for its definition. * * G is the geometry vector, the three control points for the curves. * steps is the number of steps to take along the curve. */ void QuadraticBezierIterate(Point G[], int steps) { float stepSize = 1.0 / steps; float t = stepSize; // Draw the curve as a line strip glBegin(GL_LINE_STRIP); // First control point is on the curve glVertex3f(G[0].x, G[0].y, G[0].z); // Calculate intermediate points for(int step = 1; step < steps; step++, t += stepSize) { // Calculate the blending functions float b0 = (1 - t) * (1 - t); float b1 = 2.0 * t * (1 - t); float b2 = t * t; // Blend float x = b0 * G[0].x + b1 * G[1].x + b2 * G[2].x; float y = b0 * G[0].y + b1 * G[1].y + b2 * G[2].y; float z = b0 * G[0].z + b1 * G[1].z + b2 * G[2].z; // Emit a vertex glVertex3f(x, y, z); } // Last control point is on the curve glVertex3f(G[2].x, G[2].y, G[2].z); glEnd(); } |
|
|