Top

Understanding the Model class in XNA

March 1, 2008

A Sample ModelMicrosoft has really gone out of their way to provide a ton of cool sample code for you to look at. Nevertheless, it can all be a bit intimidating for someone just getting into game development (even though XNA is definitely your best bet!).

Let’s take a look at the Model class, as that’s the first thing you’re going to need to understand if you want to get your game off the ground.

The Model class is simply a container for everything contained in the .fbx or .x file you are loading. It holds your model until you are ready to draw it to the screen (usually in the Draw() function, or in a container class’ Draw() function). You will typically see something like the following used to render a model:

foreach (ModelMesh mesh in boxModel.Meshes) {

    foreach (Effect effect in mesh.Effects) {

        effect.Parameters["World"].SetValue(world);

        effect.Parameters["View"].SetValue(view);

        effect.Parameters["Projection"].SetValue(projection);

    }    mesh.Draw();

}

That’s pretty neat, but wouldn’t it be nice to know what’s actually happening?

The good news is that its really quite simple. We can think of a model as having a hierarchy:

  • Model
    • ModelMesh
      • ModelMeshPart
        • Effect

The Model contains a bunch of ModelMesh objects. For example, your character model may consist of a mesh for the head, one for the body, and one for each arm and leg.

Each ModelMesh, in turn, contains one or more ModelMeshPart objects. What are those? Simply put, they are the smallest part of the mesh that can be sent to the video card in one pass. If you’ve worked deeper with, say, DirectX 9, you’ve probably dealt with triangle strips, vertex buffers, etc. You can think of a ModelMesh in these terms. In order to render the entire mesh, it needs to be rendered in (hopefully) an efficient series of parts. The ModelMeshPart objects are simply those parts.

Which brings us to the Effect objects. You should be able to see why these are children of the ModelMeshPart objects. Each of these Effects describes how the mesh part should be rendered, and as such, they contain the world, view and projection matrices that describe how to place the object in the 3D world, how to move the camera relative to the object, and how to project the 3D object to a 2D display.

You may have noticed that some examples refer to Effect objects, and some refer to BasicEffect objects. A BasicEffect is a subclass of Effect in which a few things that you may commonly do are built-in. So you could, for example, handle lighting simply by calling the BasicEffect.EnableDefaultLighting() function, which sets up the basic 3 light lighting rig that looks good enough.

We’ll talk about lighting in a later article. For now, know that a BasicEffect will cover the basics. If you want to create your own shaders, using the HLSL or the shader ASM language, you would use an Effect object for that.

There is just one more thing to look at. Notice in the code pasted above we do this:

foreach (ModelMesh mesh in boxModel.Meshes) {
    foreach (Effect effect in mesh.Effects) {

But wait, how can that be? I thought the Effects were children of the ModelMeshPart objects, not the ModelMesh objects! Well, they are. This simply gives you a shortcut to the entire list. So if all you want to do is iterate through the Effects and use them to render your model, you can use this shortcut. If you need to do some finer tweaks, like replacing effects with other effects, you’ll need to use Effects contained in the ModelMeshPart objects.

Comments

Got something to say?





Bottom