现在的位置: 首页 > 综合 > 正文

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

2013年10月30日 ⁄ 综合 ⁄ 共 4062字 ⁄ 字号 评论关闭

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

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


抱歉!评论已关闭.