Thursday 27 April 2017

Computer Graphics- 21. Moving Bezire Curve Using OpenGL

Moving Bezier Curves Using OpenGL

T IS A COMPUTER GRAPHICS PROJECT MADE USING VISUAL STUDIO. IN THIS PROJECT IT SHOWS ABOUT  BEZIER CURVE.

Programming in Visual C++ Using OpenGL, Introduces the three-dimensional computer graphics with OpenGL.

In this post I am going to show the OpenGL code on BEZIER CURVE and the implementation and demonstration of BEZIER CURVE using the computer graphics and OpenGL API.

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.

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.

All these objects are designed with simple OpenGL graphics objects. Do buy the project and I think you will like this project as well put your comment and let us know what more need to be added in this. Also help by contributing your projects either modified or new, we are going to publish your project on this blog with your name. It is a humble request as I single hand can't help student reach that much of projects.

""Type your email-id in the comment box and pay INR 400 on 
1). Paytm no. 7795746605   or
2). Paypal id:- paypal.me/abhishekabhi/400inr
for code & send payment details to solutionsbyabhi@gmail.com""

Please Hit the like button and subscribe the channel (subscription is FREE OF COST) and you will get new video notification next time.

2 comments:

  1. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete
  2. A Computer Science portal for geeks. It contains well written, well thought and well
    explained computer science and programming articles, quizzes and practice/competitive
    programming/company interview Questions.
    website: geeksforgeeks.org

    ReplyDelete