2010-01-25 15 views
5

Quiero utilizar una máscara alfa en OpenGL para que blanco (1) = visible y negro (0) = oculto.Máscaras alfa con OpenGL

Entonces, lo que hago es escribir algo en el componente alfa del framebuffer usando glColorMask(False, False, False, True) (estoy usando python, ves) y luego dibujar algo de geometría encima usando blending.

Pero no funciona: Intenté llenar el buffer alfa completamente con 0 y luego dibujar algo de geometría que no debería ser visible. Pero siempre aparece, el buffer alfa es completamente ignorado.

# 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() 

(Sí, las matrices de proyección están justo por lo oscila mi pantalla desde 0/0 a 320/240.)

El triángulo no debe ser visible, lo que hice mal?

+1

¿El resultado final es un triángulo blanco sobre fondo negro? No veo nada defectuoso en tus pasos. – Bahbar

Respuesta

3

Intente solicitar un alpha buffer cuando cree su contexto GL, si no lo está haciendo todavía.

+0

¡Ese fue en realidad el problema! ¡Muchas gracias! – abenthy

0

Utilice glAlphaFunc (GL_GREATER, 0.5);

+0

err ... Está usando mezclas, no pruebas alfa. Las pruebas alfa funcionan contra la fuente alfa, no la alfa de destino. – Bahbar