XNA 4.0 Rendering Issues

I’ve spent the whole day working on rendering issues for my game. This image here sums up nicely what I’ve been dealing with:

You’ll notice that that the wing texture has some black fringing around it. It also has the puzzle in the background drawn over it. Strangely enough, I didn’t have these issues in XNA 3.1 because I had correctly manipulated various RenderState properties and enjoyed its magic without fully understanding what it was doing. With XNA 4.0, the RenderState class was replaced with three new state objects, the BlendState, DepthStencilState and RasterizeState. The SamplerState class was also replaced with a SamplerState object but with different behaviour.

The above rendering issues are now fixed. Thanks to XNA 4.0, I now have a stronger understanding of the built-in state objects and how they affect rendering. The fix only required a few lines of code, but it took me a lot of trial and error, research and digging around before figuring it all out. Before rendering my 3D components (which have textures with alpha) I would set the following:

GraphicsDevice.BlendState = BlendState.AlphaBlend;
// Render 3D components
GraphicsDevice.BlendState = BlendState.Opaque;

What I had assumed for the longest time was that the DepthStencilState, RasterizeState and SamplerState would all be in their default states because I had not explicitly changed them and if I had, I would reset them back afterwards. In my game, I also draw 2D objects using SpriteBatch, and what I didn’t realize until I read this article was that SpriteBatch was changing several device states that would mess up the way my 3D objects were being drawn. Always assume nothing! Silly silly me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s