Search code examples
pythonopenglpygamepyopenglstencil-buffer

Stencil test does not clip Texture


I'm trying to clip a texture using a Stencil Test. The idea is to create a surface (in this example a simple rectangle) to select a region of the texture to be shown (as the image bellow)

Desired result

I created a simple code to do so, where I first perform an ALWAYS stencil test to set all the bits on the stencil buffer to 2, and then change the test to KEEP, which I thought would output the desired result, but nothing happens

import pygame
import sys
from OpenGL.GL import *
from pygame.locals import *

# set pygame screen
pygame.display.set_mode((1000, 500), OPENGL | DOUBLEBUF)
info = pygame.display.Info()

# basic opengl configuration
glViewport(0, 0, info.current_w, info.current_h)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()

# set up texturing
glEnable(GL_TEXTURE_2D)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

# load texture
surf = pygame.image.load('Player1.png')
s = pygame.image.tostring(surf, 'RGBA')
texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surf.get_width(), surf.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, s)
glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)

# create pygame clock
MAINCLOCK = pygame.time.Clock()

# init screen
pygame.display.init()

while True:
    # get quit event
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # prepare to render screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    # Enable stencil test
    glEnable(GL_STENCIL_TEST)
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE)
    glStencilFunc(GL_ALWAYS, 2, ~0)
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE)

    # draw rectangle
    glDisable(GL_TEXTURE_2D)
    glColor3fv((0, 0, 0))
    glRectf(-1, 1, 0, 0.5)

    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
    glStencilFunc(GL_EQUAL, 2, ~0)
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)

    # draw texture
    glEnable(GL_TEXTURE_2D)
    glColor3fv((1, 1, 1))
    glBindTexture(GL_TEXTURE_2D, texID)
    glBegin(GL_QUADS)
    glTexCoord2f(0, 0); glVertex2f(-1, -1)
    glTexCoord2f(0, 1); glVertex2f(-1, 1)
    glTexCoord2f(1, 1); glVertex2f(1, 1)
    glTexCoord2f(1, 0); glVertex2f(1, -1)
    glEnd()

    # disable stencil test
    glDisable(GL_STENCIL_TEST)

    pygame.display.flip()
    MAINCLOCK.tick(60)

What am I missing?

Thanks in advance


Solution

  • The stencil test is proper implemented, but you forgot to setup the size of the stencil buffer when you initialize the PyGame OpenGL window. In your case the stencil test does not work, because there is no stencil buffer.

    The stecil buffer can be set up by setting the GL_STENCIL_SIZE attribute with the method pygame.display.gl_set_attribute

    Add the following to your code:

    pygame.display.init()
    pygame.display.gl_set_attribute(GL_STENCIL_SIZE, 8)
    pygame.display.set_mode((1000, 500), OPENGL | DOUBLEBUF)