DirectX - 骨架动画方法不正确

时间:2012-02-10 22:03:13

标签: c++ animation directx skeletal-mesh

我正在尝试在DirectX 9中编写骨架动画程序,我已经使用LoadMeshFromHierarchy函数来加载动画网格...现在我想绕过animController,这样我就可以通过读取关键帧来控制动画了动画网格文件(例如tiny.x)并随意循环遍历这些键。

这是我到目前为止所做的...此时我已经成功解析了.x文件并存储了每个动画,并且动画键是一个类(Anim)中唯一的动画集。当我运行这个更新功能时,动画网格被毁坏,我无法弄清楚为什么......我认为这是我更新每个帧的转换矩阵的过程......这是我的代码:

void cAnimationCollection::Update(DWORD AnimSetIndex, DWORD time)
{
    D3DXFRAME_EXTENDED *currentFrame = (D3DXFRAME_EXTENDED*)m_entity->m_frameRoot;

    cAnimationSet *AnimSet = m_AnimationSets;

    assert(AnimSetIndex <= index);
    while(AnimSet != NULL)
    {
        if(AnimSet->m_index == AnimSetIndex)
        {
            cAnimation *Anim = AnimSet->m_Animations;
            while(Anim != NULL)
            {   
                D3DXMatrixIdentity(&Anim->m_Frame->TransformationMatrix);
                if(Anim->m_NumScaleKeys && Anim->m_ScaleKeys)
                {
                    DWORD ScaleKey=0, ScaleKey2=0;
                    for(DWORD i = 0; i < Anim->m_NumScaleKeys; i++)
                    {
                        if(time >= Anim->m_ScaleKeys[i].m_Time)
                            ScaleKey = i;
                    }
                    ScaleKey2 = (ScaleKey>=(Anim->m_NumScaleKeys-1))?ScaleKey:ScaleKey+1;
                    float TimeDiff = Anim->m_ScaleKeys[ScaleKey2].m_Time - Anim->m_ScaleKeys[ScaleKey].m_Time;
                    if(!TimeDiff)
                        TimeDiff = 1;
                    float Scalar = ((float)time - Anim->m_ScaleKeys[ScaleKey].m_Time) / (float)TimeDiff;
                    D3DXVECTOR3 vecScale = Anim->m_ScaleKeys[ScaleKey2].m_VecKey - Anim->m_ScaleKeys[ScaleKey].m_VecKey;
                    vecScale *= Scalar;
                    vecScale += Anim->m_ScaleKeys[ScaleKey].m_VecKey;

                    D3DXMATRIX matScale;
                    D3DXMatrixScaling(&matScale, vecScale.x, vecScale.y, vecScale.z);
                    Anim->m_Frame->TransformationMatrix *= matScale;
                }
                if(Anim->m_NumRotationKeys && Anim->m_RotationKeys)
                {
                    DWORD RotKey=0, RotKey2=0;
                    for(DWORD i = 0; i < Anim->m_NumRotationKeys; i++)
                    {
                        if(time >= Anim->m_RotationKeys[i].m_Time)
                            RotKey = i;
                    }
                    RotKey2 = (RotKey>=(Anim->m_NumRotationKeys-1))?RotKey:RotKey+1;
                    float TimeDiff = Anim->m_RotationKeys[RotKey2].m_Time - Anim->m_RotationKeys[RotKey].m_Time;
                    if(!TimeDiff)
                        TimeDiff = 1;
                    float Scalar = ((float)time - Anim->m_RotationKeys[RotKey].m_Time) / (float)TimeDiff;
                    D3DXQUATERNION quatRotation;
                    D3DXQuaternionSlerp(&quatRotation,
                        &Anim->m_RotationKeys[RotKey].m_QuatKey,
                        &Anim->m_RotationKeys[RotKey2].m_QuatKey,
                        Scalar);

                    D3DXMATRIX matRotation;
                    D3DXMatrixRotationQuaternion(&matRotation, &quatRotation);
                    Anim->m_Frame->TransformationMatrix *= matRotation;
                }
                if(Anim->m_NumTranslationKeys && Anim->m_TranslationKeys)
                {
                    DWORD PosKey=0, PosKey2=0;
                    for(DWORD i = 0; i < Anim->m_NumTranslationKeys; i++)
                    {
                        if(time >= Anim->m_TranslationKeys[i].m_Time)
                            PosKey = i;
                    }
                    PosKey2 = (PosKey>=(Anim->m_NumTranslationKeys-1))?PosKey:PosKey+1;
                    float TimeDiff = Anim->m_TranslationKeys[PosKey2].m_Time - Anim->m_TranslationKeys[PosKey].m_Time;
                    if(!TimeDiff)
                        TimeDiff = 1;
                    float Scalar = ((float)time - Anim->m_TranslationKeys[PosKey].m_Time) / (float)TimeDiff;
                    D3DXVECTOR3 vecPos = Anim->m_TranslationKeys[PosKey2].m_VecKey - Anim->m_TranslationKeys[PosKey].m_VecKey;
                    vecPos *= Scalar;
                    vecPos += Anim->m_TranslationKeys[PosKey].m_VecKey;;
                    D3DXMATRIX matTranslation;
                    D3DXMatrixTranslation(&matTranslation, vecPos.x, vecPos.y, vecPos.z);
                    Anim->m_Frame->TransformationMatrix *= matTranslation;
                }
                if(Anim->m_NumMatrixKeys && Anim->m_MatrixKeys)
                {
                    DWORD Key1 = 0, Key2 = 0;
                    for(DWORD i=0;i<Anim->m_NumMatrixKeys;i++)
                    {
                        if(time >= Anim->m_MatrixKeys[i].m_Time)
                            Key1 = i;
                    }
                    Key2 = (Key1>=(Anim->m_NumMatrixKeys-1))?Key1:Key1+1; 
                    float TimeDiff = Anim->m_MatrixKeys[Key2].m_Time - Anim->m_MatrixKeys[Key1].m_Time;
                    if(!TimeDiff)          
                        TimeDiff = 1;        
                    float Scalar = ((float)time - Anim->m_MatrixKeys[Key1].m_Time) / (float)TimeDiff; 
                    D3DXMATRIX matDiff = Anim->m_MatrixKeys[Key2].m_MatKey - Anim->m_MatrixKeys[Key1].m_MatKey;
                    matDiff *= Scalar;        
                    matDiff += Anim->m_MatrixKeys[Key1].m_MatKey;
                    Anim->m_Frame->TransformationMatrix *= matDiff;     
                }

                Anim = Anim->m_Next;
            }
        }
        AnimSet = AnimSet->m_Next;
    }
    m_entity->UpdateFrameMatrices(m_entity->m_frameRoot, 0);
    m_entity->UpdateSkinnedMesh(m_entity->m_frameRoot);
    if(AnimSet == NULL)
        return;
}

我的方法是否正确?我为每个帧做的第一件事就是将变换矩阵重置为identity,然后我计算每个键的内插值(平移,缩放,旋转和矩阵)并将其应用到变换矩阵...然后我更新框架矩阵,然后是蒙皮网格。

有什么想法吗?

0 个答案:

没有答案