Giving ourselves some Objects for Play

One thing that I have decided is missing from our HMMesh class is a way to generate simple default meshes that come with DirectX like the torus and teapot (instead of adding lines to our class everytime we want to test a shader with a round object like I had been doing.) The changes neccessary for this function are very minimal, and so this will more than likely be the shortest tutorial of the whole series, but we will probably use a lot of these objects when testing our physics later.

Step 1: Adding a Default Object Constructor

To let ourselves pick from the 5 included default meshes, I decided to simply duplicate the constructor in HMMesh and take an integer instead of a string for the first parameter. Here is the new code:

private int myType = -1; public HMMesh(int meshType, Vector3 pos, Vector3 rot, Vector3 scl, Device myDevice) { myType = meshType; myPosition = pos; myRotation = rot; myScaling = scl; ReloadResources(myDevice); myTimer = new FrameworkTimer(); myTimer.Start(); } // At the top of the old constructor myType = -1;

Now that we have our constructor set up, we will need to modify the ReloadResources() function to implement our new default object behavior. This is as simple as adding an if else with a switch inside. The top of ReloadResources now looks like this:

ExtendedMaterial[] mtrls = null; if(myType != -1) { switch(myType) { case 0: myMesh = Mesh.Box(myDevice, 1, 1, 1); break; case 1: myMesh = Mesh.Cylinder(myDevice, 1, 1, 3, 32, 1); break; case 2: myMesh = Mesh.Sphere(myDevice, 1, 32, 32); break; case 3: myMesh = Mesh.Teapot(myDevice); break; case 4: myMesh = Mesh.Torus(myDevice, 0.25f, 0.5f, 32, 32); break; default: myMesh = Mesh.Teapot(myDevice); break; } myMaterials = new Material[1]; myTextures = new Texture[1]; } else { myMesh = Mesh.FromFile(myPath, MeshFlags.Managed, myDevice, out mtrls); }

There you have it. A very simple way to add quite a few new objects to the world. Now I just have to think of a use for them!

Leave a Reply

You must be logged in to post a comment.