XNA, alpha testing, stencil buffer and two different projections

221 views Asked by At

My question is about using the stencil buffer from another projection as a mask in another projection. I'm not sure how to approach this, so I'll go ahead and lay down some code:

spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, AlwaysStencilState, null, alphaEffect2, Camera.Projection);

// Draw stuff used as a stencil in Camera.Projection space

spriteBatch.End();

spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, EqualStencilState, null, null);

// Draw a background behind

spriteBatch.End();

Alright, so this works if I take the Camera.Projection out of the first equation and render to the screen coordinate space. However, this does NOT work with the Camera projection. My understanding is that the rendering is simply done to the stencil buffer "far far away" compared to the latter draw calls which take place in screen space.

How can I use the stencil of the first operation as a mask for the second one?

1

There are 1 answers

0
Muhwu On

Alrighty. I found out that I could in fact just use the original effect's projection (0,0,width,height) and set the effect's World matrix to correspond the camera's projection matrix and everything started magically working.