OpenGL - reproducing a transparency effect from a black background to a white one
My program use开发者_如何学JAVAs blending via glBlendFunc(GL_SRC_ALPHA, GL_ONE)
to make a pleasent effect when the background is set to black with clearColor(0,0,0,1)
. When
the background is set to white with clearColor(1,1,1,1)
the screen is blank white. I don't know how to generate the same trasparency effect on the white background. Suggestions appreciated.
glBlendFunc(GL_SRC_ALPHA, GL_ONE)
roughly translates into the following equation (the clamping happens at another place, technically)
New_Framebuffer_Value :=
New_Red := min(1, Incoming_Red * Incoming_Alpha + Previous_Framebuffer_Red )
New_Green := min(1, Incoming_Green * Incoming_Alpha + Previous_Framebuffer_Green )
New_Blue := min(1, Incoming_Blue * Incoming_Alpha + Previous_Framebuffer_Blue )
However if you cleared to while, the Previous_Framebuffer_{Red,Green,Blue}
are already at 1.0 and so all following will be clamped.
So you need to modulate not only the incoming fragments, but also the previous ones:
New_Framebuffer_Value :=
New_Red := min(1, Incoming_Red * Incoming_Alpha + Previous_Framebuffer_Red * ( 1 - Incoming_Alpha ) )
New_Green := min(1, Incoming_Green * Incoming_Alpha + Previous_Framebuffer_Green * ( 1 - Incoming_Alpha ) )
New_Blue := min(1, Incoming_Blue * Incoming_Alpha + Previous_Framebuffer_Blue * ( 1 - Incoming_Alpha ) )
which is done by glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
There's not a lot of information here, but I'd suggest trying glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
. This is the most "typical" blend mode for achieving alpha blending. Each color value in the source pixel is multiplied by the source pixel's alpha value, and added to destination pixel's color value multiplied by, well, one minus the source pixel's alpha.
This would work the same as the quoted blend mode on a black background, but introduce the correct "amount" of the background color as it shifted away from black.
精彩评论