XNA Models

A an XNA Model encapsulates a 3D Entity, a person, a vehicle or even a level. A model contains a number of "ModelMeshes", each modelmesh is a component wichich can be moved somewhat independently (arm, wheel, lift etc). Each modelmesh is composed of a bunch of ModelMeshParts which are individual primitive draw calls [ a group of triangles with the same material.

Each ModelMesh has a Draw() method so that components can be drawn in different places.

The Model is created by the XNA content importer, which populates the Model object with data from the file produced by the DCC. The content importer exampines material property of the each ModelMeshPart and configures a BasicEffect, which controls the colour, texture and lighting properties for the mesh.

     foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = worldMatrix;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
        }

        mesh.Draw();
    }

Within the DCC model components can be taged, if the tags are known, the programmer can use the following to render a specific components;

        ModelMesh= model.Meshes["head"];
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = worldMatrix;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
        }

        mesh.Draw();

//or

	foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = worldMatrix;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
        }

        if(mesh.Name!="Wheel_rf")
        	mesh.Draw();
    }
    

The relative position of ModelMeshes is controled by a 4x4 transformation called a 'Bone'. The bones dictate where the components are positioned relative to the 'origin' of the Model. If a model contain bones (most non trivial models do), then the world transform of the Effect for each ModelMeshPart must be given the appropriate bone.

    
    Matrix[] transforms = new Matrix[model.Bones.Count];

    Model.CopyAbsoluteBoneTransformsTo(transforms);

    foreach (ModelMesh mesh in model.Meshes)
    {
        foreach (BasicEffect effect in mesh.Effects)
        {
            effect.World = transforms[mesh.ParentBone.Index]*world;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
        }

        mesh.Draw();
    }