Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: Stencil pattern does not work correctly

  1. #1
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    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-
    <span style="color: #006600">-Ehsan-</span>

  2. #2
    Super Moderator OpenGL Guru dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    4,388

    Re: Stencil pattern does not work correctly

    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.

  3. #3
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: Stencil pattern does not work correctly

    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.
    -Ehsan-
    <span style="color: #006600">-Ehsan-</span>

  4. #4
    Super Moderator OpenGL Guru dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    4,388

    Re: Stencil pattern does not work correctly

    I thought that was the point.

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

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,749

    Re: Stencil pattern does not work correctly

    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

  6. #6
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: Stencil pattern does not work correctly

    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-
    <span style="color: #006600">-Ehsan-</span>

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,749

    Re: Stencil pattern does not work correctly

    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.

  8. #8
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: Stencil pattern does not work correctly

    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
    <span style="color: #006600">-Ehsan-</span>

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,749

    Re: Stencil pattern does not work correctly

    So, do you mean that works ?

  10. #10
    Advanced Member Frequent Contributor Ehsan Kamrani's Avatar
    Join Date
    May 2005
    Location
    Iran
    Posts
    547

    Re: Stencil pattern does not work correctly

    Yeah.It works very well. I'm moving around the world and the stencil patterns clip the walls correctly
    <span style="color: #006600">-Ehsan-</span>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •