使用 OpenGL 的 Alpha 蒙版

发布于 2024-08-19 07:02:35 字数 1386 浏览 6 评论 0原文

我想在 OpenGL 中使用 alpha 蒙版,以便白色(1)=可见,黑色(0)=隐藏。

所以我要做的就是使用 glColorMask(False, False, False, True) 在帧缓冲区的 alpha 分量中写入一些内容(我正在使用 python,你看),然后在其上方绘制一些几何图形使用混合。

但它不起作用: 我尝试用 0 完全填充 alpha 缓冲区,然后绘制一些不应该可见的几何图形。但它总是出现,阿尔法缓冲区被完全忽略。

# Clear alpha buffer to 0, and clear color buffer.
# After this, the alpha buffer should probaby be filled with 0.
glClearColor(0, 0, 0, 0)
glClear(GL_COLOR_BUFFER_BIT)

# Disable blending.
glDisable(GL_BLEND)

# Disable color writing.
glColorMask(False, False, False, True)

# Set color to a white with alpha 0.
glColor4f(1, 1, 1, 0)

# Now draw a fullscreen quad.
# After this, the alpha buffer should really be filled with 0.
# Shouldn't it?
glBegin(GL_QUADS)
glVertex2f(0, 0)
glVertex2f(320, 0)
glVertex2f(320, 480)
glVertex2f(0, 480)
glEnd()

# Enable color writing.
glColorMask(True, True, True, True)

# Enable blending so that incoming fragments are multiplied
# by alpha values already in the buffer.
glEnable(GL_BLEND)
glBlendFunc(GL_DST_ALPHA, GL_ONE)

# Set color to a white with alpha 1.
glColor4f(1, 1, 1, 1)    

# Now draw a triangle.
# It should not be visible because alpha in framebuffer is 0
# and 0 * 1 = 0.
glBegin(GL_TRIANGLES)
glVertex2f(20, 50)
glVertex2f(300, 50)
glVertex2f(160, 210)
glEnd()

(是的,投影矩阵是正确的,所以我的屏幕范围从 0/0 到 320/240。)

三角形不应该是可见的,我做错了什么?

I want to use an alpha mask in OpenGL so that white(1)=visible and black(0)=hidden.

So what I do is I write something in the alpha component of the framebuffer using glColorMask(False, False, False, True) (I'm using python, you see) and then draw some geometry above it using blending.

But it isn't working:
I tried filling the alpha buffer completly with 0 and then drawing some geometry that should thus not be visible. But it always shows up, the alpha buffer is completly ignored.

# Clear alpha buffer to 0, and clear color buffer.
# After this, the alpha buffer should probaby be filled with 0.
glClearColor(0, 0, 0, 0)
glClear(GL_COLOR_BUFFER_BIT)

# Disable blending.
glDisable(GL_BLEND)

# Disable color writing.
glColorMask(False, False, False, True)

# Set color to a white with alpha 0.
glColor4f(1, 1, 1, 0)

# Now draw a fullscreen quad.
# After this, the alpha buffer should really be filled with 0.
# Shouldn't it?
glBegin(GL_QUADS)
glVertex2f(0, 0)
glVertex2f(320, 0)
glVertex2f(320, 480)
glVertex2f(0, 480)
glEnd()

# Enable color writing.
glColorMask(True, True, True, True)

# Enable blending so that incoming fragments are multiplied
# by alpha values already in the buffer.
glEnable(GL_BLEND)
glBlendFunc(GL_DST_ALPHA, GL_ONE)

# Set color to a white with alpha 1.
glColor4f(1, 1, 1, 1)    

# Now draw a triangle.
# It should not be visible because alpha in framebuffer is 0
# and 0 * 1 = 0.
glBegin(GL_TRIANGLES)
glVertex2f(20, 50)
glVertex2f(300, 50)
glVertex2f(160, 210)
glEnd()

(Yes, the projection matrices are right so my screen ranges from 0/0 to 320/240.)

The triangle shouldn't be visible, what did I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会傲 2024-08-26 07:02:35

创建 GL 上下文时尝试请求 alpha 缓冲区 ,如果你还没有的话。

Try asking for an alpha buffer when you create your GL context, if you aren't already.

娇妻 2024-08-26 07:02:35

使用 glAlphaFunc( GL_GREATER, 0.5 );

Use glAlphaFunc( GL_GREATER, 0.5 );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文