我正面临着这个问题,我试图通过互联网上找到的很多方式解决这个问题,但我尝试的所有方法都没有任何视觉差异。我想绘制一个形状,丢弃圆形掩模之外的所有像素,在示例代码中,我使用的是矩形蒙版而不是圆形,只是为了展示我正在尝试做的事情。而我唯一能做的就是蓝色的洞。我想剪掉面具外的所有东西,比如剪刀,但我想用圆形面具,并且光滑。你们能帮助我吗?缺少哪些代码行?
import pyglet
from pyglet.gl import gl
from pyglet.gl import*
from pyglet.window import Window
Config = pyglet.gl.Config(sample_buffers=1, samples=16, double_buffer=True)
window = Window(800, 640, caption='Stencil Test Draw with mask', config=Config)
print([i for i in dir(gl) if 'invert'.lower() in i.lower()])
@window.event
def on_draw():
window.clear()
""" alpha blending """
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
""" Trying to smooth stencil Circle Mask """
glEnable(GL_SMOOTH)
""" Draw a background """
glColor4f(1, 1, 0, 1.0)
pyglet.graphics.draw(4, GL_QUADS, ('v2f', [0,0, 0,500, 500,500, 500,0]))
glEnable(GL_STENCIL_TEST)
glEnable(GL_DEPTH_TEST)
glColorMask(False, False, False, False)
""" Draw Stencil Mask (I'm trying with a circle shape, but in this example I use rect) """
pyglet.graphics.draw(4, GL_QUADS, ('v2f', [250,250, 250,300, 300,300, 300,250]))
glColorMask(True, True, True, True)
""" Draw rectangle pixels only inside stencil mask """
glColor4f(0, 0, 1, 1.0)
pyglet.graphics.draw(4, GL_QUADS, ('v2f', [200,200, 200,350, 350,350, 350,200]))
""" Remove stencil test """
glDisable(GL_STENCIL_TEST)
pyglet.app.run()
答案 0 :(得分:1)
我解决了这个问题:
import pyglet
from pyglet.gl import gl
from pyglet.gl import*
from pyglet.window import Window
Config = pyglet.gl.Config(sample_buffers=1, samples=16, double_buffer=True, stencil_size=8)
window = Window(800, 640, caption='Stencil Test Draw with mask', config=Config)
@window.event
def on_draw():
window.clear()
""" alpha blending """
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
""" Trying to smooth stencil Circle Mask """
glEnable(GL_SMOOTH)
"""Clear """
glClearStencil(0)
glEnable(GL_STENCIL_TEST)
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
glStencilFunc(GL_NEVER, 0, 1)
glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT)
""" Draw Stencil Mask (I'm trying with a circle shape, but in this example I use rect) """
glColor4f(1, 0, 0, 1.0)
pyglet.graphics.draw(4, GL_QUADS, ('v2f', [250,250, 250,300, 300,300, 300,250]))
""" Now, we want only the framebuffer to be updated if stencil value is 1 """
""" Draw rectangle pixels only inside stencil mask """
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
glStencilFunc(GL_EQUAL, 1, 1)
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO)
glColor4f(0.0, 1.0, 1.0, 1.0)
pyglet.graphics.draw(4, GL_QUADS, ('v2f', [200,200, 200,350, 350,350, 350,200]))
""" Remove stencil test """
glDisable(GL_STENCIL_TEST)
pyglet.app.run()