2
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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)

\$\endgroup\$
7
  • \$\begingroup\$ 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? \$\endgroup\$
    – chiarfe
    Sep 23, 2012 at 18:11
  • \$\begingroup\$ Can you maybe upload a small video? Hard to guess what you're seeying. Can you post more relevant code? \$\endgroup\$
    – Roy T.
    Sep 23, 2012 at 19:22
  • 1
    \$\begingroup\$ here is the video link, i've disabled every 2d content in my code, but this is what happens \$\endgroup\$
    – chiarfe
    Sep 24, 2012 at 14:57
  • \$\begingroup\$ Hmm I wouldn't know what's happening there. Are you sure the device states are correct? We'd need more code. \$\endgroup\$
    – Roy T.
    Sep 25, 2012 at 7:44
  • \$\begingroup\$ 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! \$\endgroup\$
    – chiarfe
    Sep 25, 2012 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .