OpenGL中的Alpha蒙版

5

我想在OpenGL中使用alpha掩码,使白色(1)=可见,黑色(0)=隐藏。

所以我写了一些代码来在framebuffer的alpha分量中使用glColorMask(False, False, False, True)(我正在使用Python),然后使用混合技术绘制一些几何图形。

但它并没有起作用: 我尝试将alpha缓冲区完全填充为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。)

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


1
最终结果是黑色背景上的白色三角形?我在您的步骤中没有发现任何错误。 - Bahbar
2个回答

3

如果您还没有这样做,创建GL上下文时尝试请求一个alpha缓冲区


那确实是问题所在!非常感谢! - abenthy

0
使用 glAlphaFunc(GL_GREATER, 0.5);

他在使用混合而不是alpha测试。Alpha测试针对源alpha而非目标alpha。 - Bahbar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接