Results 1 to 2 of 2

Thread: Stencil Buffer Shenanigans

  1. #1

    Stencil Buffer Shenanigans

    I'm working on a stencilling system for my latest project that works like this:

    A function call, BeginStencilDraw() will set all color masks to false and the stencil mask to true. As a result, all subsequent drawing commands will create stencilling areas. EndStencilDraw() is the inverse function that will return the color mask and disable the stencil mask.

    There is also a counter variable that is initially zero, that corresponds to the ref value for the stencil test. This is the clear value for the stencil buffer, so if the counter is at zero then the stencil test is disabled for performance. BeginStencilDraw() will enable the stencil test if the counter is at zero, because it also uses glStencilOp(GL_KEEP, GL_KEEP, GL_INCR) so that the pixels that should become part of the stencil area are appropriately promoted to the next stencil value. EndStencilDraw() sets glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP) and disables the test if the counter is at zero.

    The other functions are EnterStencil() which increments the counter, calls glStencilFunc(GL_EQUAL, ++Counter, 0xff) so that the stencil areas previously drawn are used. If the counter is zero on the way into the function, the stencil test is enabled first. Its companion function ExitStencil() willc all glStencilFunc(GL_EQUAL, --Counter, 0xff) and disable the stencil test if counter is zero. There is also rollover protection in that EnterStencil() will abort if the stencil counter is 255 (8-bit stencil buffer, more on this later) and ExitStencil() will abort if the stencil value is already 0.

    The idea is that this will help me nest stencil areas easily, as such:

    Code :
    The problem, however, is that the StencilOp is not being performed properly. If I attempt to draw another stencil area inside an area of stencil value one, instead of incrementing the stencil value will get zeroed. I have checked this by dumping images of the stencil buffer and examining them. It's not precisely zeroed as it is decremented. If I use a default nonzero stencil value and clear value, such as 4, the first area is correctly drawn with value 5, but when I EnterStencil and draw the next stencil area the second area gets a value of 4. I can provide more code upon request, but this problem has me frustrated.

    The platform is Win32 on an nVidia Geforce 3, but I have duplicated the problem on a friend's Geforce 2 GTS and another's ATI Radeon. My pixel format calls for:

    ColorBits 32
    DepthBits 16
    StencilBits 8

    At first I thought that since the stencil values went 0, 1, then back to 0 that I was only getting a 1 bit stencil buffer, but I examined my pixel format with DescribePixelFormat() and it surely says 8, and the test I did with initial values >1 dismissed that theory.

  2. #2

    Re: Stencil Buffer Shenanigans

    Also, if I leave the stencil test on the whole time, the stencil buffer gets completely messed up; it fills up with tons of artifacts and junk data. I can't figure this one out.

Similar Threads

  1. Replies: 1
    Last Post: 09-11-2013, 11:35 AM
  2. copy app generated stencil data to stencil buffer
    By guilder in forum OpenGL: Basic Coding
    Replies: 7
    Last Post: 12-23-2010, 06:46 PM
  3. Coping from FBO's stencil to main stencil buffer ?
    By gregory_x in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 08-30-2010, 01:09 AM
  4. Matrix Shenanigans
    By in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 06-01-2004, 09:44 PM
  5. How to clear stencil buffer after stencil test?
    By in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-14-2003, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean