How to intersect two smooth shapes in OpenGL?

I successfully painted two smooth shapes in OpenGL, using a procedure that generates a triangular strip, in which the extreme line of the edge has all its vertices in alpha 0 . Now I want to cross them, but I always seem to lose the smooth edges of one shape. Here is the code I'm using:

 // Draw: smooth black shape as normal glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); // Draw: smooth black shape into alpha channel glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glBlendFunc(GL_DST_ALPHA, GL_SRC_ALPHA); // Draw: Yellow overlay shape with black shape alpha // Reset blending modes for rest of program 

Combined shapes

And here is the result (bottom) - the yellow figure loses its smooth right edge, because the alpha in these pixels is now equal to 1. How can I get a smooth intersecting shape?

+4
source share
2 answers

I struggled with this problem for a long time and tried every drawing order and a combination of glColorMask and glBlendEquation . In the end, I realized there is a very simple solution - pre-multiplied alpha.

In my procedure for drawing a "smooth shape", and not just to paint the outer smoothing edge as a single color, alpha=0 , I allowed to specify the outer smoothing color. For the yellow form, I set the color to black, and this gave me a flat edge while the overall shape had a smooth edge - although the right right edge of the yellow shape is not masked by the alpha channel.

0
source

You need something like this snippet:

 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); glEnable(GL_BLEND); 

I think the key element is glEnable (GL_BLEND).

0
source

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


All Articles