How to handle alpha composition using OpenGL

I used glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)for alpha composition as the document said (and in fact the same thing was said in the Direct3D document).

At first, everything was fine until I downloaded the result from the GPU and made it a PNG image. The result of the alpha component is incorrect. Before drawing, I cleared the frame buffer with an opaque black color. And after I drew something translucent, the frame buffer became translucent.

+4
source share
1 answer

Well, the reason is obvious. With glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)we actually ignore the target alpha channel and consider it always to be 1. This is normal when we treat the frame buffer as opaque.

But what if we need the right alpha value? glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)and make the original pre-multiplex (pre-multiplied texture / vertex color, or multiply the alpha component by alpha components before setting it to gl_FragColor).

glBlendFunc , - , one_minus_src_alpha dst_alpha. . , , , . (: 0.5, 0.5, 0.5, 0.5 1.0, 1.0, 1.0, 0.5) glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA), - . , ,

+6

All Articles