Thursday, September 8, 2016

Bezier Curve

Quadratic:

Cubic:



In case of the cubic formula P0, P1 and P2 are your control points. t has to be a value between 0 and 1 and represents the "position" on the curve. By incrementing t step by step you'll get several points you can use to actually draw the curve.

So using the above formula you could do something like
glBegin(GL_LINE_STRIP);
for(float t=0; t <= 1; t += 0.1) {
     float x = (1-t)*(1-t)*p0.x + 2(1-t)*t*p1.x + t*t*p2.x;
     float y = (1-t)*(1-t)*p0.y + 2(1-t)*t*p1.y + t*t*p2.y;
     float z = (1-t)*(1-t)*p0.z + 2(1-t)*t*p1.z + t*t*p2.z;

     glVertex3f(x, y, z);
}
glEnd();
The smaller the steps you use for t the smoother the curve will become. That's it. Really.
Generic:
http://www.gamedev.net/topic/534082-drawing-a-curve-in-opengles---how/

iOS:
http://stackoverflow.com/questions/5054790/cgpathref-bezier-curves-in-opengl-es

Android:
http://blog.uncle.se/2012/02/opengl-es-tutorial-for-android-part-ii-building-a-polygon/

Unity:
http://www.theappguruz.com/blog/bezier-curve-in-games

No comments:

Post a Comment