Well, I think I finally have it figured out. After much research I have figured that I have to load the mesh Hierarchy and for each instance of it I have to clone the frame hierarchy and the animation controller, then set the frame hierarchy to use the new animation controller. Everything seems to be working OK except that the program is hanging up in my FrameMove function at the call to ID3DxAnimationController::AdvanceTime(). Here is what I have so far
Code:
void CMeshInstance::SetMesh(LPDIRECT3DDEVICE9 pDevice, CSkinnedMesh *pMesh, bool MakeSphere, bool MakeBox)
{
Release();
m_pSkinnedMesh = pMesh;
D3DXFRAME *pOriginalFrameRoot = m_pSkinnedMesh->GetFrameRoot();
ID3DXAnimationController *pAC = m_pSkinnedMesh->GetAnimController();
if (pAC && m_pFrameRootEx)
{
// Clone the mesh hierarchy
CloneFrameHierarchy(pOriginalFrameRoot, &m_pFrameRootEx);
// Clone the animation controller
if(FAILED(pAC->CloneAnimationController(pAC->GetMaxNumAnimationOutputs(),
pAC->GetMaxNumAnimationSets(),
pAC->GetMaxNumTracks(),
pAC->GetMaxNumEvents(),
&m_pAnimControllerInstance) ) )
{
SHOW_ERROR("Cannot clone animation controller");
}
// Set our cloned animation controller to work with our cloned frame hierarchy
if(FAILED(D3DXFrameRegisterNamedMatrices(m_pFrameRootEx, m_pAnimControllerInstance) ) )
{
SHOW_ERROR("Cannot register frame hierarchy with animation controller");
}
// Set the bones up
SetupBoneMatrices(pDevice,(D3DXFrameEx*)m_pFrameRootEx, NULL);
// Create the bone matrices array for use during FrameMove to hold the final transform
m_pBoneMatrices = new D3DXMATRIX[m_NumBones];
ZeroMemory(m_pBoneMatrices, sizeof(D3DXMATRIX) * m_NumBones);
}
return;
}
and my FrameMove function
Code:
void CMeshInstance::FrameMove(float ElapsedTime)
{
// Adjust animation speed
ElapsedTime /= m_SpeedAdjust;
// Advance the time and set in the controller
if (m_pAnimControllerInstance)
{
m_pAnimControllerInstance->AdvanceTime(ElapsedTime, NULL);
} else {
SHOW_MESSAGE("Anim Controller set to NULL");
}
m_CurrentTime += ElapsedTime;
// Now update the model's world matrices in the hierarchy
UpdateFrameMatrices(m_pFrameRootEx, GetTransform() );
// If the model contains a skinned mesh update the vertices
LPD3DXMeshContainerEx pMesh = m_pFirstMeshEx;
if(pMesh && pMesh->pSkinInfo)
{
UINT Bones = pMesh->pSkinInfo->GetNumBones();
// Create the bone matrices that transform each bone from bone space into
// character space, and also wraps the
// mesh around the bones using the bone offsets
for (UINT i = 0; i < Bones; ++i)
D3DXMatrixMultiply(&m_pBoneMatrices[i],
&pMesh->pBoneOffsetsEx[i],
pMesh->ppCombinedMatrixEx[i] );
// Modify the vertex positions based on the new bone matrices.
void *srcPtr;
pMesh->MeshData.pMesh->LockVertexBuffer(D3DLOCK_READONLY, (void**)&srcPtr);
void *destPtr;
pMesh->pSkinMeshEx->LockVertexBuffer(0, (void**)&destPtr);
// Update the skinned mesh
pMesh->pSkinInfo->UpdateSkinnedMesh(m_pBoneMatrices, NULL, srcPtr, destPtr);
// Unlock the meshes vertex buffers
pMesh->pSkinMeshEx->UnlockVertexBuffer();
pMesh->MeshData.pMesh->UnlockVertexBuffer();
}
return;
}
in RED is where it hangs up. If I comment out that part then it runs just fine, just without any animation.
Can anyone tell me what I am doing wrong here?