我有兩個描述任意旋轉的旋轉矩陣。 (兼容4x4 opengl)在旋轉矩陣之間插值
現在我想插入它們之間,以便它遵循從一個旋轉到另一個旋轉的徑向路徑。想想一個三腳架上的相機,然後旋轉。
如果我插入每個組件我得到一個擠壓結果,所以我認爲我需要只插入矩陣的某些組件。但哪些?
我有兩個描述任意旋轉的旋轉矩陣。 (兼容4x4 opengl)在旋轉矩陣之間插值
現在我想插入它們之間,以便它遵循從一個旋轉到另一個旋轉的徑向路徑。想想一個三腳架上的相機,然後旋轉。
如果我插入每個組件我得到一個擠壓結果,所以我認爲我需要只插入矩陣的某些組件。但哪些?
您需要將矩陣轉換爲不同的表示 - 四元數對此很好,內插四元數是一個明確定義的操作。
您必須將SLERP用於矩陣的旋轉部分,對於其他部分則必須使用SLERP。最好的方法是把你的矩陣變成四元數,並使用(更簡單的)四元數SLERP:http://en.wikipedia.org/wiki/Slerp。
我建議閱讀Graphic Gems II或III,特別是關於將矩陣分解爲簡單變換的部分。下面是斯賓塞W.托馬斯本章來源:
http://tog.acm.org/resources/GraphicsGems/gemsii/unmatrix.c
當然,我建議你學習如何做這自己。這真的不那麼難,只是很多煩人的代數。最後,這裏有一個如何把一個矩陣到四元,而回,由id Software公司一個非常好的論文:http://cache-www.intel.com/cd/00/00/29/37/293748_293748.pdf
編輯:這是幾乎每個人都引用了公式,這是從1985年SIGGRAPH紙。
其中:
- qm = interpolated quaternion
- qa = quaternion a (first quaternion to be interpolated between)
- qb = quaternion b (second quaternion to be interpolated between)
- t = a scalar between 0.0 (at qa) and 1.0 (at qb)
- θ is half the angle between qa and qb
代碼:
quat slerp(quat qa, quat qb, double t) {
// quaternion to return
quat qm = new quat();
// Calculate angle between them.
double cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
// if qa=qb or qa=-qb then theta = 0 and we can return qa
if (abs(cosHalfTheta) >= 1.0){
qm.w = qa.w;qm.x = qa.x;qm.y = qa.y;qm.z = qa.z;
return qm;
}
// Calculate temporary values.
double halfTheta = acos(cosHalfTheta);
double sinHalfTheta = sqrt(1.0 - cosHalfTheta*cosHalfTheta);
// if theta = 180 degrees then result is not fully defined
// we could rotate around any axis normal to qa or qb
if (fabs(sinHalfTheta) < 0.001){ // fabs is floating point absolute
qm.w = (qa.w * 0.5 + qb.w * 0.5);
qm.x = (qa.x * 0.5 + qb.x * 0.5);
qm.y = (qa.y * 0.5 + qb.y * 0.5);
qm.z = (qa.z * 0.5 + qb.z * 0.5);
return qm;
}
double ratioA = sin((1 - t) * halfTheta)/sinHalfTheta;
double ratioB = sin(t * halfTheta)/sinHalfTheta;
//calculate Quaternion.
qm.w = (qa.w * ratioA + qb.w * ratioB);
qm.x = (qa.x * ratioA + qb.x * ratioB);
qm.y = (qa.y * ratioA + qb.y * ratioB);
qm.z = (qa.z * ratioA + qb.z * ratioB);
return qm;
}
來源:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
好的,謝謝我們說我使用四元數來代替。我是否只是插入4個組件中的每一個以獲得平滑過渡? – clamp 2010-11-14 11:46:27
用公式編輯我的答案,並用一些簡單的代碼來消化。 – 2010-11-14 18:14:06
你第一堆鏈接不再工作:( – Narfanator 2016-06-08 22:13:09
的感謝!那麼我怎樣才能將矩陣轉換爲四元數? – clamp 2010-11-04 17:29:37
我建議你首先閱讀下面的內容:http://en.wikipedia.org/wiki/Quaternion – tdammers 2010-11-04 17:35:36