Reputation: 939
I am trying to mask an image with OpenGL so that part of it is transparent. Here's my code, which isn't working:
draw_img(background);
...
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_ONE, GL_ZERO);
draw_img(mask);
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
draw_img(foreground);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Here is my background image, over which I am trying to draw:
Here are the texture and the mask, respectively (The white in the mask is really transparent, I just flattened it so you can see):
This is what I expect to get:
And this is what I actually get:
Any idea what might be the problem?
Upvotes: 4
Views: 756
Reputation: 35923
Well for starters, I don't think this will do what you expect:
glBlendFunc(GL_ONE, GL_ZERO);
draw_img(mask);
This is blending the mask with the background, but you're saying "Take 100% of the mask always, and 0% of the destination always". After this step there will be no more red pixels in the framebuffer, because you've replaced all the channels of the framebuffer with the mask texture.
I guess you were probably intending to replace only the alpha channel, and leave the colors untouched? You can do this if you mask the color channels before drawing the mask, with glColorMask(GL_FALSE,GL_FALSE,GL_FALSE, GL_TRUE);
I think if you do that it might work.
I think this kind of effect might typically be done with a multitexturing operation with glTexEnv, but if you don't want to use multitexturing this should work.
Upvotes: 4