Sign in to follow this  
Matthew Meeks

Stencil Buffer

Recommended Posts

I'm having issues understanding how stencil buffers work.  I used them a few times in XNA but there appears to be many differences.  The comparison test always seems to pass and the objects are always drawn.

 

Here are my depth states:

DepthStencilStateDescription dsStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = false,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            DepthState = DepthStencilState.FromDescription(Device, dsStateDesc);

            //===

            DepthStencilStateDescription dsaStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = true,
                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Greater,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Increment,
                    DepthFailOperation = StencilOperation.Zero
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Always,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Increment,
                    DepthFailOperation = StencilOperation.Zero
                },
                StencilWriteMask = 0,
                StencilReadMask = 1,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            addDepthState = DepthStencilState.FromDescription(Device, dsaStateDesc);

            //===

            DepthStencilStateDescription dscStateDesc = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                IsStencilEnabled = true,
                FrontFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Equal,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    Comparison = Comparison.Equal,
                    FailOperation = StencilOperation.Keep,
                    PassOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Keep
                },
                StencilReadMask = 1,
                StencilWriteMask = 1,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less
            };

            compareDepthState = DepthStencilState.FromDescription(Device, dscStateDesc);

 

When I draw:

  • Clear the depth/stencil buffer
  • set the first depth state
  • draw the world
  • set the second depth state
  • draw the cube to render the second view point on
  • set the third depth state
  • draw the world from the second view point

 

If this helps these are the XNA depth states I'm trying to mimic:

        DepthStencilState addIfPortal = new DepthStencilState()
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Always,
            StencilPass = StencilOperation.Increment

        };
        DepthStencilState checkPortal = new DepthStencilState()
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Equal,
            ReferenceStencil = 1,
            StencilPass = StencilOperation.Keep
        };

 

I appreciate any help and thanks for your patience smile.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this