PDA

View Full Version : Stencil pattern does not work correctly



Ehsan Kamrani
06-06-2005, 06:39 AM
I want to clip part of a wall with the stencil buffer.I can rotate and translate the scene with the arrow keys. But the problem is that the stencil pattern is moved when i move the camera--It seems that this pattern has been attached to the viewr!Here's the code:
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT );
glLoadIdentity();
glRotatef( 360.0f - yrot, 0.0f, 1.0f, 0.0f );
glTranslatef( -xpos, -ypos , -zpos );
glDepthMask( GL_FALSE );
glStencilFunc( GL_NEVER, 1, 1 );
glStencilOp( GL_REPLACE, GL_KEEP, GL_KEEP );
glBegin( GL_QUADS );
glVertex3f( -0.5, -0.5,-2.0 );
glVertex3f( 0.5, -0.5,-2.0 );
glVertex3f( 0.5, 0.5,-2.0 );
glVertex3f( -0.5, 0.5,-2.0 );
glEnd();

glDepthMask( GL_TRUE );
glStencilMask( GL_FALSE );
glStencilFunc( GL_EQUAL, 1, 1 );
glColor3f( 1.0f, 0.0f, 0.0f );
glBegin( GL_QUADS );
glVertex3f( -1.0, -1.0,-2.0 );
glVertex3f( 1.0, -1.0,-2.0 );
glVertex3f( 1.0, 1.0,-2.0 );
glVertex3f( -1.0, 1.0,-2.0 );
glEnd();

I need a *fixed* pattern.So what should i do?
-Ehsan-

dorbie
06-06-2005, 06:48 AM
Don't do your rotate & translate before drawing to the stencil buffer of course. Just loadidentity and draw in eye space. Optionally use an ortho projection too.

Ehsan Kamrani
06-06-2005, 07:34 AM
I changed the code
glLoadIdentity();
glDepthMask( GL_FALSE );
glStencilFunc( GL_NEVER, 1, 1 );
glStencilOp( GL_REPLACE, GL_KEEP, GL_KEEP );
glBegin( GL_QUADS );
glVertex3f( -0.5, -0.5,-2.0 );
glVertex3f( 0.5, -0.5,-2.0 );
glVertex3f( 0.5, 0.5,-2.0 );
glVertex3f( -0.5, 0.5,-2.0 );
glEnd();

glRotatef( 360.0f - yrot, 0.0f, 1.0f, 0.0f );
glTranslatef( -xpos, -ypos , -zpos );

glDepthMask( GL_TRUE );
...

But the rusult is like the previous code.The stencil pattern is in the middle of my window-- like writting the pixels with the glDrawPixel(). It seems that this pattern is independent of the transformations. :confused:
-Ehsan-

dorbie
06-06-2005, 07:56 AM
I thought that was the point.

I don't know what you're asking. Describe EXACTLY what you want the stencil pattern to do.

jide
06-06-2005, 07:56 AM
You draw in the stencil just like you draw every other things. But the result isn't 3D, only 2D, as a mask, and after the Zbuffer test. So you draw what you could have seen in your window.

hope that helps

Ehsan Kamrani
06-06-2005, 08:09 AM
I want to make a pattern and clip part of the wall.As an example i want to create a window in this wall.i can use from the depth buffer to do this; but i think that we should also be able to do this with the stencil buffer.
I guess that the stencil pattern is in eye space.When i make a stencil pattern, and then draw the wall, it clips part of the wall. but when i rotate the camera-actually the scene-, the stencil pattern clips *another* part of the wall.The result is like this: the viewer has a pattern on his eyes. he rotates around the wall; so this pattern will clip the different parts of the wall. I want to find a way to create a window in my wall with the stencil buffer.
-Ehsan-

jide
06-06-2005, 09:40 AM
Your logic seems well.

You can draw a smaller quad on your wall, representing the window, in the stencil buffer. Both (the wall and the window: 2 quads) will be coplanar planes and have the same depth value. Also, check your stencil functions so that it rejects all elements from the same z value.

Do remember that the stencil operation happens after depth test. So yes, stencil happens at eye coordinates.

Hope that helps.

Ehsan Kamrani
06-06-2005, 03:17 PM
Thank you. I created that house and used from the stencil buffer to create the windows. after almost 20 hours efforts, now i can go to bed :rolleyes:

jide
06-07-2005, 12:41 AM
So, do you mean that works ?

Ehsan Kamrani
06-07-2005, 01:10 AM
Yeah.It works very well. I'm moving around the world and the stencil patterns clip the walls correctly :)