Python,Pyglet 在模板蒙版内绘制形状像素

Posted

技术标签:

【中文标题】Python,Pyglet 在模板蒙版内绘制形状像素【英文标题】:Python, Pyglet draw shape pixels inside stencil mask 【发布时间】:2017-07-31 19:58:13 【问题描述】:

我正面临着这个问题,我试图通过在互联网上找到的多种方式来解决这个问题,但我尝试的所有方法都没有任何视觉差异。我想绘制一个丢弃圆形遮罩之外的所有像素的形状,在下面的示例代码中,我使用矩形遮罩而不是圆形,只是为了展示我正在尝试做的事情。而我唯一能做的就是在蓝色形状上打一个洞。我想剪掉面具外面的所有东西,就像剪刀一样,但我想用圆形面具,并且光滑。你们能帮帮我吗?缺少哪些代码行?

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

【问题讨论】:

【参考方案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()

【讨论】:

以上是关于Python,Pyglet 在模板蒙版内绘制形状像素的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL:在背景图像上使用蒙版绘制颜色

从四个点绘制填充形状

为啥 Pyglet 不能正确绘制多边形?

如何在处理中加载的 png 图像上创建颜色剪贴蒙版?

Alpha蒙版边界框透明与其他alpha蒙版冲突/覆盖。 XNA

我想在 UIView 上绘制像苹果形状这样的自定义形状