Stencil pattern does not work correctly

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-

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.

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-

I thought that was the point.

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

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

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-

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.

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:

So, do you mean that works ?

Yeah.It works very well. I’m moving around the world and the stencil patterns clip the walls correctly :slight_smile: