• Advertisement
Sign in to follow this  

Stencil Buffer

This topic is 1902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Sign in to follow this  

  • Advertisement