Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

The problem... i'm drawing polygons, in this case boxes, and i want clip children polygons with its parent's client area.

enter image description here

    // Class Region
    public void Render(GraphicsDevice Device, Camera Camera)
    {
        int StencilLevel = 0;
        Device.Clear( ClearOptions.Stencil, Vector4.Zero, 0, StencilLevel );
        Render( Device, Camera, StencilLevel );
    }

    private void Render(GraphicsDevice Device, Camera Camera, int StencilLevel)
    {
        Device.SamplerStates[0] = this.SamplerState;
        Device.Textures[0] = this.Texture;
        Device.RasterizerState = RasterizerState.CullNone;
        Device.BlendState = BlendState.AlphaBlend;
        Device.DepthStencilState = DepthStencilState.Default;

        Effect.Prepare(this, Camera );

        Device.DepthStencilState = GlobalContext.GraphicsStates.IncMask;
        Device.ReferenceStencil = StencilLevel;

        foreach ( EffectPass pass in Effect.Techniques[Technique].Passes )
        {
            pass.Apply( );
            Device.DrawUserIndexedPrimitives<VertexPositionColorTexture>( PrimitiveType.TriangleList, VertexData, 0, VertexData.Length, IndexData, 0, PrimitiveCount );
        }

        foreach ( Region child in ChildrenRegions )
        {
            child.Render( Device, Camera, StencilLevel + 1 );
        }

        Effect.Prepare( this, Camera );

        // This does not works
        Device.BlendState = GlobalContext.GraphicsStates.NoWriteColor;
        Device.DepthStencilState = GlobalContext.GraphicsStates.DecMask;
        Device.ReferenceStencil = StencilLevel; // This should be +1, but in that case the last drrawed is blue and overlap all

        foreach ( EffectPass pass in Effect.Techniques[Technique].Passes )
        {
            pass.Apply( );
            Device.DrawUserIndexedPrimitives<VertexPositionColorTexture>( PrimitiveType.TriangleList, VertexData, 0, VertexData.Length, IndexData, 0, PrimitiveCount );
        }
    }



    public static class GraphicsStates
    {
        public static BlendState NoWriteColor = new BlendState( )
        {
             ColorSourceBlend = Blend.One,
             AlphaSourceBlend = Blend.One,
             ColorDestinationBlend = Blend.InverseSourceAlpha,
             AlphaDestinationBlend = Blend.InverseSourceAlpha,
             ColorWriteChannels1 = ColorWriteChannels.None
        };

        public static  DepthStencilState IncMask = new DepthStencilState( )
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Equal,
            StencilPass = StencilOperation.IncrementSaturation,
        };

        public static  DepthStencilState DecMask = new DepthStencilState( )
        {
            StencilEnable = true,
            StencilFunction = CompareFunction.Equal,
            StencilPass = StencilOperation.DecrementSaturation,
        };
    }

How can achieve this?

EDIT: I've just relized that the NoWriteColors.ColorWriteChannels1 should be NoWriteColors.ColorWriteChannels. :)

Now it's clipping right.

Any other approach?

share|improve this question

1 Answer 1

I think you are complicating things too much, and the best approach would be to draw from children to parents and use standard depth-testing to clip the polygons.

share|improve this answer
    
i will think about that –  Blau Oct 12 '11 at 20:33
    
I don't see how it's done that way, ... what happens if child are bigger than any ancestor? it won't be clipped. –  Blau Oct 12 '11 at 20:39
    
They wouldn't be clipped by stencil, anyway. –  r2d2rigo Oct 12 '11 at 21:13
    
youtube.com/watch?v=-xUMC7sPAx0 yes, they are –  Blau Oct 12 '11 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.