Setting up the Java stencil buffer

In OpenGL, the stencil buffer is used to mask pixels on the screen. However, if you notice that your stencil test is always passing (i.e. your stencil bits are always returns as zero), then you probably haven’t initialized the stencil bits.

This isn’t very well documented. It took a few days of searching to find it. In C, one would be using GLUT, so calling glutInitDisplayMode() would be he function to call that requests stencil bits. In Java, this is in the GLCapabilities object.

So, to request stencil bits, in your init() function put


GLCapabilities cap = new GLCapabilities();
cap.setStencilBits(8);
GLCanvas canvas = new GLCanvas(cap);


Basically, this initializes a new GLCapabilities object, and assigns it an 8-bit stencil buffer. Pass it into the GLCanvas as a parameter and you’re gold. 

One use for this buffer is to render shadow volumes:

 

Notes

  1. publicboolean posted this