Search Projects

Monday, December 21, 2015

Drawing Bézier curves in Opengl C++

Curves in Computer graphics is one of the important topics to learn. Today we are going to learn about the about a special curve type - Bezier Curve. First we know about the curves, then what is bezier curves then we will learn drawing bezier curves in opengl.


Curves are set of indefinite set of points which need not to be straight. Curves are differently defines in mathematics, hence context will tell the proper definition. A curve can be in 2d (plane curves) or in 3d (space or Euclidean Space curves). A line is special type of curve which is straight. A curve is represented with some set of equation called the equation of curve.

Classification of Curves

There are mainly two types of curves -
  • Open Curve : The curves which have no endpoints are called open curves. Eg - Parabola, Hyperbola.
  • Closed Curve : When the curves end points are joined, they are called closed curves. Eg - Circle, Ellipse.
The curves can be further classified into three types -  implicit, explicit and parametric curves.
  • Implicit Curve - These type of curves implicitly represent the set of points on a curve which can be easily tested. The general function form is  - f(x, y) = 0. Eg.  Cricle (x2 + y2 - R2 = 0, R = Radius)
  • Explicit Curve - Curves which is explicitly represented and for each value of x there is only one y can be calculated are Explicit curves. They are generally represented with the function y = f(x). Eg. Cubic curves (Y=aX3+bx2+cx+d)
  • Parametric Curve - The curves that can be represented by parametric form, described by expressing the position of all its points as function of one parameter. The general functional form is P(t) = f(t), g(t). Eg. Bezier Curve.

Bézier Curves

Bézier curves are parametric curves that are generated with the control points. It is widely used in computer graphics and other related industry, as they appear reasonably smooth at all scales. Bézier curves was name after french engineer Pierre Bézier, who discover it. Mathematically Bézier curves is represented as -

Bézier curves are of different degree - linear curves, quadratic curve, cubic curve and high order curve. See the images below taken from wikipedia.

Quadratic Bézier curves

Cubic Bézier curves

Drawing Bézier curves in opengl

To draw bezier curve opengl c++ you need to define the curve center points. First we will define a function that will compute the points of the curve. This functions is very important for drawing bezier curves in opengl. 

 void bezierCoefficients(int n, int *c)
{ int k,i; for(k=0;k<=n;k++)
{ c[k]=1; for(i=n;i>=k+1;i--)
c[k]*=i; for(i=n-k;i>=2;i--)
 } } 

In bezier curve opengl code display function we will define an array that holds the x, y coefficients of end points. In this function only we will define the line width which will define the width of curve. The bezierCoefficients function is called, then we looped it to form the curve. We are going to use the GL_LINE_STRIP to develop the curve. In the drawing bezier curves in opengl we also need to define the glPointSize as well as set the gluOrtho2D to certain x, y values which define the curve path.

I hope you like this article showcasing the bezier curve opengl c++ and learn more about drawing the curve. Download the full bezier curve opengl code from below and tell us about it in comments.

No comments:

Post a Comment