Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Stencil buffer help  (Read 871 times)
0 Members and 1 Guest are viewing this topic.
Offline SaycoDevils

Senior Newbie





« Posted 2013-01-15 00:47:44 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
/*
 * 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?
Online ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2013-01-15 01:02:42 »

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.

Offline SaycoDevils

Senior Newbie





« Reply #2 - Posted 2013-01-15 01:08:30 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2013-01-15 01:39:24 »

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) Tongue

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!