Rendering Full Textured Meshes

Now that we have the ability to construct and render textured objects, you are probably thinking, “Do I really have to create a vertex for every single point on my game models?” The answer is basically yes, but it does not apply to the code itself. You will have to create your own models or obtain some that are made in a 3D modelling program like Blender, or 3D Studio Max.

Once you have these models you can add the code into your engine to render them. This way, you can visually create your models and load them into your game without having to specify any buffers or vertices along the way (actually, the Mesh class that DirectX supplies creates the vertex and index buffers for us.) With that said, lets see how we can load these meshes.

Step 1: Loading the Mesh, its Materials and Textures

The first thing we want to do before we can draw our mesh is to load the mesh file itself and make sure we get all of the textures it contains loaded correctly. Here is how we do that:

public class HMMesh : HMObject { private Material[] myMaterials; private Texture[] myTextures; private Mesh myMesh; private string myPath; public HMMesh(string meshPath, Vector3 pos, Vector3 rot, Vector3 scl, Device myDevice) { myPath = meshPath; myPosition = pos; myRotation = rot; myScaling = scl; ReloadResources(myDevice); } public override void ReloadResources(Device myDevice) { ExtendedMaterial[] mtrls; myMesh = Mesh.FromFile(myPath, MeshFlags.Managed, myDevice, out mtrls); // Store any mtrls included in the mesh if((mtrls != null) && (mtrls.Length > 0)){ myMaterials = new Material[mtrls.Length]; myTextures = new Texture[mtrls.Length]; // Store each material and texture for(int i = 0; i < mtrls.Length; i++){ myMaterials[i] = mtrls[i].Material3D; if((mtrls[i].TextureFilename != null) && (mtrls[i].TextureFilename != string.Empty)) { // This material has a texture associated with it myTextures[i] = TextureLoader.FromFile(myDevice, mtrls[i].TextureFilename); } } } } }

This will load and retrieve all the materials and textures along with the main mesh file and store them all in our custom Mesh class.

Step 2: Putting our Mesh on the Screen

Now all that is left to do is render the mesh. Here is the function for doing so:

public override void Render(Device myDevice) { // Set the video card to use our objects tranformations myDevice.Transform.World = Matrix.Scaling(myScaling) * Matrix.RotationYawPitchRoll(myRotation.Y, myRotation.X, myRotation.Z) * Matrix.Translation(myPosition); for(int i = 0; i < myMaterials.Length; i++) { myDevice.Material = myMaterials[i]; myDevice.SetTexture(0, myTextures[i]); myMesh.DrawSubset(i); } }

Now if you create a new Mesh object in the Main class of the demo and add it, you will see a lovely rendered tiger mesh in the middle of the screen (remember to put the mesh and all its textures in the bin folder of the demo project.) In case you weren’t sure how to do this (you should know by now), here is the code:

HMMesh mesh1 = new HMMesh(”tiger.x”, new Vector3(), new Vector3(), new Vector3(), demo1.MyDevice); demo1.MyScene.AddObject(mesh1);

Leave a Reply

You must be logged in to post a comment.