2017-07-31 92 views
0

我正面臨着我在互聯網上發現的許多方面都要解決的麻煩,但是我嘗試的所有方法都沒有任何視覺差異。我想繪製一個形狀,丟棄圓形面具外的所有像素,在示例代碼中,我使用的是矩形蒙版而不是圓形,僅用於顯示我正在嘗試執行的操作。而我唯一能做的就是一個藍色的洞。我想剪掉面具外的所有東西,就像剪刀一樣,但是我想用一個圓圈面具,並且光滑。你們能幫我嗎?缺少哪些代碼行?Python,Pyglet在模具掩模內繪製形狀像素

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

我解決了這個問題:

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