Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  2D openGL and clipping window  (Read 3062 times)
0 Members and 1 Guest are viewing this topic.
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Posted 2008-05-29 15:48:40 »

I make a 2D game right now. I draw the background, sprites then the interface.
For the interface, i want to define a drawable zone for some animation. I did some search and it seems that i have to use stencil buffer (that i never used) :

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  
      // How i init openGL
     GL11.glViewport(0,0,800,600);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GLU.gluOrtho2D(0.0f, 800, 0.0f, 600);
      GL11.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix
     GL11.glLoadIdentity();
     
      GL11.glDisable(GL11.GL_LIGHTING);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA,GL11.GL_ONE_MINUS_SRC_ALPHA);
     
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);


       ...

      // Where i want to do the clipping window

      Sprite sp1 = sprites[items[selected]];
      Sprite sp2 = sprites[items[next]];
                 
      GL11.glClearStencil(0);
      GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);
      GL11.glEnable(GL11.GL_STENCIL_TEST);
      GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
      GL11.glStencilOp(GL11.GL_REPLACE,GL11.GL_REPLACE,GL11.GL_REPLACE);
      GL11.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
      GL11.glDisable(GL11.GL_TEXTURE_2D);
      GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(800-48, 600-48);
        GL11.glVertex2f(800-16, 600-48);
        GL11.glVertex2f(800-16, 600-16);
        GL11.glVertex2f(800-48, 600-16);
      GL11.glEnd();
     
      GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1);
      GL11.glStencilOp(GL11.GL_KEEP,GL11.GL_KEEP,GL11.GL_KEEP);
     
      GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
     
      if(sp1 != null)
      {
        sp1.draw(800-48-dir*compt*2,600-48,false,false);
      }
     
      if(sp2 != null)
      {
        sp2.draw(800-48-dir*compt*2+dir*32,600-48,false,false);
      }
     
      GL11.glDisable(GL11.GL_STENCIL_TEST);


There is a red scare where i want to setup my cliping window. Sprites show inside and outside the cliping window.
It must be a stupid mistake...
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #1 - Posted 2008-05-29 17:22:33 »

You need to turn off colour writing when you draw your red square to set your stencil region. IIRC it's glColorMask(false, false, false, false).

Alternatively, if you only want rectangular clipping regions then glScissor is much easier to use.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #2 - Posted 2008-05-30 09:13:44 »

Thanks for the reply.

I was looking for the glScissor function  Grin. With it, it work.
But i didn't manage to make it work with stencil  Cry

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  
      Sprite sp1 = sprites[items[selected]];
      Sprite sp2 = sprites[items[next]];
     
      GL11.glEnable(GL11.GL_STENCIL_TEST);
      GL11.glClearStencil(0);
      GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);
      GL11.glStencilFunc(GL11.GL_ALWAYS, 1, 1);
      GL11.glStencilOp(GL11.GL_KEEP,GL11.GL_KEEP,GL11.GL_REPLACE);
      GL11.glColorMask(false,false,false,false);
      GL11.glDisable(GL11.GL_TEXTURE_2D);
      GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2f(800-48, 600-48);
        GL11.glVertex2f(800-16, 600-48);
        GL11.glVertex2f(800-16, 600-16);
        GL11.glVertex2f(800-48, 600-16);
      GL11.glEnd();
     
      GL11.glStencilFunc(GL11.GL_EQUAL, 1, 1);
      GL11.glStencilOp(GL11.GL_KEEP,GL11.GL_KEEP,GL11.GL_KEEP);
      GL11.glColorMask(true,true,true,true);
      GL11.glEnable(GL11.GL_TEXTURE_2D);
     
    /*  GL11.glEnable(GL11.GL_SCISSOR_TEST);
      GL11.glScissor(800-48, 600-48, 32, 32);*/

     
      if(sp1 != null)
      {
        sp1.draw(800-48-dir*compt*2,600-48,false,false);
      }
     
      if(sp2 != null)
      {
        sp2.draw(800-48-dir*compt*2+dir*32,600-48,false,false);
      }
     
      //GL11.glDisable(GL11.GL_SCISSOR_TEST);
     
      GL11.glDisable(GL11.GL_STENCIL_TEST);


Is stencil buffer on all graphic card ? I try to look for something about stencil in opengl capabilities but I didn't find it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #3 - Posted 2008-05-30 11:00:13 »

You also need to specify that you want a stencil buffer when creating your display, but I forget how to do that with Jogl. Try glGet(GL_STENCIL_BITS) and make sure it's >0.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #4 - Posted 2008-05-30 11:29:00 »

Yes !!!  Grin

1  
2  
3  
4  
5  
6  
7  
8  
9  
      PixelFormat pixelFormat = new PixelFormat();

      int depth = pixelFormat.getDepthBits();
      int alpha = pixelFormat.getAlphaBits();
      int stencil = 1;
     
      pixelFormat = new PixelFormat(alpha,depth,stencil);

      Display.create(pixelFormat);


It create a display with a stencil buffer and it works fine.

Thanks  Wink
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.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!