我已经按照以下概念在OpenGL中实现了掩蔽:
- 掩膜由黑色和白色颜色组成。
- 前景纹理只应在遮罩的白色部分可见。
- 背景纹理只应在遮罩的黑色部分可见。
我可以通过使用glBlendFunc()使白色部分或黑色部分按预期工作,但不能同时使用两种颜色,因为前景层不仅会混合到掩模层,还会混合到背景层。
有没有人知道如何以最佳方式完成这项任务?我已经在网上搜索并阅读了一些关于片元着色器的内容。这是正确的方法吗?
我已经按照以下概念在OpenGL中实现了掩蔽:
我可以通过使用glBlendFunc()使白色部分或黑色部分按预期工作,但不能同时使用两种颜色,因为前景层不仅会混合到掩模层,还会混合到背景层。
有没有人知道如何以最佳方式完成这项任务?我已经在网上搜索并阅读了一些关于片元着色器的内容。这是正确的方法吗?
这应该可以正常工作:
glEnable(GL_BLEND);
// Use a simple blendfunc for drawing the background
glBlendFunc(GL_ONE, GL_ZERO);
// Draw entire background without masking
drawQuad(backgroundTexture);
// Next, we want a blendfunc that doesn't change the color of any pixels,
// but rather replaces the framebuffer alpha values with values based
// on the whiteness of the mask. In other words, if a pixel is white in the mask,
// then the corresponding framebuffer pixel's alpha will be set to 1.
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
// Now "draw" the mask (again, this doesn't produce a visible result, it just
// changes the alpha values in the framebuffer)
drawQuad(maskTexture);
// Finally, we want a blendfunc that makes the foreground visible only in
// areas with high alpha.
glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);
drawQuad(foregroundTexture);
Stefan Monov的回答非常好!但是对于那些仍然无法使他的答案工作的人:
如果你得到了0 - 前往EGLConfig并确保你传递了alpha bits
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_ALPHA_SIZE, 8, <- 我没有这个并且浪费了很多时间
EGL14.EGL_DEPTH_SIZE, 16,