Can't get masking to work correctly with OpenGL

岁酱吖の 提交于 2019-12-07 09:18:17

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10400651/cant-get-masking-to-work-correctly-with-opengl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!