-1
\$\begingroup\$

I am trying to draw a 3D Model in XNA 4.0, but it doesn't show up. I think I made it correctly, but maybe not. My code follows:

Here is the LoadContent and Draw method of my Main class. Spaceship is a class representing the model I want to draw.

    private Spaceship _spaceship;
    private Matrix _view, _projection;

    protected override void LoadContent()
    {
        _spaceship = new Spaceship(Game, Game.Content.Load<Model>("spaceship"), Vector3.Zero);

        _view = Matrix.CreateLookAt(_spaceship.CamPos, new Vector3(0, 0, 0), Vector3.UnitY);
        _projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
            Game.Window.ClientBounds.Width / Game.Window.ClientBounds.Height, 1f, 100f);
    }

    public override void Update(GameTime gameTime)
    {
        _view = Matrix.CreateLookAt(_spaceship.CamPos, new Vector3(0, 0, 0), Vector3.UnitY);
    }

    public override void Draw(GameTime gameTime)
    {
        _spaceship.Draw(_view, _projection);
        base.Draw(gameTime);
    }

Here is the Spaceship class.

    private Vector3 _camPos;
    protected Matrix _world;

    public Vector3 CamPos
    {
        get { return _camPos; }
    }

    public Spaceship(Game game, Model spaceship, Vector3 position)
    {
        _game = game;
        _model = model;
        _position = position;

        Initialize();
    }

    private void Initialize()
    {
        _camPos = new Vector3();

        _camPos.X = Position.X;
        _camPos.Y = Position.Y;
        _camPos.Z = Position.Z + 10;
    }

    public override void Update(GameTime gt)
    {
        _world = Matrix.Identity * Matrix.CreateTranslation(_position);

        _camPos.X = Position.X;
        _camPos.Y = Position.Y;
        _camPos.Z = Position.Z + 10;
    }

    public virtual void Draw(Matrix view, Matrix projection)
    {
        ModelDrawer.DrawModel(_model, _world, view, projection);
    }

Finally, this is my ModelDrawer class's DrawModel method:

    public static void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
    {
        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.World = world;
                effect.View = view;
                effect.Projection = projection;
            }

            mesh.Draw();
        }
    }

This is what the model looks like in Sketchup before I export to .fbx.

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ This question appears to be off-topic because it is about debugging your code for you. \$\endgroup\$
    – user1430
    Jan 14 '14 at 16:28
1
\$\begingroup\$

Seems that I simply missed a line of code in the main class update method.

 _spaceship.Update(gameTime);
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.