# Trying to figure out ID3DXAllocateHierarchy:

This topic is 4277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been working through the tutorials http://www.toymaker.info/Games/html/load_x_hierarchy.html and http://www.gamedev.net/reference/articles/article2079.asp and I'm understanding most of the stuff they put in the CreateMeshContainer method, however I'm struggling to understand what some of the matrices are used for.
// If there is skin data associated with the mesh copy it over
if (pSkinInfo)
{
// save off the SkinInfo
newMeshContainer->pSkinInfo = pSkinInfo;

// Need an array of offset matrices to move the vertices from the figure space to the bone's space
UINT numBones = pSkinInfo->GetNumBones();
newMeshContainer->exBoneOffsets = new D3DXMATRIX[numBones];

// Create the arrays for the bones and the frame matrices
newMeshContainer->exFrameCombinedMatrixPointer = new D3DXMATRIX*[numBones];

// get each of the bone offset matrices so that we don't need to get them later
for (UINT i = 0; i < numBones; i++)
newMeshContainer->exBoneOffsets = *(newMeshContainer->pSkinInfo->GetBoneOffsetMatrix(i));
}
else
{
// No skin info so null all the pointers
newMeshContainer->pSkinInfo = NULL;
newMeshContainer->exBoneOffsets = NULL;
newMeshContainer->exSkinMesh = NULL;
newMeshContainer->exFrameCombinedMatrixPointer = NULL;
}


The parts I don't understand are what this part means "Need an array of offset matrices to move the vertices from the figure space to the bone's space" and why those matrices copied to a new array when the same info already seems to be in pSkinInfo which I already copied into the container. Also, exFrameCombinedMatrixPointer is allocated but doesn't seem to be filled in with any data. It would make more sense to me (at the moment) to allocate it when it's needed. Am I missing where it fills in the array or it it just there ready for when it's needed? It looks like it's supposed to point to the exCombinedTransformationMatrix in the frame struct. It that what it is?
struct D3DXMESHCONTAINER_EXTENDED: public D3DXMESHCONTAINER
{
// The base D3DXMESHCONTAINER has a pMaterials member which is a D3DXMATERIAL structure
// that contains a texture filename and material data. It is easier to ignore this and
// instead store the data in arrays of textures and materials in this extended structure:
IDirect3DTexture9**  exTextures;		// Array of texture pointers
D3DMATERIAL9*		 exMaterials;		// Array of materials

// Skinned mesh variables
ID3DXMesh*           exSkinMesh;			// The skin mesh
D3DXMATRIX*			 exBoneOffsets;			// The bone matrix Offsets, one per bone
D3DXMATRIX**		 exFrameCombinedMatrixPointer;	// Array of frame matrix pointers
};

struct D3DXFRAME_EXTENDED: public D3DXFRAME
{
D3DXMATRIX exCombinedTransformationMatrix;
};


In my own code I put:
		// Array of pointers to the bone matrices in character space
// Pointer to the combined/frame matrix (matCombined)
// Does this get used yet?
pMeshContainer->ppFrameMatrices = new LPD3DXMATRIX[dwBones];
Where matCombined is the name of the exCombinedTransformationMatrix in my frame structure. One other thing that confused me was why they added the D3DMATERIAL9 array when a D3DMATERIAL9 is already part of the D3DXMATERIAL pointed to by the elements in the pMaterials array.

##### Share on other sites
Too long winded again? I'm basically just asking what happend with those bone matrices in the examples and why the exising material array in the container struct doesn't get used.

##### Share on other sites
There are different ways to go with this. It just comes down to how you want to make all the information available to your main application for rendering the mesh. They chose to do a lot of the processing inside the AllocateHierarchy stuff. You will eventually need an array of the offsetMatrices for rendering the mesh. You can leave the materials in the fields already provided for in the meshcontainer if you want to. You just gotta make sure you can access them when you render each subset of the mesh.

Take a look at the actual data you need to render the mesh, and the things you must do each frame when you are running an animation. Then I think you will see how the constructs they use can make it easier to update each D3DXFRAME each frame.

##### Share on other sites
From what I can tell, exSkinMesh is figured out elsewhere in the program other than in the CreateMeshContainer method.

It seems like the newMeshContainer->exBoneOffsets array is just a copy of all the matrices that if I wanted I could get from pSkinInfo->GetBoneOffsetMatrix long after my ID3DXAllocateHierarchy has been deleted.

I would guess that the exFrameCombinedMatrixPointer in D3DXMESHCONTAINER_EXTENDED will eventually get set to the exCombinedTransformationMatrix in the containers corresponding D3DXFRAME_EXTENDED. But only at some point after the ID3DXAllocateHierarchy has finished it's work.

OK, I didn't actually look at this to start with because I hadn't investigated the container struct and then I ended up looking for the explanation in the implementation. Anyway this bit makes sense now:

"The base D3DXMESHCONTAINER has a pMaterials member which is a D3DXMATERIAL structure that contains a texture filename and material data. It is easier to ignore this and instead store the data in arrays of textures and materials in this extended structure."

Assuming the assumsions I made above are correct, that just leaves:

"Need an array of offset matrices to move the vertices from the figure space to the bone's space"

Does this mean the vertices are all relative to the the bones and not the entire model? I understand the how the combined matix is the multiplication of all the matrices of parent bones/frames, but aren't the vertices sometimes relative to the entire model already?
I mean sometimes I've seen models where if you don't perform some of the transforms, the hands, feel and head all show up in the chest, but on other models it makes no difference at all.
If I just draw the skin itself with the model I'm using without applying any transforms it's still drawn the way it should be, but without the animations.
I would guess that if the person who drew the model put all the vertices in character space to start with, the offset matrices (if I understand correctly) would just be the identity matrix to begin with and only change to do the animations.