Search Projects

Sunday, January 15, 2017

OpenGL Moving Bézier curves Program in C/C++

In our many tutorial, we have explained about how to create various objects, diagrams, things in OpenGL.  In our earlier tutorial we have explained about how to draw Bézier curves in opengl C/C++ with source code. In this post we will use the same source code and bring motion to our Bézier curves. You will see OpenGL Moving Bézier curves Program in C/C++ explained in this post.

Before going to the tutorial we are going to see the output of our program. Below gif will demo the out put and show Moving Bézier curve.

A curve is collection of set of points which are not in a straight line. They exist in 2d as well as 3d space. Bézier curves are parametric curves which can be generate with some set of points called as control points. Bézier curves are of different degree - linear curves, quadratic curve, cubic curve and high order curve. Following image will show the mathematical representation of Bézier curves -

In our example we will use simple Bézier curves, and give motion to them using two control points.

First we define the global variables which we can use in more than one set of function or methods. We have four set of control points, one each at end of curve and two in the middle. Using the middle control points we will give the curve a movement to change their position in 2d plane.
int bzco[4][2]={{0,0},{99,201},{204,49},{320,300}},c[4],n=3;
You can experiments with the bzco and choose the specific points for motion.

Next is to draw bezier curve, for which we need to define the curve center points. Firs of all 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;

In display function we will call the above function and use the GL_LINE_STRIP in loop to develop the curve. The array of x, y coefficients will help in containing value for control and end points. Size of points is also defined using the glPointSize and gluOrtho2D to certain x, y values which define the curve path. You can see code in source code download below.

We have finished drawing Bézier curve but this post is about Moving Bézier curves Program. Hence we need to define a function called motion(), which will give movement to the curve. Simple logic we use to determine the extent of movement is the x, y coefficient point of 2d plane. We will set the value to which curve can move and then came down from there. For this we will use the array which we have declared as global variable.
void motion(void)
if(bzco[1][0]<0 bzco="">320)
if(bzco[1][1]<0 bzco="">300)
if(bzco[2][0]<0 bzco="">320)
if(bzco[2][1]<0 bzco="">300)
In the above code you can see that first we add the number(distance) to the array and then negate it. So, logic is to first add the value to coefficient to go up and bring them down. You can see we have chosen 300, 320 two different control points. You all can experiment with these points as well and see the the magic yourselves. The glutPostRedisplay(); repeat drawing of objects on screen with call back to main function, hence will repeat the same.

Download the source code of this program from the link given below.

No comments:

Post a Comment