# Xna model parts are overlying others

I am trying to import in XNA an .fbx model exported with blender.

Here is my drawing code

public void Draw()
{
Matrix[] modelTransforms = new Matrix[Model.Bones.Count];
Model.CopyAbsoluteBoneTransformsTo(modelTransforms);

foreach (ModelMesh mesh in Model.Meshes)
{
foreach (BasicEffect be in mesh.Effects)
{
be.EnableDefaultLighting();
be.World = modelTransforms[mesh.ParentBone.Index] * GameCamera.World * Translation;
be.View = GameCamera.View;
be.Projection = GameCamera.Projection;
}
mesh.Draw();
}
}


The problem is that when I start the game some model parts are overlying others instead of being behind. I've tried to download other models from internet but they have the same problem.

Your device states are probably wrong. This often happens when mixing 2D and 3D (for example the overload for SpriteBatch.Begin() which takes no arguments sets some device states that are incompatible with 3D rendering. No worries though, all you have to do is to make sure that the following device states are set the way you want them:

BlendState, SamplerState, DepthStencilState, RasterizerState


The default values, which should be fine for just rendering a model are:

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;


Especially take a look at DepthStencilState it should be set to DepthStencilState.Default and at the moment is probably set to DepthStencilState.None.

To learn more about graphics device states take a look at this series of "What is" articles on MSDN. To learn more about why this is managed in such a way you should read this article by Shawn Hargreaves (used to be on the XNA team and his blog posts are all brilliant)

• thanks for your answer, seems something changed now. Model parts are now flickering every time I move, instead of being the same like it was. Any idea about it? – Federico Chiaravalli Sep 23 '12 at 18:11
• Can you maybe upload a small video? Hard to guess what you're seeying. Can you post more relevant code? – Roy T. Sep 23 '12 at 19:22
• here is the video link, i've disabled every 2d content in my code, but this is what happens – Federico Chiaravalli Sep 24 '12 at 14:57
• Hmm I wouldn't know what's happening there. Are you sure the device states are correct? We'd need more code. – Roy T. Sep 25 '12 at 7:44
• At last, when I was copying the code to show it to you, I find the problem, it was that I wrote Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 0.000001f, 1000); for the projection matrix but seems that 0.000001f wasn't a good idea. I changed it with 1f and now it works. Have you any idea of why 0.000001f was wrong? Anyway thank you for your help and for the two articles you shown me, they were very interesting! – Federico Chiaravalli Sep 25 '12 at 13:27