supersuraccoon
supersuraccoon

Reputation: 1691

Opengl premultiplied alpha image render process

I'v been learning OpenGL2.0 rendering stuffs for a while and here are some personal understanding on alpha rendering I want to know if they are right:

Say I have a PNG (32 bit) file (none premultiplied alpha image) with only one pixel (src image)

PNG                                                 Shader - glFragColor        GPU - SRC_ALPHA           SRC_ONE
(255, 0, 0, 255) - red / none transparent           (1.0, 0.0, 0.0, 1.0)        (1.0, 0.0, 0.0, 1.0)      (1.0, 0.0, 0.0, 1.0)
(255, 0, 0, 128) - red / half transparent           (1.0, 0.0, 0.0, 0.5)        (0.5, 0.0, 0.0, 0.75)     (1.0, 0.0, 0.0, 1.0)
(128, 0, 0, 255) - drak red / none transparent      (0.5, 0.0, 0.0, 1.0)        (0.5, 0.0, 0.0, 1.0)      (0.5, 0.0, 0.0, 1.0)
(128, 0, 0, 128) - drak red / half transparent      (0.5, 0.0, 0.0, 0.5)        (0.25, 0.0, 0.0, 0.75)    (0.5, 0.0, 0.0, 1.0)

Here are four examples and 4 columns for each. Take row NO.2 for example:

1. The PNG image is red but half transparent so the rgba should be (255, 0, 0, 128)
2. The texture info is passed to frag shader and the value for glFragColor shoud be (1.0, 0.0, 0.0, 0.5)
3. When rendering the src color to GPU OpenGL will try to bend it with the dst color
4. let's say the dst color is (0, 0, 0, 255) and blend func for dst is GL_ONE_MINUS_SRC_ALPHA
5. if the blend func for dst is GL_SRC_ALPHA, the final pixel rendered will be (0.5, 0.0, 0.0, 0.75)
6. if the blend func for dst is GL_ONE, the final pixel rendered will be (1.0, 0.0, 0.0, 1.0)

The result for (6) is definitely wrong since the transparency lost while the result for (5) should be good.

And that's why I should use GL_SRC_ALPHA for none premultiplied alpha image and GL_ONE for premultiplied alpha image.

Upvotes: 0

Views: 381

Answers (0)

Related Questions