[SOLVED] Stencil buffer help

Hey guys, I cant seem to get the stencil buffer working properly.
Here is the code.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chapter3;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class Stencil {
    // Square position and size variables
    private float x1;
    private float y1;
    private float rsize;
    // Step size in x and y directions
    // (number of pixels to move each time)
    private float xstep;
    private float ystep;
    
    // Clipping area variables
    private float clipWidth;
    private float clipHeight;
    
    public Stencil() {
        // Initialize variables
        x1 = 0.0f;
        y1 = 0.0f;
        rsize = 25;
        xstep = 1.0f;
        ystep = 1.0f;
        
        // Create window
        try {
            Display.setDisplayMode(new DisplayMode(800, 600));
            Display.setTitle("Stencil Animation");
            Display.create();
            Display.setResizable(true);
            Display.setVSyncEnabled(true);
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
    }
    
    /*
     * Called to run the program
     */
    public void run() {
        while (!Display.isCloseRequested()) {
            setupRC();
            changeSize(Display.getWidth(), Display.getHeight());
            animate(Display.getWidth(), Display.getHeight());
            renderScene();
            
            // Update display
            Display.update();
           // Display.sync(60);
        }
        
        cleanUp();
    }
    
    /*
     * Called to draw scene
     */
    public void renderScene() {
        double dRadius = 0.1;   // Initial radius of spiral
        double dAngle;
        
        // Use 0 for clear stencil, enable stencil test
        glClearStencil(0);
        glEnable(GL_STENCIL_TEST);
        
        // Clear color and stencil buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        
        // All drawing commands fail the stencil test, and are not 
        // drawn, but increment the value in the stencil buffer
        glStencilFunc(GL_NEVER, 0x0, 0x0);
        glStencilOp(GL_INCR, GL_INCR, GL_INCR);
        
        // Spiral pattern will create stencil pattern
        // Draw the spiral pattern with white lines. We
        // make the lines white to demonstrate that the
        // stencil function prevents them from being drawn
        glColor3f(1.0f, 1.0f, 1.0f);
        glBegin(GL_LINE_STRIP);
            for (dAngle = 0; dAngle < 400; dAngle += 0.1) {
                glVertex2d(dRadius * Math.cos(dAngle), dRadius * Math.sin(dAngle));
                dRadius *= 1.002;
            }
        glEnd();
        
        // Now, allow drawing, except where the stencil pattern is 0x1
        // and do not make any further changes to the stencil buffer
        glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        
        // Now draw red bouncing square
        // (x and y) are modified by a timer function
        glColor3f(1.0f, 0.0f, 0.0f);
        glRectf(x1, y1, x1 + rsize, y1 - rsize);
    }
    
    /* 
     * Set up the rendering state
     */
    public void setupRC() {
        // Set clear color to blue
        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    }
    
    /*
     * Set up method to adjust window size
     */
    public void changeSize(int w, int h) {
        float nRange = 100.0f;
        
        // Prevent a divide by zero
        if (h == 0) {
            h = 1;
        }
        
        // Set Viewport to window dimensions
        glViewport(0, 0, w, h);
        
        // Reset coordinate system
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        // Establish clipping volume (left, right, bottom, top, near, far)
        if (w <= h) {
            glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w,
                    -nRange, nRange);
            clipWidth = nRange;
            clipHeight = nRange * h / w;
        } else {
            glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange,
                    -nRange, nRange);
            clipWidth = nRange * w / h;
            clipHeight = nRange;
        }
        
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    /*
     * Check window size and adjust x and y values 
     * accordingly, animate square
     */
    public void animate(float windowWidth, float windowHeight) {  
        // Reverse direction when you reach left or right edge
        // of the screen
        if (x1 > clipWidth - rsize || x1 < -clipWidth) {
            xstep = -xstep;
        }
        
        // Reverse direction when you reach top or bottom edge
        if (y1 > clipHeight || y1 < -clipHeight + rsize) {
            ystep = -ystep;
        }
        
        animate();
        
        // Check bounds. This is in case the window is made
        // smaller while the rectangle is bouncing and the
        // rectangle suddenly finds itself outside the new
        // clipping volume
        if (x1 > (windowWidth - rsize + xstep)) {
            x1 = windowWidth - rsize - 1;
        } else if (x1 < -(windowWidth + xstep)) {
            x1 = -windowWidth - 1;
        }
        
        if (y1 > (windowHeight + ystep)) {
            y1 = windowHeight - 1;
        } else if (y1 < -(windowHeight - rsize + ystep)) {
            y1 = -windowHeight + rsize - 1;
        }
    }
    
    /*
     * Move the square around the screen
     */
    public void animate() {
        x1 += xstep;
        y1 += ystep;
    }
    
    /*
     * Clean up method used to destroy unused objects
     */
    public void cleanUp() {
        Display.destroy();
    }
    
    public static void main(String[] args) {
        new Stencil().run();
    }
}

Im learning OpenGL through the OpenGL Superbible book. This is an example of using stencil buffer but it doesnt work for me? What am i missing?

Ah this is from the oooolllldddd RedBook! I ported the code as well here.

Your problem is that you forgot to enable the stencil buffer. By default, LWJGL doesn’t initialize a stencil buffer, so you have to manually create one. Look at the arguments for the PixelFormat in my main method. Each value is bpp (bits per pixel) for alpha, depth, and stencil respectively. I put 0 for alpha and depth because we don’t need them for this example and 1 bit-per-pixel for stencil. By default, I believe LWJGL only initializes a depth buffer with 8bpp, and no alpha nor stencil buffers.

Ya but how can I do this without extending GLProgram?
Edit** What is the OpenGL command?
EDIT*** Nevermind i figuered it out. Thanks alot dude.

Ah yeah I should have showed you where you stick the PixelFormat. You give it (and an empty ContextAttribs instance) to Display.create(PixelFormat, ContextAttribs) :stuck_out_tongue: