# 使用矩阵和四元数实现三维模型的空间定位

通过矩阵变换实现绕三个坐标轴的特定角度的旋转:

//-----------------------------------------------------------------------------
// Desc: 设置世界矩阵
//-----------------------------------------------------------------------------
VOID SetWorldMatrix()
{
static long curTime=0;
static float elapsetime=0;
elapsetime = (timeGetTime()-curTime)/1000.0f;
curTime = timeGetTime();

//创建并设置世界矩阵
float fRoll, fPitch, fYaw;
fRoll = fPitch = fYaw = 0.0f;

if (m_bKey['D']) fRoll  -= 3*elapsetime;
if (m_bKey['A']) fRoll  += 3*elapsetime;

if (m_bKey['S']) fPitch -= 3*elapsetime;
if (m_bKey['W']) fPitch += 3*elapsetime;

if (m_bKey['Q']) fYaw   -= 3*elapsetime;
if (m_bKey['E']) fYaw   += 3*elapsetime;

//更新网格模型姿态
static D3DXVECTOR3 vRight, vUp, vLook, vPos;

vRight.x = g_matWorld._11;
vRight.y = g_matWorld._12;
vRight.z = g_matWorld._13;
vUp.x    = g_matWorld._21;
vUp.y    = g_matWorld._22;
vUp.z    = g_matWorld._23;
vLook.x  = g_matWorld._31;
vLook.y  = g_matWorld._32;
vLook.z  = g_matWorld._33;
vPos.x   = g_matWorld._41;
vPos.y   = g_matWorld._42;
vPos.z   = g_matWorld._43;

D3DXVec3Normalize(&vLook, &vLook);
D3DXVec3Cross(&vRight, &vUp, &vLook);

D3DXVec3Normalize(&vRight, &vRight);
D3DXVec3Cross(&vUp, &vLook, &vRight);

D3DXVec3Normalize(&vUp, &vUp);

static D3DXMATRIX matPitch, matYaw, matRoll;

D3DXMatrixRotationAxis(&matYaw, &vUp, fYaw);
D3DXVec3TransformCoord(&vLook,  &vLook, &matYaw);
D3DXVec3TransformCoord(&vRight, &vRight, &matYaw);

D3DXMatrixRotationAxis(&matRoll, &vLook, fRoll);
D3DXVec3TransformCoord(&vRight, &vRight, &matRoll);
D3DXVec3TransformCoord(&vUp,    &vUp, &matRoll);

D3DXMatrixRotationAxis(&matPitch, &vRight, fPitch);
D3DXVec3TransformCoord(&vLook, &vLook, &matPitch);
D3DXVec3TransformCoord(&vUp,   &vUp,  &matPitch);

g_matWorld._11 = vRight.x;
g_matWorld._12 = vRight.y;
g_matWorld._13 = vRight.z;
g_matWorld._21 = vUp.x ;
g_matWorld._22 = vUp.y  ;
g_matWorld._23 = vUp.z;
g_matWorld._31 = vLook.x;
g_matWorld._32 = vLook.y;
g_matWorld._33 = vLook.z;

//向前移动
if (m_bKey['F'])
{
g_matWorld._41 += 30*elapsetime * vLook.x;
g_matWorld._42 += 30*elapsetime * vLook.y;
g_matWorld._43 += 30*elapsetime * vLook.z;
}

//向后移动
if (m_bKey['V'])
{
g_matWorld._41 -= 30*elapsetime * vLook.x;
g_matWorld._42 -= 30*elapsetime * vLook.y;
g_matWorld._43 -= 30*elapsetime * vLook.z;
}
g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
}

通过四元数实现绕三个坐标轴的特定角度的旋转:

//-----------------------------------------------------------------------------
// Desc: 设置世界矩阵
//-----------------------------------------------------------------------------
VOID SetWorldMatrix()
{
static long curTime=0;
static float elapsetime=0;
elapsetime = (timeGetTime()-curTime)/1000.0f;
curTime = timeGetTime();

//创建并设置世界矩阵
float fRoll, fPitch, fYaw;
fRoll = fPitch = fYaw = 0.0f;

if (m_bKey['D']) fRoll  -= 3*elapsetime;
if (m_bKey['A']) fRoll  +=  3*elapsetime;
if (m_bKey['S']) fPitch -= 3*elapsetime;
if (m_bKey['W']) fPitch += 3*elapsetime;
if (m_bKey['Q']) fYaw   -= 3*elapsetime;
if (m_bKey['E']) fYaw   += 3*elapsetime;

//更新网格模型姿态
D3DXQUATERNION qR;
D3DXMATRIX matRot;
D3DXQuaternionRotationYawPitchRoll (&qR, fYaw, fPitch, fRoll);
D3DXMatrixRotationQuaternion (&matRot, &qR);
D3DXMatrixMultiply (&g_matWorld, &matRot, &g_matWorld);

//获取网格模型前向量
static D3DXVECTOR3 vLook;
vLook.x = g_matWorld._31;
vLook.y = g_matWorld._32;
vLook.z = g_matWorld._33;

//向前移动
if (m_bKey['F'])
{
g_matWorld._41 += 10*elapsetime * vLook.x;
g_matWorld._42 += 10*elapsetime * vLook.y;
g_matWorld._43 += 10*elapsetime * vLook.z;
}

//向后移动
if (m_bKey['V'])
{
g_matWorld._41 -= 10*elapsetime * vLook.x;
g_matWorld._42 -= 10*elapsetime * vLook.y;
g_matWorld._43 -= 10*elapsetime * vLook.z;
}

g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
}

|
2月前
|

84 1
|
2月前
|

48 0
|
2月前
|

78 0
|
2月前
|

56 0
|
2月前
|

【PACS】医学影像管理系统源码带三维重建后处理技术
【PACS】医学影像管理系统源码带三维重建后处理技术
85 0
|
2月前
|
C++
【C++医学影像PACS】CT检查中的三维重建是什么检查？
【C++医学影像PACS】CT检查中的三维重建是什么检查？
97 0
|
2月前
|

142 3
|
2月前
|

【C++】医学影像PACS三维重建后处理系统源码

47 2
|

123 0
|
2月前
|

32 1