I am trying a couple of tutorials from http://nehe.gamedev.net, in order to learn openGL programming, I would like to position spheres along a Bezier curve such that they appear as a string of pearls. how can I position such spheres along the curve. I am drawing the curve using de Casteljau's algorithm and hence can get the XYZ points on the curve.
If your spheres are small enough relative to the overall length of the Bezier curve, you can just position your spheres at even intervals to get an appearance similar to a string of pearls. (If the spheres are relatively large then you'll have to start worrying about sphere overlap more -- not an easy problem, and probably not very instructive for learning OpenGL.)
The parameter value t
of a Bezier curve varies from 0
to 1
. To evaluate your Bezier curve at 10 locations (the ends and eight interior points) you can do something like this:
for( int i = 0; i <= 9; ++i )
{
double t = i / 9.0;
double x, y;
EvalBezier( t, x, y );
DrawSphere( x, y, radius );
}
Where EvalBezier( t, x, y )
fills in (x,y)
for a given t
. Just pick radius
to give you a pleasing result. If you want to try to pick radius
automatically, just use half the minimum distance from the point i
to points i-1
and i+1
as a rough estimate. If you do this, remember to handle the end points specially, using either only the next or previous points (whichever you have).