How To: Render a Model

Demonstrates how to load and render a model using the XNA Framework Content Pipeline. It is assumed that an existing Windows game project is loaded in XNA Game Studio. In this example, the project is called "CPModel."

This example has three main parts: importing and processing the model (design-time code), drawing the resultant managed object as a model with full lighting effect in the game (run-time code), and enabling movement of the model with the Xbox 360 game pad (run-time code).

This technique is implemented in the FuelCell game, a game developed by following a series of focused articles that discuss basic 3D game development. For more information, see FuelCell: Setting the Scene.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Adding and Rendering a Model

To add the model to the game

  1. Right-click the Content project, click Add, and then click Existing Item.
  2. From the Files of type: control, click XNA Content Files.
  3. Navigate to the correct folder and select the model to be added.

    For this example, use the wedge_player1.x file.

  4. Click the small arrow to the right of the Add button, and then click Add as Link.

    This creates a reference to the selected asset (and not a copy) in your Content project.


    Making a reference to an existing file in your project is quite different from adding an existing item to your project. A file reference only stores the path to the file in question, not a copy of the contents. This is useful if the referenced file is dependent on additional external files and ensures that the solution always uses the latest version. This differs from adding a copy of the file to the project. In this case, it is possible to break external file dependencies and create extra work if the original content changes but the copy is not updated.

    For these reasons, it is recommended that you place any media used by the solution in a subfolder of the solution, and that you add a reference to the media and not a copy of the media.

  5. After you add the asset, open the Properties window and verify that the correct importer and processor are specified.

    For this example, the Content Importer is X File–XNA Framework and the Content Processor is Model–XNA Framework. For ease of reference, the asset name value was manually changed to "ship" using the Properties window.

    For more information on the Properties window of a game asset, see Game Asset Properties.

  6. Save the solution.

At this point, build the solution.

This completes the design-time portion of the example. The remaining parts render the model using the managed object (called "ship") and add some user control of the model. All code modifications for this part occur within the game1.cs file of the game project.

To render the model

  1. From Solution Explorer, double-click the game1.cs file.
  2. Modify the Game1 class by adding the following code at the beginning of the declaration.

    private Model gameShip;

    This member is used to hold the ship model.

  3. Modify the LoadGraphicsContent method by adding the following code.

    gameShip = Content.Load<Model>("ship");

    This code loads the model into the gameShip member (using Load).

  4. Create a new private method (called DrawModel) in the Game1 class by adding the following code before the existing Draw method.

    private void DrawModel(Model m)
        Matrix[] transforms = new Matrix[m.Bones.Count];
        float aspectRatio = graphics.GraphicsDevice.Viewport.Width / graphics.GraphicsDevice.Viewport.Height;
        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.View = view;
                effect.Projection = projection;
                effect.World = gameWorldRotation * transforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Position);

    This code sets up the lighting effects for each sub-mesh of the model. The gameWorldRotation and Zoom variables are used for player control. This functionality is added later.

    This render code is designed for only those models with a BasicEffect. For custom effects, the inner for-each loop should be changed to use Effect instead of BasicEffect. In addition, you must use EffectParameter objects to manually set the world, view, and projection matrices.
  5. Modify the Game1.Draw method by replacing the following code // TODO: Add your drawing code here with the following code:


    This causes the effects initialization of the model before the entire model is rendered.

  6. Save the solution.

At this point, the rendering code for the model is complete, but the user control code still needs implementation.

To move the model:

  1. Modify the Game1 class by adding the following code after the gameShip declaration.

    private Vector3 Position = Vector3.One;
    private float Zoom = 2500;
    private float RotationY = 0.0f;
    private float RotationX = 0.0f;
    private Matrix gameWorldRotation;

    These members store the current position, zoom, and rotation values. In addition, the gameWorldRotation simplifies the UpdateGamePad code.

  2. Add a private method (called UpdateGamePad) before the call to Update.

    private void UpdateGamePad()
        GamePadState state = GamePad.GetState(PlayerIndex.One);
        if (state.Buttons.A == ButtonState.Pressed)
        Position.X += state.ThumbSticks.Left.X * 10;
        Position.Y += state.ThumbSticks.Left.Y * 10;
        Zoom += state.ThumbSticks.Right.Y * 10;
        RotationY += state.ThumbSticks.Right.X;
        if (state.DPad.Up == ButtonState.Pressed)
            RotationX += 1.0f;
        else if (state.DPad.Down == ButtonState.Pressed)
            RotationX -= 1.0f;
        gameWorldRotation = Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) *

    This code implements an exit method for the game (pressing the A button), and updates the position members with the current input of the game controller.

  3. Modify the Update method by adding a call to UpdateGamePad, before the call to Update.


    This code updates the state of the position variables with the latest input.

  4. Save the solution.

At this point, all development is complete. You can control the ship location with the game pad, and exit by pressing the A button.