Recall from our Discussion page that points in a Bezier curve are defined with functions of a parameter U. The parametric functions are constructed from a set of control points. Thus, by evaluating the functions for a set of parameter values, we obtain vertices that define the rendered geometry. Since we are approximating a curve, we rendered the vertices as a line strip. But before evaluation can be done, we must first provide OpenGL with the control points. The latter is accomplished with a call to an OpenGL "Map" function. An example of such a call as used in the tutorial follows.
glMap1d(GL_MAP1_VERTEX_3,
lowerU, upperU,
stride,
numberOfControlPoints,
&controlPoints[0][0]);
The identifiers lowerU and upperU are doubles that set the range of values for the parameter U. Generally we use 0.0 and 1.0 for the lower and upper bounds. Stride is an int that basically gives the number of coordinates for each control point; for our purposes, we set the stride to 3. The last parameter in the call is the address of the first entry of the array of doubles holding the control points. The following is the declaration of the array when numberOfControlPoints is 6.
double controlPoints[6][3];A call to the map function may be made in our initialization function unless a change to any of the parameters occurs during runtime.
Also before evaluating the parametric functions, we must also enable the mapping with the following call.
glEnable(GL_MAP1_VERTEX_3);As usual, you can also disable the mapping, if need be, with the call
glDisable(GL_MAP1_VERTEX_3);
With the above two OpenGL function calls, we can now proceed to obtaining vertices and drawing the approximating curve. In particular, we obtain one vertex at a time with the following call.
glEvalCoord1f(U);where the parameter U is in the range specified by lowerU and upperU given in the call to glMap1d. As usual, we give the vertices of the geometry within a glBegin-glEnd pair and for our work, we set geometry type set as GL_LINE_STRIP. For example, the following code snippet creates a drawing of the Bezier curve, approximated with line segments.
int numberOfSegments = 32;
glBegin(GL_LINE_STRIP);
for (int i=0; i<=numberOfSegments; i++) {
double u = (double) i / numberOfSegments;
glEvalCoord1f(u);
glEnd();
Observe that the call to glEvalCoord1f is equivalent to makeing the call glVertex3f(x, y, z) where (x,y,z) is a point in the Bezier curve. Of course, if the latter function call is used, then we would have to compute the coordinates using the formulae given in the Discussion page.