Smooth textured line with OpenGL ES 2.0 shaders

We have an iOS drawing application. Currently, the drawing is implemented using OpenGL ES 1.1. We use some algorithms to smooth lines, such as Bezier curves. So, when touch events occur, we get some set of points from the touch points (based on algorithms) and draw these points. We also use the texture of the eyeglass brush to have a more natural look.

I wonder if it is possible to implement these algorithms in OpenGL ES 2.0 shaders. Something like calling the OpenGL function to draw lines made from touch points and output, it has smooth graphics with a texture texture.

enter image description here

Points P0, P1, ... P4 here are touch events and points on the red curve โ€” output points with such a step for T so that the distance between two adjacent points on the curve does not exceed 1 pixel.

And here is the link with the Bezier algorithm: Bezier curve - Wikipedia, the free encyclopedia

Any help is greatly appreciated. Thanks.

+6
source share
1 answer

You cannot generate new vertices inside the vertex shader (you can do this in a geometric shader that ES does not have). The number of output vertices always coincides with the number of input vertices, you can only change your positions (and, of course, the ohter attributes).

Thus, you will need to draw a linear strip made of enough vertices to guarantee a smooth enough curve. What you can do is always the same linear strip having the value of the curve parameter T as 1D vertex positions. In the shader, you then use this input position (parameter value) to calculate the actual 2D / 3D position on the curve using the DeKastelau algorithm (or any other) and points P0-P4 that you put in the shader as constants (uniform variables in GLSL terms )

But I'm not sure if that would really buy you anything by simply calculating these points on the processor and incorporating them into dynamic VBO. What you save is copying the curve points from the CPU to the GPU and computing to the CPU, but on the other hand, your vertex shader is much more complicated. It needs to be assessed, which is the best approach. If you need to calculate the points of the curve in each frame (since the control points change each frame), and the curve is pretty detailed, this might not be so bad. But otherwise I donโ€™t think it really pays. And also your shader will not easily adapt to easily changing the number of control points / degree of the curve at runtime.

But again, you cannot set 5 control points and create N curve points on the GPU. A vertex shader always works at one vertex and outputs one vertex, just like a fragment shader, always works on one fragment (for example, a pixel, although it is not yet one) and leads to a single (or missing) fragment.

+6
source

Source: https://habr.com/ru/post/919929/


All Articles