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.

Is there any way to enable a temporary clipping mask in XNA while using a SpriteBatch? I'm drawing a bunch of Texture2D objects and Sprites in a given rectangle, and I want to make sure that said objects aren't drawn outside of that rectangle. If it's of any help, I'm already drawing my content within a Viewport, I'm not sure if I can draw, for instance, in another Viewport within that Viewport.

Illustrative image of what I'm trying to accomplish: http://i955.photobucket.com/albums/ae34/ThatsGobbles/Untitled-1.png

share|improve this question
add comment

2 Answers 2

up vote 8 down vote accepted

The scissor rect is a bit easier to use than the stencil buffer. You need to set ScissorRect to your Rectangle and set ScissorTestEnable to true.

        spriteBatch.GraphicsDevice.RenderState.ScissorTestEnable = true;
        spriteBatch.GraphicsDevice.ScissorRectangle = clipRect;
        spriteBatch.Begin();
share|improve this answer
    
Thanks a ton! I wasn't able to get it working at first, but then I realized it wasn't XNA 4.0 compatible. I modified it and it worked awesomely. –  ThatsGobbles Oct 16 '10 at 4:35
add comment

You need to use a Stencil Buffer. This is specified by passing a DepthStencilState into your SpriteBatch.Begin method call.

For example, to remove an object from a scene, fill a stencil buffer with a cut out pattern (using zeros) for each pixel where the object is visible. This is done by setting the reference stencil value to 0, clearing the stencil buffer, and rendering the object. Then set the reference stencil value to 1, set the compare function to CompareFunction.LessEqual, and render again. The stencil data masks those pixels whose value is non zero but less than 1, resulting in drawing over (or removing) the object.

share|improve this answer
add comment

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.