Reputation: 3036
the title must seem somewhat cryptic but I could not really explain there what I want to do, so I drew a picture to visualize my problem:
The black parts are transparent (aka alpha = 0). I have the blue object (left) in the framebuffer and want to render the white bitmap (middle) onto it, so that it looks like the merged bitmap (right).
The problem is that if I use the standard glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
the whole part of the white object is being displayed. I don't want it to completely overlap the stuff in the framebuffer (blue) but only be visible on the parts where it has an alpha value > 0 (is visible). And then it should also still takes its own alpha values into account (notice the hole in the white object).
Is something like this possible with glBlendFunc or do I have to write a shader for this ?
PS: I looked at the documentation of glBlendFunc at http://www.khronos.org/opengles/documentation/opengles1_0/html/glBlendFunc.html but I don't really get anywhere with it.
PPS: I am using OpenGL-ES 2.0 on Android with C++, but I don't think the language/platform matters all that much.
Upvotes: 1
Views: 406
Reputation: 35943
I don't think it will be possible to do this purely with the blend function. You want the source pixel to be multiplied by both the source and destination alpha, while the blendfunc only allows one or the other.
However the result you want may be possible with some use of the stencil buffer. I'm not an expert in it but I think you can set the stencil op to increment while drawing the background image, and then when you draw the bitmap set the stencil test to reject where stencil == 0 (with blending still enabled to get the transparent area of the bitmap correct). You'll have to review the API for glStencilOp
and glStencilFunc
to figure out the exact right arguments to use.
It might also be possible with some combination of glBlendFunc and glAlphaFunc, but it would depend on the order of which they are evaluated, so I'm not positive.
Upvotes: 1