3D 视角旋转矩阵 yaw pitch roll (pan, tilt)的数学计算

一、 yaw pitch roll  含义


  yaw:  水平偏航转角  (绕Y轴旋转,   Y轴向上)

  pitch: 上下俯仰转角 (绕X轴 旋转,   X轴向右)

  roll:    旋转角(绕Z轴 旋转,   Z轴向前)

                                                                      Direct3D 左手座标 


图见:  yaw pitch roll 图




二、旋转矩阵、视角矩阵、yaw-pitch-roll的关系



     1.  D3DXMatrixRotationYawPitchRoll 函数, 计算顺序不是 yaw, pitch, roll .   而是 pitch, yaw。

           注意: 第一个 旋转计算 完毕之后, 第二个旋转的参考轴仍然是全局座标,  而不是自身参考轴。

	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 );





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章