2010-01-25 143 views
5

我想在OpenGL中使用alpha蒙版,以便白色(1)=可見和黑色(0)=隱藏。帶有OpenGL的Alpha蒙版

所以我做的是我使用glColorMask(False, False, False, True)(我使用python,你會看到)在framebuffer的alpha組件中編寫一些東西,然後使用混合在它上面繪製一些幾何圖形。

但它不工作: 我試圖用0填充alpha緩衝區,然後繪製一些幾何圖形,因此看不到它。但它總是顯示出來,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範圍爲二百四十○分之三百二十零。)

,我做了什麼錯三角形應該是不可見的?

+1

最終結果是黑色背景上的白色三角形?我沒有發現你的步驟有問題。 – Bahbar 2010-01-25 21:49:55

回答

3

嘗試在創建GL上下文時請求alpha buffer,如果您尚未。

+0

這是在問題!非常感謝! – abenthy 2010-01-29 06:49:03

0

使用glAlphaFunc(GL_GREATER,0.5);

+0

err ...他使用混合,而不是阿爾法測試。阿爾法測試對源alpha,而不是目標alpha。 – Bahbar 2010-01-26 07:40:34