yaw: 水平偏航轉角 (繞Y軸旋轉, Y軸向上)
pitch: 上下俯仰轉角 (繞X軸 旋轉, X軸向右)
roll: 旋轉角(繞Z軸 旋轉, Z軸向前)
Direct3D 左手座標
圖見: yaw pitch roll 圖
二、旋轉矩陣、視角矩陣、yaw-pitch-roll的關係
注意: 第一個 旋轉計算 完畢之後, 第二個旋轉的參考軸仍然是全局座標, 而不是自身參考軸。
D3DXMATRIXA16 mx_xlib;
D3DXMATRIXA16 mx_pitch_first;
D3DXMATRIXA16 mx_yaw_first;
D3DXMATRIXA16 mx_yaw, mx_pitch;
float yaw, pitch, roll =0.0f;
yaw = D3DX_PI/6;
pitch = D3DX_PI/3;
TRACE( "yaw: %f pitch: %f\n", yaw, pitch );
D3DXMatrixRotationYawPitchRoll( &mx_xlib, yaw, pitch, roll );
D3DXMatrixRotationY( &mx_yaw, yaw );
D3DXMatrixRotationX( &mx_pitch, pitch );
mx_pitch_first = mx_pitch * mx_yaw ;
mx_yaw_first = mx_yaw * mx_pitch ;
上述代碼結果: mx_pitch_first== mx_xlib. 但是 mx_yaw_first 與 mx_xlib 不同
如果 yaw 一定在前面, 則 pitch 使用自身參考軸, 見如下代碼:
D3DXMATRIXA16 mx_yp_local;
D3DXMATRIXA16 mx_local_pitch;
D3DXVECTOR3 vAxisX( 1.0, 0, 0 );
D3DXVECTOR3 vLocalX;
D3DXVec3TransformCoord( &vLocalX, &vAxisX, &mx_yaw );
D3DXMatrixRotationAxis( &mx_local_pitch, &vLocalX, pitch );
mx_yp_local = mx_yaw * mx_local_pitch ;
結果相同: mx_yp_local == mx_xlib.
2. D3DXMatrixRotationYawPitchRoll 生成的旋轉矩陣 不等於 攝像的視角矩陣。
視角矩陣 是通過 旋轉一個標準 z軸單位向量( 0,0,1) , 再通過 兩個點 來 確定 視角矩陣。 代碼見下:
D3DXVECTOR3 vUp (0,1,0);
D3DXVECTOR3 vZero(0,0,0);
D3DXVECTOR3 vZ(0,0,1.0f);
D3DXVECTOR3 vNewZ;
D3DXMATRIX mRotView;
D3DXVec3TransformCoord( &vNewZ, &vZ, &d_xlib );
D3DXMatrixLookAtLH( &mRotView, &vZero, &vNewZ, & vUp );
3. 視角矩陣 ==> 計算 yaw, pitch, roll
D3DXMATRIX mInvView;
D3DXMatrixInverse( &mInvView, NULL, &mRotView );
// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
// To figure out the yaw/pitch of the camera, we just need the Z basis vector
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &mInvView._31;
float fCameraYawAngle = atan2f( pZBasis->x, pZBasis->z );
float fLen = sqrtf(pZBasis->z*pZBasis->z + pZBasis->x*pZBasis->x);
float fCameraPitchAngle = -atan2f( pZBasis->y, fLen );
三、雲臺pan-tilt-zoom
雲臺 支持 pan, tilt, zoom,focus 四個參數,常用前三個,簡稱PTZ.
雲臺一般只能 水平(pan),上下(tilt)動作。
不支持旋轉。
對於Pan, tilt 構造 旋轉矩陣 次序
1. tilt[全局 x 軸] -> Pan [全局軸 y ]
2. Pan [全局軸 y軸] -> tilt [自身軸 x 軸]
和 yaw pitch roll 概念, 完全相同。
四、補充
1. 對於旋轉矩陣, 它的逆矩陣 和 轉置矩陣 是相同。 所以可以用 旋轉矩陣 直接 變換 方向矢量。
2. 視角矩陣 雖然 和 yaw-pitch-roll 不同, 但是其實就是 逆(或轉置) 的關係。
3. 如果一個 先yaw, 後pitch的視角矩陣 (全局軸) ==> 計算 yaw, pitch , 見下式。
D3DXMATRIX mInvView;
D3DXMatrixInverse( &mInvView, NULL, &mRotView );
// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
// To figure out the yaw/pitch of the camera, we just need the Z basis vector
D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &mInvView._31;
float fYawFirst_PitchAngle = -atan2f( pZBasis->y, pZBasis->z );
float fYawFirst_Len = sqrtf(pZBasis->z*pZBasis->z + pZBasis->y*pZBasis->y);
float fYawFirst_YawAngle = atan2f( pZBasis->x, fYawFirst_Len );