Search code examples
openglpyopengl

(py)OpenGL: stencil buffer not working


The stencil buffer is not working at all. It is as if it is not turned on. With the below code, I expect to see nothing on the screen as my test is GL_NEVER which will always fail. But the rectangle reliably show up on the screen.

glEnable(GL_STENCIL_TEST);
glStencilMask(0xff)
glClear(GL_STENCIL_BUFFER_BIT)

glStencilFunc(GL_NEVER, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

glBegin(GL_QUADS)
glVertex3fv(...); glVertex3fv(...)
glVertex3fv(...); glVertex3fv(...)
glEnd()    

Another thing worth noting is that I am using pygame+ pyOpenGL. pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

I saw references with GLUT saying that a stencil buffer needs to be allocated on initialization, but I am not sure how to with pygame.


Solution

  • This took me some time to figure out. So I am going to put it here in case someone else runs into this too.

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