Reputation: 155
I want to use Alpha Blending (SRC_ALPHA, ONE_MINUS_SRC_ALPHA)
, which basically is this:
frag_out.aaaa * frag.out + ((1.0, 1.0, 1.0, 1.0) - frag_out.aaaa) * pixel_color
Let's say the pixel color already on screen is (1.0, 1.0, 1.0, 1.0)
The color I am currently rendering is (1.0, 1.0, 1.0, 0.4)
When I render this the resulting color on screen has an alpha 0.76 (even though it was fully opaque BEFORE)
Indeed:
(0.4, 0.4, 0.4, 0.4) * (1.0, 1.0, 1.0, 0.4) + (0.6, 0.6, 0.6, 0.6) * (1.0, 1.0, 1.0, 1.0) = (1.0, 1.0, 1.0, 0.76)
So basically the color on the screen was opaque (white) and after I put a transparent sprite on top the screen become transparent (0.76 alpha) and I am able to look through the background. It would be perfect if I could blend it like this:
(0.4, 0.4, 0.4, 1.0) * (1.0, 1.0, 1.0, 0.4) + (0.6, 0.6, 0.6, 0.6) * (1.0, 1.0, 1.0, 1.0) = (1.0, 1.0, 1.0, 1.0)
Is it possible to achieve this? If yes, how?
Upvotes: 0
Views: 255
Reputation: 22175
This is perfectly possible when using OpenGL 3.0 and above since the blend functions can there be set separately for RGB and A. For your case this would be
glBlendFuncSeparate(GL_SOURCE_ALPHA, GL_ONE_MINUS_SOURCE_ALPHA, GL_ONE, GL_ONE);
The last parameter specifying the blend factor for destination alpha might have to be adapted since you didn't specify the behavior for destination alpha < 1. For more details have a look at the function documentation.
Upvotes: 3