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();
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.
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.
-Ehsan-
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.
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-
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.