N E D Gold:
Disregarding the animation data for the moment, I added these two lines:
...
private int boneDataIndex;
...
boneDataIndex = meshBuilder.CreateVertexChannel<BoneWeightCollection>(VertexChannelNames.Weights());
...
but I receive an error (Unable to parse ms3d txt file. Exception: Object reference not set to an instance of an object. )
I'm making progress!!!
I first thought the above error was some sort of syntax error because all I did was tell the meshBuilder I was going to add some bone weights to it. But Nooooo, in this case the error means it wants bone data now! P.S. only use that statement and the other bone related statements if your model actually has bone data.
In this case, this means I need to process the bone data portion of the ms3d file before I process the vertex data. Why? Because I have learned that CreateVertexChannel(weights) requires a BoneWeightCollection as input which is a collection of one or more BoneWeight objects that specify the name and weight for that vertex. The BoneWeight.Name must match a BoneContent.Name object already in existence. Thus, I have to build all of the BoneContent objects first. And that's fine, as this is where you build the skeleton hierarchy that I was previously concerned with. Unfortunately, bone data comes after vertex data in the ms3d file, so two passes are required. (Actually, this pre-processing must also be done for building up the Materials ahead of the vertices as well, so my importer makes three passes; one for the materials, one for the bones, and lastly one for the vertex/tex cood/normals).
Now I think I have a proper skeleton of bones so let's skip ahead to the generic model drawing fragment below:
// Copy any parent transforms.
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
In my test model, I have 46 bones. But this model is kind of weird in that it had a camera attachment point (cam bone) that wasn't directly connected to the rest of the bones (it was a second root bone). Since you can't have two root bones (as I learned the hard way) I created a master root bone and attached the main skeleton with this cam bone. Anyway, that means I should have 46+1 bones in the model. However, in the above code, model.Bones.Count = 52. Huh?
I then loaded a simple model that is only two triangles and no bones whatsoever, yet model.Bones.Count = 2. So that got me thinking....BoneContent extends from NodeContent and NodeContent is the return value for Importers. Each mesh you process is also a NodeContent object that is added as a child to the root node that will become the return value. (Bones are also added as children to the root node.) So, for the simple two triangle model, you have the main root node plus the mesh node = 2 nodes and apparently nodes also means bones.
Back to my test model, it has 4 meshes, so let's see if it adds up: 1 root node return value + 4 mesh nodes + 46 bone nodes + 1 master root bone node = 52. Yea!!!! it works.
But wait! Won't this throw a monkey wrench in the works when it comes to animating later on? How am I going to separate mesh nodes from bone nodes? I guess I'll soon find out as I tackle animation next.
- - - - - - - -
Although my Importer builds w/o errors and draws correctly I won't know if the bones I added are actually correct until I start to move them. After all, the model was drawn correctly before I even started adding bones to it.
Could someone please confirm that I'm on the right track? I'd really appreciate it. Thanks!