gpt4 book ai didi

c++ - OpenGL - 具有多个纹理的蒙版

转载 作者:IT老高 更新时间:2023-10-28 22:23:49 36 4
gpt4 key购买 nike

我已经按照以下概念在OpenGL中实现了 mask :

  • 面具由黑色和白色组成。
  • 前景纹理应仅在蒙版的白色部分可见。
  • 背景纹理应仅在蒙版的黑色部分可见。

我可以使用 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);

这相当棘手,如果有不清楚的地方请告诉我。

别忘了request an alpha buffer创建 GL 上下文时。否则有可能在没有 alpha 缓冲区的情况下获得上下文。

编辑:在这里,我做了一个插图。 illustration

编辑:自从写下这个答案后,我了解到有更好的方法来做到这一点:

  • 如果您受限于 OpenGL 的固定功能管道,请使用纹理环境
  • 如果可以使用着色器,请使用片段着色器。

此答案中描述的方式有效,并且性能并不比这两个更好的选项特别差,但不太优雅且不太灵活。

关于c++ - OpenGL - 具有多个纹理的蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5097145/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com