macospygletstencil-buffer

pyglet stencil test on Mac OS


I am trying to use a stencil test to clip and create borders for 2d content, but it does not seem to work. It might be a platform issue, I am running the code on a M1 MacBook Air with MacOS 12.0.1

From the code below I would expect a blue square with a 50px red border, I get only a big red square.

from pyglet import window, gl, shapes, app
win = window.Window(width=800, height=600)


@win.event
def on_draw():
    win.clear()
    gl.glEnable(gl.GL_STENCIL_TEST)
    gl.glStencilMask(gl.GL_TRUE)
    gl.glClearStencil(0)
    gl.glClear(gl.GL_STENCIL_BUFFER_BIT)
    gl.glStencilFunc(gl.GL_NEVER, 1, 0xFF)
    gl.glStencilOp(gl.GL_REPLACE, gl.GL_KEEP, gl.GL_KEEP)
    square = shapes.Rectangle(x=200, y=200, width=200, height=200, color=(0, 0, 255))
    square.draw()
    gl.glStencilMask(gl.GL_FALSE)
    gl.glStencilFunc(gl.GL_NOTEQUAL, 1, 0xFF)
    square2 = shapes.Rectangle(x=150, y=150, width=300, height=300, color=(255, 0, 0))
    square2.draw()
    gl.glDisable(gl.GL_STENCIL_TEST)


app.run()

Solution

  • The window does not have a stencil buffer. There is no guarantee that the window's default framebuffer will have a stencil buffer by default. Explicitly specify the stencil buffer bits (see pyglet, Context configuration):

    config = gl.Config(double_buffer=True, stencil_size=8)
    win = window.Window(width=800, height=600, config=config)