2015-01-03 81 views
0

對於使用colladas的骨架動畫,我需要在2個矩陣之間進行線性插值。我看到某處我可以使用四元數在矩陣之間進行插值,但只適用於旋轉分量,而且我還需要保留變換。這裏是我的代碼,除了翻譯部分:2個4x4矩陣之間的插值

float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1])*100.0; 
         float progress = orderedBones[i]->Animation->accumTime - (orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]*100.0); 
         float interpolation = progress/total; 

         glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]); 
         glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]); 
         glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation); 

         orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat); 

有沒有什麼辦法可以做到這一點?

+0

我會建議加標籤'quaternion'(單數) –

+0

@SeverinPappadeux新增:) – BlueSpud

+0

是什麼啥東東? –

回答

2

我最終通過更多的網上衝浪來解決我的問題。爲了將來的參考,繼承人如何做到這一點。

變換組件被存儲在4×4矩陣是這樣的:

r r r t 
r r r t 
r r r t 
0 0 0 1 

其中r是旋轉分量和t是平移分量。因此,我們可以將翻譯組件表示爲一個向量。 2矢量可以線性插值,所以我們插入這兩個矢量,然後在旋轉矩陣完成後將它們推回到旋轉矩陣中。繼承人的最終代碼,但它有點混亂:

float total = (orderedBones[i]->Animation->keyFrames[nextKeyFrame] - orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1])*ANIMATION_MULTIPLICATION_CONST; 
         float progress = orderedBones[i]->Animation->accumTime - (orderedBones[i]->Animation->keyFrames[nextKeyFrame - 1]*ANIMATION_MULTIPLICATION_CONST); 
         float interpolation = progress/total; 


         glm::quat firstQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1]); 
         glm::quat secondQuat = glm::quat_cast(orderedBones[i]->Animation->Matrices[nextKeyFrame]); 
         glm::quat finalQuat = glm::slerp(firstQuat, secondQuat, interpolation); 

         orderedBones[i]->Animation->interpoltaedMatrix = glm::mat4_cast(finalQuat); 

         glm::vec4 transformComp1 = glm::vec4(orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][0][3],orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][1][3],orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][2][3],orderedBones[i]->Animation->Matrices[nextKeyFrame - 1][3][3]); 
         glm::vec4 transformComp2 = glm::vec4(orderedBones[i]->Animation->Matrices[nextKeyFrame][0][3],orderedBones[i]->Animation->Matrices[nextKeyFrame][1][3],orderedBones[i]->Animation->Matrices[nextKeyFrame][2][3],orderedBones[i]->Animation->Matrices[nextKeyFrame][3][3]); 

         glm::vec4 finalTrans = (float)(1.0-interpolation)*transformComp1+transformComp2*interpolation; 


         //good for now, although in future the 2 transformation components need to be interpolated 
         orderedBones[i]->Animation->interpoltaedMatrix[0][3] = finalTrans.x; 
         orderedBones[i]->Animation->interpoltaedMatrix[1][3] = finalTrans.y; 
         orderedBones[i]->Animation->interpoltaedMatrix[2][3] = finalTrans.z; 
         orderedBones[i]->Animation->interpoltaedMatrix[3][3] = finalTrans.w; 

希望這能回答別人的問題:)