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;
	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--)
			c[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)
{
	bzco[1][0]+=s1x;
	bzco[1][1]+=s1y;
	bzco[2][0]+=s2x;
	bzco[2][1]+=s2y;
if(bzco[1][0]<0 bzco="">320)
{
	s1x=-s1x;
}
if(bzco[1][1]<0 bzco="">300)
{
	s1y=-s1y;
}
if(bzco[2][0]<0 bzco="">320)
{
	s2x=-s2x;
}
if(bzco[2][1]<0 bzco="">300)
{
	s2y=-s2y;
}
glutPostRedisplay();
}
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