Using 3D Models


In previous tutorials, we've talked about where to get 3D models from, or how to make your own. In this tutorial, we will take a look at how to use a 3D model in your game. This tutorial will just cover the basics, but in future tutorials we will look at a few more advanced features of drawing in 3D and working with models.

Loading the Model

The first thing we need to do is acquire a 3D model to place in our game. There's a good chance that you've already got something that you want to put in your game, but if not, you can go over to my 3D Model Library and grab one. For this tutorial, I will use the SimpleShip model there, but you are more than welcome to use one of your own.

Take your model, and all associated textures, and add them to the content of your project. See the tutorial on managing content if you don't remember how to do this. Now, we want our game to manage the model, but we don't want it to manage the texture, other than when it loads the model, so we will want to exclude any textures by right clicking on the texture in the Solution Explorer and choosing Exclude From Project. When we do this, the content pipeline will no longer process the file, but it will still be in the location that the model will look for it at.

The next step is to actually load the model into our game. This is pretty easy to do. We will first create a variable to store the model. XNA has a type already created specifically for this called the Model class. So go to the beginning of your class where your other instance variables are (with the template, you should already have one called graphics and one called spriteBatch), and add the following line of code:

private Model model;

Now we just need to load our model into this object. Go down to the LoadContent() method, and add the following line of code (replace "Ship" with the name of your model if you are using a different model):

model = Content.Load<Model>("Ship");

Your model will now be ready for use!

Drawing the Model

We are now ready to draw our model. It is at this point that we will use the stuff we talked about in the previous tutorial, which discussed Basic Matrices. If you haven't been through that tutorial, I would recommend going back to them now, but I've made the tutorial in such a way that if you want to continue without it, you can still do that. In order to draw, we will need to define the three standard matrices. So go back up to your instance variables section and add the following code:

private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
private Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY);
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f);

With these created, we have everything we need to draw. To make things easy for us, let's go down just below the Draw() method and create a new method, that will draw a single model, with given matrices. We can do this by adding the following code to our program:

private 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;

This code shouldn't be too difficult to understand. All it does is goes through each of the model's meshes (a model can be made up of multiple objects, which are called meshes), and it goes through each of the effects and sets the effect's matrices to be the matrices that we want. We will talk about effects a lot more later. For now it is enough for you to know that an effect simply determines how a model is to be displayed on the screen. Finally, we tell each mesh of the model to be drawn, and we're done.

All we need to do now is call this method with the appropriate values, and we will see our model appear on the screen. So add the following line of code to your Draw() method:

DrawModel(model, world, view, projection);

You should now be able to run your game, and see the model being drawn, as shown below:


You might be thinking that it doesn't look too good right now, and that it doesn't do anything but sit there, which is true. We will make big improvements in both of these areas in the next few tutorials.

Download the completed project

What's Next?

In this tutorial we looked at how to do simple drawing of a 3D model. We've still got a ways to go before this will really be useful in a game, but at least we're making progress. There's really two directions you can go from here. First, you can go ahead and look at the tutorials for working with the BasicEffect class, which will really help to make your model look cooler. Also, you can go look at the tutorials for doing animation in 3D, which builds directly off of this tutorial.

Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!