2
\$\begingroup\$

OpenGl is capable of rendering points, lines, triangles and quads; but what if i want to draw a bezier curve? I read online you should use something called GL_STRIP, but the solutions were using either legacy Opengl, or they were not explaining well the process. The question is: How can i render bezier curves in Modern Opengl? I'm using C++.

\$\endgroup\$
  • 2
    \$\begingroup\$ To my knowledge there's no built-in bezier curve primitive, so your options are either approximating it with a sequence of short straight line/strip segments, or drawing a bounding polygon with a custom shader to evaluate the curve function per pixel. Do you have a preference of the two? \$\endgroup\$ – DMGregory Feb 15 '18 at 22:10
  • \$\begingroup\$ @DMGregory i think that for the moment, the first approach is the most suitable and easiest of the two, so I'm gonna use that one.. \$\endgroup\$ – user100681 Feb 16 '18 at 3:26
5
\$\begingroup\$

First of all, the basics:

The bèzier equations are very simple. You basically take the line segment created by the points, get the lines between them, then you linearly interpolate a point between the starting and ending point of each line. This way you get one less point than before. You do the same with the lines defined by these, and so on until you get a single point. This will draw your bezier curve.

You can also just use specialized equations, like for instance for the 4 point version:

$$P = (1−t)^3P_1 + 3(1−t)^2tP_2 +3(1−t)t^2P_3 + t^3P_4$$

After this you just loop through between 0 and 1 in as many steps as precise you want the curve to be and plug the value into the interpolations or into the equation as t. This gives you 2d vectors. You can put those values into a buffer and render them using GL_LINES (make sure to use LINES, LINE doesn't work) and voilá, you have your bézier curve.

A bézier curve has the nice effect of getting denser around curves, so you won't have to make the precision much higher to get a smooth curve

After the happiness of rendering something smooth subsides, forget what I just said and never use built-in lines in production again. They don't work very well, when they're wider than a couple of pixels. Instead create a custom one using triangles, but that topic is far too complicated to cover here. You should be able to either figure out or find a tutorial about it.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy