0
votes

I'm trying to draw a depth map of a scene. Now, I made an HLSL file like the one in: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Shadow_map.php And everything went just fine. However! I want to find out the exact value of each pixel, so instead of drawing on the normal render target, I created a new render target which is a Texture2D as in: http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Render_to_texture.php and then I drew it using a spritebatch.

Then! when I display the images, the first, that is, the one drawing directly to the screen gives this output:

Correct Depth Image

and the one that draws to a texture then to the screen using a sprite gives this: Sprite drawn Image

Notice how in the second capture, the right arm of the body, which is obviously behind the body, is drawn as if the body doesn't exist in the overlapping area.

I think, its an ordering problem, as the vertices in the front are drawn first to the texture then the more distant ones overwriting the closer ones resulting in the observed image.

I would appreciate any input.

1

1 Answers

2
votes

SpriteBatch modifies the graphics device state when you use it. From the linked article, here is what it sets:

GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = DepthStencilState.None;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

You probably want to set the depth-stencil state back to default (depth read and write).

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

You may also have to set some other states, depending on what you need for your rendering.