Tada
Tada

Reputation: 1655

Can anyone spot why my XNA model doesn't render correctly?

My last question was put on hold because I simply tried to provide all of my source code (which actually is a pretty small example as is). My problem is I am able to draw a quad and position my camera and face it towards {0,0,0}. For whatever reason, when I draw my Model which should be a soldier standing in the very center, I don't see it. While debugging I see the model is loaded correctly, has its bones/meshes etc so I am just unsure of why I don't see it.

My camera is setup as such:

public class CameraManager : GameComponent
{

    private GraphicsDeviceManager fGraphicsDeviceManager;
    public Matrix ViewMatrix { get; private set; }
    public Matrix ProjectionMatrix { get; private set; }

    public Vector3 CameraPosition { get; set; }
    public Vector3 CameraTarget { get; set; }

    public float AspectRatio { get; set; }
    public float NearClip { get; set; }
    public float FarClip { get; set; }
    public float ViewAngle { get; set; }


    public CameraManager(Game pGame, GraphicsDeviceManager pGraphicsDeviceManager) : base(pGame)
    {
        fGraphicsDeviceManager = pGraphicsDeviceManager;

        CameraPosition = new Vector3(500, 300, 100);
        CameraTarget = Vector3.Zero;

        Viewport vViewPort = pGraphicsDeviceManager.GraphicsDevice.Viewport;

        AspectRatio = (float)vViewPort.Width / (float)vViewPort.Height;
        NearClip = 1.0f;
        FarClip = 2000.0f;
        ViewAngle = MathHelper.PiOver4;
    }

    public override void Update(GameTime pGameTime)
    {
        ViewMatrix = Matrix.CreateLookAt(CameraPosition, CameraTarget, Vector3.Up);
        ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(ViewAngle, AspectRatio, NearClip, FarClip);
        base.Update(pGameTime);
    }
}

Notice the Camera doesn't change, its just set static out in the world looking at Zero.

Now the Model Class:

public class Soldier : DrawableGameComponent
{
    Game1 fGame;
    Model fModel;

    public Vector3 Forward { get; set; }
    public Vector3 Position { get; set; }

    public Soldier(Game1 pGame)
        : base(pGame)
    {
        fGame = pGame;
        Forward = Vector3.Forward;
        Position = Vector3.Zero;
    }

    protected override void LoadContent()
    {
        fModel = fGame.Content.Load<Model>(@"Models\Human\Soldier\MP_US_Support");
        //fModel = fGame.Content.Load<Model>(@"Models\Vehicles\Planes\model_plane");
    }

    public override void Update(GameTime pGameTime)
    {
        base.Update(pGameTime);
    }

    public override void Draw(GameTime pGameTime)
    {
        foreach (ModelMesh vMesh in fModel.Meshes)
        {
            foreach (BasicEffect vBasicEffect in vMesh.Effects)
            {
                vBasicEffect.Projection = fGame.fCameraManager.ViewMatrix;
                vBasicEffect.View = fGame.fCameraManager.ProjectionMatrix;
                vBasicEffect.World = Matrix.Identity;
            }
            vMesh.Draw();
        }
        base.Draw(pGameTime);
    }
}

Again, the model loads fine (I think). I set the Projection/View to the Camera's ViewMatrix/ProjectionMatrix.

And finally in my Game1.cs I add these as components:

protected override void Initialize()
{
    //Initialize Camera
    fCameraManager = new CameraManager(this,fGraphicsDeviceManager);
    this.Components.Add(fCameraManager);

    //Initialize Map
    fMap = new FlatMap(this);
    this.Components.Add(fMap);

//Initialize a Soldier
fSoldier = new Soldier(this);
this.Components.Add(fSoldier);

this.IsMouseVisible = true; 
base.Initialize();

}

If anyone is interested in the full source code it can be found here: http://www.filedropper.com/worldexplorergame (26mb due to a model, otherwise the amount of source code is super small).

Upvotes: 0

Views: 102

Answers (1)

Nico Schertler
Nico Schertler

Reputation: 32667

You have swapped projection and view transformation:

vBasicEffect.Projection = fGame.fCameraManager.ProjectionMatrix;
vBasicEffect.View = fGame.fCameraManager.ViewMatrix;

Btw, your model seems a bit too detailed. If you want to have multiple soldiers and want to maintain real-timeness, you should consider simplifying the model.

Upvotes: 1

Related Questions