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

 

None.gif//-----------------------------------------------------------------------------
None.gif// Desc: 设置世界矩阵
None.gif//-----------------------------------------------------------------------------
None.gifVOID SetWorldMatrix()
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif    static long curTime=0;
InBlock.gif    static float elapsetime=0;
InBlock.gif    elapsetime = (timeGetTime()-curTime)/1000.0f;
InBlock.gif    curTime = timeGetTime();
InBlock.gif
InBlock.gif    //创建并设置世界矩阵
InBlock.gif    float fRoll, fPitch, fYaw;
InBlock.gif    fRoll = fPitch = fYaw = 0.0f;
InBlock.gif
InBlock.gif    if (m_bKey['D']) fRoll  -= 3*elapsetime;
InBlock.gif    if (m_bKey['A']) fRoll  += 3*elapsetime;
InBlock.gif
InBlock.gif    if (m_bKey['S']) fPitch -= 3*elapsetime;
InBlock.gif    if (m_bKey['W']) fPitch += 3*elapsetime;
InBlock.gif    
InBlock.gif    if (m_bKey['Q']) fYaw   -= 3*elapsetime;
InBlock.gif    if (m_bKey['E']) fYaw   += 3*elapsetime;
InBlock.gif
InBlock.gif    //更新网格模型姿态
InBlock.gif    static D3DXVECTOR3 vRight, vUp, vLook, vPos;
InBlock.gif
InBlock.gif    vRight.x = g_matWorld._11;
InBlock.gif    vRight.y = g_matWorld._12;
InBlock.gif    vRight.z = g_matWorld._13;
InBlock.gif    vUp.x    = g_matWorld._21;
InBlock.gif    vUp.y    = g_matWorld._22;
InBlock.gif    vUp.z    = g_matWorld._23;
InBlock.gif    vLook.x  = g_matWorld._31;
InBlock.gif    vLook.y  = g_matWorld._32;
InBlock.gif    vLook.z  = g_matWorld._33;
InBlock.gif    vPos.x   = g_matWorld._41;
InBlock.gif    vPos.y   = g_matWorld._42;
InBlock.gif    vPos.z   = g_matWorld._43;
InBlock.gif
InBlock.gif    D3DXVec3Normalize(&vLook, &vLook);
InBlock.gif    D3DXVec3Cross(&vRight, &vUp, &vLook);
InBlock.gif    
InBlock.gif    D3DXVec3Normalize(&vRight, &vRight);
InBlock.gif    D3DXVec3Cross(&vUp, &vLook, &vRight);
InBlock.gif    
InBlock.gif    D3DXVec3Normalize(&vUp, &vUp);
InBlock.gif
InBlock.gif    static D3DXMATRIX matPitch, matYaw, matRoll;
InBlock.gif    
InBlock.gif    D3DXMatrixRotationAxis(&matYaw, &vUp, fYaw);
InBlock.gif    D3DXVec3TransformCoord(&vLook,  &vLook, &matYaw);
InBlock.gif    D3DXVec3TransformCoord(&vRight, &vRight, &matYaw);
InBlock.gif
InBlock.gif    D3DXMatrixRotationAxis(&matRoll, &vLook, fRoll);
InBlock.gif    D3DXVec3TransformCoord(&vRight, &vRight, &matRoll);
InBlock.gif    D3DXVec3TransformCoord(&vUp,    &vUp, &matRoll);
InBlock.gif
InBlock.gif    D3DXMatrixRotationAxis(&matPitch, &vRight, fPitch);
InBlock.gif    D3DXVec3TransformCoord(&vLook, &vLook, &matPitch);
InBlock.gif    D3DXVec3TransformCoord(&vUp,   &vUp,  &matPitch);
InBlock.gif
InBlock.gif    g_matWorld._11 = vRight.x;
InBlock.gif    g_matWorld._12 = vRight.y;
InBlock.gif    g_matWorld._13 = vRight.z;
InBlock.gif    g_matWorld._21 = vUp.x ;
InBlock.gif    g_matWorld._22 = vUp.y  ;
InBlock.gif    g_matWorld._23 = vUp.z;
InBlock.gif    g_matWorld._31 = vLook.x;
InBlock.gif    g_matWorld._32 = vLook.y;
InBlock.gif    g_matWorld._33 = vLook.z;
InBlock.gif
InBlock.gif    //向前移动
InBlock.gif    if (m_bKey['F'])
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        g_matWorld._41 += 30*elapsetime * vLook.x;
InBlock.gif        g_matWorld._42 += 30*elapsetime * vLook.y;
InBlock.gif        g_matWorld._43 += 30*elapsetime * vLook.z;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    //向后移动
InBlock.gif    if (m_bKey['V']) 
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        g_matWorld._41 -= 30*elapsetime * vLook.x;
InBlock.gif        g_matWorld._42 -= 30*elapsetime * vLook.y;
InBlock.gif        g_matWorld._43 -= 30*elapsetime * vLook.z;
ExpandedSubBlockEnd.gif    }
InBlock.gif    g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
ExpandedBlockEnd.gif}
None.gif

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

 

None.gif
None.gif//-----------------------------------------------------------------------------
None.gif// Desc: 设置世界矩阵
None.gif//-----------------------------------------------------------------------------
None.gifVOID SetWorldMatrix()
ExpandedBlockStart.gif ContractedBlock.gif dot.gif{
InBlock.gif    static long curTime=0;
InBlock.gif    static float elapsetime=0;
InBlock.gif    elapsetime = (timeGetTime()-curTime)/1000.0f;
InBlock.gif    curTime = timeGetTime();
InBlock.gif
InBlock.gif    //创建并设置世界矩阵 
InBlock.gif    float fRoll, fPitch, fYaw;
InBlock.gif    fRoll = fPitch = fYaw = 0.0f;
InBlock.gif
InBlock.gif    if (m_bKey['D']) fRoll  -= 3*elapsetime;
InBlock.gif    if (m_bKey['A']) fRoll  +=  3*elapsetime;
InBlock.gif    if (m_bKey['S']) fPitch -= 3*elapsetime;
InBlock.gif    if (m_bKey['W']) fPitch += 3*elapsetime;
InBlock.gif    if (m_bKey['Q']) fYaw   -= 3*elapsetime;
InBlock.gif    if (m_bKey['E']) fYaw   += 3*elapsetime;
InBlock.gif
InBlock.gif    //更新网格模型姿态
InBlock.gif    D3DXQUATERNION qR;
InBlock.gif    D3DXMATRIX matRot;
InBlock.gif    D3DXQuaternionRotationYawPitchRoll (&qR, fYaw, fPitch, fRoll);    
InBlock.gif    D3DXMatrixRotationQuaternion (&matRot, &qR);
InBlock.gif    D3DXMatrixMultiply (&g_matWorld, &matRot, &g_matWorld);
InBlock.gif
InBlock.gif    //获取网格模型前向量
InBlock.gif    static D3DXVECTOR3 vLook;
InBlock.gif    vLook.x = g_matWorld._31;
InBlock.gif    vLook.y = g_matWorld._32;
InBlock.gif    vLook.z = g_matWorld._33;
InBlock.gif
InBlock.gif    //向前移动
InBlock.gif    if (m_bKey['F'])
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        g_matWorld._41 += 10*elapsetime * vLook.x;
InBlock.gif        g_matWorld._42 += 10*elapsetime * vLook.y;
InBlock.gif        g_matWorld._43 += 10*elapsetime * vLook.z;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    //向后移动
InBlock.gif    if (m_bKey['V']) 
ExpandedSubBlockStart.gif ContractedSubBlock.gif     dot.gif{
InBlock.gif        g_matWorld._41 -= 10*elapsetime * vLook.x;
InBlock.gif        g_matWorld._42 -= 10*elapsetime * vLook.y;
InBlock.gif        g_matWorld._43 -= 10*elapsetime * vLook.z;
ExpandedSubBlockEnd.gif    }
InBlock.gif
InBlock.gif    g_pd3dDevice->SetTransform( D3DTS_WORLD, &g_matWorld );
ExpandedBlockEnd.gif}
None.gif
None.gif