Now lets create a model animator for the dwarf that will allow us to view the dwarf and animate it.

Viewing the Dwarf with a ModelAnimator object

  • First, create two member variables:
    // Add these as member variables
    ModelAnimator dwarfAnimator;
    Matrix view;
  • Load the dwarf model and create a new instance of the animator in the LoadGraphicsContent method.
  • The ModelAnimator will render the model automatically by default, but we do need to set the view and projection matrices for the effects associated with the model.
  • Here is an example of what your LoadGraphicsContent should look like after setting the effects. Notice that because the dwarf is skinned, it will be loaded with BasicPaletteEffect:
    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {
            Model model = content.Load<Model>("dwarfmodel");
            dwarfAnimator = new ModelAnimator(this, model);
            Viewport port = graphics.GraphicsDevice.Viewport;
            view = Matrix.CreateLookAt(
                new Vector3(0, 15, -20), Vector3.Zero, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4, (float)port.Width / port.Height, .1f, 100000f);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicPaletteEffect effect in mesh.Effects)
                {
                    effect.View = view;
                    effect.Projection = projection;
                }
            }
        }

    }

Possible Questions

  • Why do I have to set the View and Projection matrices but not the world matrix for ModelAnimator?
  • How do I override the ModelAnimator's automatic rendering and draw the dwarf when I want to?
  • What is a "skinned" model and how do I know when BasicEffect/BasicPaletteEffect/My own effects are loaded from the content pipeline?

Last edited Mar 7, 2007 at 8:48 PM by dastle, version 10

Comments

Vino87 Mar 6, 2009 at 11:29 AM 
I get an error when the game tries to draw. It says that I need both a valid vertex and pixel shader (or valid affect) set on the device before drawing. Is this not done in the dwarfAnimator? I'm using XNA 3.0 as well as C# 2008 express edition. Any ideas?

Vino87