Antony
Antony

Reputation: 199

FBX Model not displaying properly in XNA 4.0

I have a problem that XNA 4.0 is not displaying a 3D FBX model correctly.

A friend created a model and when I open this in FBX Viewer it displays it correctly https://docs.google.com/open?id=0B54ow8GRluDUYTBubTQ4bjBramM

But when I load it into XNA and click run, it displays as https://docs.google.com/open?id=0B54ow8GRluDUSE14TWMxcnBJWWc

The code that i have for the drawing is
foreach (ModelMesh mesh in model.Meshes)
{
    foreach (BasicEffect effect in mesh.Effects)
    {
         effect.Projection = projection;
         effect.View = view;
         effect.World = Matrix.CreateScale(1.0f) * Matrix.CreateRotationX(90) * Matrix.CreateTranslation(position);
         effect.EnableDefaultLighting();
     }
     mesh.Draw();
 }

Any help to fix this will be very much appreciated.

Thanks.

Upvotes: 1

Views: 982

Answers (1)

Blau
Blau

Reputation: 5762

Each mesh has a bone... and you should use it to position the mesh in the right place... this code is from Microsoft

private void DrawModel(Model m)
{
    Matrix[] transforms = new Matrix[m.Bones.Count];
    float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
    m.CopyAbsoluteBoneTransformsTo(transforms);
    Matrix projection =
        Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
        aspectRatio, 1.0f, 10000.0f);
    Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom),
        Vector3.Zero, Vector3.Up);

    foreach (ModelMesh mesh in m.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.EnableDefaultLighting();

            effect.View = view;
            effect.Projection = projection;
            effect.World = gameWorldRotation *
                transforms[mesh.ParentBone.Index] *
                Matrix.CreateTranslation(Position);
        }
        mesh.Draw();
    }
}

You can found it at http://msdn.microsoft.com/en-us/library/bb203933(v=xnagamestudio.40).aspx

Upvotes: 2

Related Questions