سؤال

أرغب في استخدام قناع ألفا في OpenGL بحيث أبيض (1) = مرئي والأسود (0) = مخفي.

إذن ما أقوم به هو أن أكتب شيئًا في مكون ألفا من برنامج FrameBuffer باستخدام 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.)

لا ينبغي أن يكون المثلث مرئيًا ، ماذا فعلت خطأ؟

هل كانت مفيدة؟

المحلول

حاول طلب ألفا العازلة عند إنشاء سياق GL الخاص بك ، إذا لم تكن بالفعل.

نصائح أخرى

استخدم Glalphafunc (gl_greater ، 0.5) ؛

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top