XNA 4.0.4 use Z-buffer in rendertarget

1.3k views Asked by At

I try to draw my 3d world in a rendertarget2d. If I use the default rendertarget, the world looks perfect... but if i render to a rendertarget2d the z buffer does not work and object in the background will be drawed in front of others.

is rendertarget2d the right class? and what does I do wrong?

public class Camera
{
    public Matrix View { get; set; }
    public Matrix Projection { get; set; }
    public RenderTarget2D RenderTarget { get; set; }
    public Vector2 Screen { get; set; }
    public Effect ZBufferEffect { get; set; }
    private GraphicsDevice device;


    public void Draw(SpriteBatch spriteBatch, Player player, SpriteFont font)
    {
        device.SetRenderTarget(RenderTarget);

        device.Clear(Color.Black);

        device.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };

        // Draw the scene



        View = Matrix.Invert(Object.OBJECTS[0].PosRotSca);
        View *= Matrix.CreateTranslation(0, -5, -5);
        View *= Matrix.CreateRotationX(MathHelper.ToRadians(20));
        foreach(Object obj in Object.OBJECTS)
        {               
            foreach(ModelMesh mesh in Game1.modelArray[obj.Model].Meshes)
            {
                //foreach (ModelMeshPart part in mesh.MeshParts)
                //{
                //    part.Effect = ZBufferEffect;
                //    ZBufferEffect.Parameters["World"].SetValue(obj.PosRotSca * mesh.ParentBone.Transform);
                //    ZBufferEffect.Parameters["View"].SetValue(View);
                //    ZBufferEffect.Parameters["Projection"].SetValue(Projection);
                //    ZBufferEffect.Parameters["ModelTexture"].SetValue(Game1.textureArray[obj.Texture]);
                //    ZBufferEffect.Parameters["invert"].SetValue(false);
                //}
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.Texture = Game1.textureArray[obj.Texture];
                    effect.TextureEnabled = true;
                    effect.World = obj.PosRotSca;
                    effect.View = View;
                    effect.Projection = Projection;
                }
                mesh.Draw();
            }
        }
        device.SetRenderTarget(null);

        spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
            SamplerState.LinearClamp, DepthStencilState.Default,
            RasterizerState.CullNone);

        spriteBatch.Draw(RenderTarget, new Rectangle(0, 0, device.Viewport.Width, device.Viewport.Height), Color.White);

        spriteBatch.End();
    }


    public Camera(GraphicsDevice device)
    {

        RenderTarget2D rt = new RenderTarget2D(
            device,
            device.PresentationParameters.BackBufferWidth,
            device.PresentationParameters.BackBufferHeight,
            false,
            device.PresentationParameters.BackBufferFormat,
            DepthFormat.Depth24);

        RenderTarget = rt;


        this.device = device;

    }        

}

can I change some settings in the rendertarget? or does I have to write a Z-buffer shader? Thank you for your help!

1

There are 1 answers

2
Tigran Gasparian On BEST ANSWER

Since I have no XNA installed on my machine right now, I can only speculate based on previous experience.

I see two possible places where things might go wrong.

First of all, the SpriteBatch changes a lot of render states that need to be changed back for 3D rendering. Take a look here for an overview of all the states: http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

Secondly, but I don't think this is the cause of your problem, try clearing the render target with a different clear overload.

GraphicsDevice.Clear has the following overload where you can specify the depth value to clear to:

public void Clear (
         ClearOptions options,
         Color color,
         float depth,
         int stencil
)

Read the docs here: http://msdn.microsoft.com/en-us/library/ff433734(v=xnagamestudio.40).aspx