0
\$\begingroup\$

My Problem is similar to XNA 4.0 - Why does using a RenderTarget2d cause transparency on models?.

I want to render my scene to a Texture2D and then draw the texture. If I set no RenderTarget at all (render directly to the back buffer), everything looks fine.

I draw my models/textures with:

graphics.GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.AnisotropicClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);

[...Drawing methods for models etc. are called here...]

spriteBatch.End();

If I change that code to:

RenderTarget2D target = new RenderTarget2D(this.GraphicsDevice,this.GraphicsDevice.PresentationParameters.BackBufferWidth,this.GraphicsDevice.PresentationParameters.BackBufferHeight);
this.GraphicsDevice.SetRenderTarget(target);

graphics.GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.AnisotropicClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise);

[...Drawing methods for models etc. are called here...]

spriteBatch.End();
this.GraphicsDevice.SetRenderTarget(null);

And render the texture / save it as an image-file, some objects get transparent and the drawing order changes sort of randomly.

\$\endgroup\$
1
\$\begingroup\$

The docs do not state what kind of depth buffer and color buffer will be used using that overload. I suspect not explicitly specifiying a depth buffer is the issue here.

Try the most complete constructor overload like this, and probably your problems will disappear:

new RenderTarget2D (
         this.GraphicsDevice graphicsDevice,
         this.GraphicsDevice.PresentationParameters.BackBufferWidth,
         this.GraphicsDevice.PresentationParameters.BackBufferHeight,
         true,
         SurfaceFormat.Color,
         DepthFormat.Depth16,
         4,
         RenderTargetUsage.DiscardContents
);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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