簡體   English   中英

DX9 中 LookAt 矩陣計算需要說明

[英]Clarification needed on LookAt matrix calculation in DX9

我將通過這兩種方法來計算觀察矩陣

  1. D3DXMatrixLookAtLH

zaxis = normal(At - Eye)

xaxis = 正常(交叉(向上,zaxis))

yaxis = cross(zaxis, xaxis)

xaxis.x yaxis.x zaxis.x 0

xaxis.y yaxis.y zaxis.y 0

xaxis.z yaxis.z zaxis.z 0

-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1

  1. D3DXMatrixLookAtRH

zaxis = 正常(眼睛 - 在)

xaxis = 正常(交叉(向上,zaxis))

yaxis = cross(zaxis, xaxis)

xaxis.x yaxis.x zaxis.x 0

xaxis.y yaxis.y zaxis.y 0

xaxis.z yaxis.z zaxis.z 0

點(x軸,眼睛)點(y軸,眼睛)點(z軸,眼睛)1

為什么 RH 的翻譯不乘以 -1?

TL;DR: D3DXMatrixLookAtRH的 Microsoft Doc 頁面是錯誤的。 我已經提交了PR來修復它。

在 D3DXMath 的實際實現中,很明顯,在這兩種情況下,LH 和 RH 的 Translation 都應該是-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye)

唯一的區別是zaxis計算。

D3DXMATRIX* WINAPI D3DXMatrixLookAtRH
    ( D3DXMATRIX *pOut, const D3DXVECTOR3 *pEye, const D3DXVECTOR3 *pAt,
      const D3DXVECTOR3 *pUp )
{
    D3DXVECTOR3 XAxis, YAxis, ZAxis;

    // Compute direction of gaze. (-Z)
    D3DXVec3Subtract(&ZAxis, pEye, pAt);
    D3DXVec3Normalize(&ZAxis, &ZAxis);

    // Compute orthogonal axes from cross product of gaze and pUp vector.
    D3DXVec3Cross(&XAxis, pUp, &ZAxis);
    D3DXVec3Normalize(&XAxis, &XAxis);
    D3DXVec3Cross(&YAxis, &ZAxis, &XAxis);

    // Set rotation and translate by pEye
    pOut->_11 = XAxis.x;
    pOut->_21 = XAxis.y;
    pOut->_31 = XAxis.z;
    pOut->_41 = -D3DXVec3Dot(&XAxis, pEye);

    pOut->_12 = YAxis.x;
    pOut->_22 = YAxis.y;
    pOut->_32 = YAxis.z;
    pOut->_42 = -D3DXVec3Dot(&YAxis, pEye);

    pOut->_13 = ZAxis.x;
    pOut->_23 = ZAxis.y;
    pOut->_33 = ZAxis.z;
    pOut->_43 = -D3DXVec3Dot(&ZAxis, pEye);

    pOut->_14 = 0.0f;
    pOut->_24 = 0.0f;
    pOut->_34 = 0.0f;
    pOut->_44 = 1.0f;


    return pOut;
}

D3DXMATRIX* WINAPI D3DXMatrixLookAtLH
    ( D3DXMATRIX *pOut, const D3DXVECTOR3 *pEye, const D3DXVECTOR3 *pAt,
      const D3DXVECTOR3 *pUp )
{
    D3DXVECTOR3 XAxis, YAxis, ZAxis;

    // Compute direction of gaze. (+Z)
    D3DXVec3Subtract(&ZAxis, pAt, pEye);
    D3DXVec3Normalize(&ZAxis, &ZAxis);

    // Compute orthogonal axes from cross product of gaze and pUp vector.
    D3DXVec3Cross(&XAxis, pUp, &ZAxis);
    D3DXVec3Normalize(&XAxis, &XAxis);
    D3DXVec3Cross(&YAxis, &ZAxis, &XAxis);

    // Set rotation and translate by pEye
    pOut->_11 = XAxis.x;
    pOut->_21 = XAxis.y;
    pOut->_31 = XAxis.z;
    pOut->_41 = -D3DXVec3Dot(&XAxis, pEye);

    pOut->_12 = YAxis.x;
    pOut->_22 = YAxis.y;
    pOut->_32 = YAxis.z;
    pOut->_42 = -D3DXVec3Dot(&YAxis, pEye);

    pOut->_13 = ZAxis.x;
    pOut->_23 = ZAxis.y;
    pOut->_33 = ZAxis.z;
    pOut->_43 = -D3DXVec3Dot(&ZAxis, pEye);

    pOut->_14 = 0.0f;
    pOut->_24 = 0.0f;
    pOut->_34 = 0.0f;
    pOut->_44 = 1.0f;

    return pOut;
}

D3DXMatrixLookAtRHD3DXMatrixLookAtLH都是“D3DXMath”的一部分,該數學庫包含在 D3DX9 和 D3DX10 中。 這些幫助程序庫已被棄用,DirectX SDK 本身也是如此。 建議改用DirectXMath

如果您出於某種原因必須使用 D3DX9,請注意,您可以通過使用Microsoft.DXSDK.D3DX NuGet 來避免混合舊版 DirectX SDK 和現代版本的 Visual Studio 的Microsoft Docs中涵蓋的所有復雜問題。 新項目應該轉移到各種替代品中的任何一個。 有關更多詳細信息,請參閱此博客文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM