XMVector3Normalize 函式 (directxmath.h)

傳回 3D 向量的標準化版本。

語法

XMVECTOR XM_CALLCONV XMVector3Normalize(
  [in] FXMVECTOR V
) noexcept;

參數

[in] V

3D 向量。

傳回值

傳回 V 的正規化版本。

備註

對於長度為 0 的向量,此函式會傳回零向量。 對於長度無限的向量,它會傳回 QNaN 的向量。

請注意,對於大部分圖形應用程式,確保向量具有定義完善的長度,不會造成正規化的問題是常見的作法。 不過,如果您需要適用于所有浮點輸入的健全正規化,您可以改用下列程式碼:


inline XMVECTOR XMVector3NormalizeRobust( FXMVECTOR V )
{
    // Compute the maximum absolute value component.
    XMVECTOR vAbs = XMVectorAbs(V);
    XMVECTOR max0 = XMVectorSplatX(vAbs);
    XMVECTOR max1 = XMVectorSplatY(vAbs);
    XMVECTOR max2 = XMVectorSplatZ(vAbs);
    max0 = XMVectorMax(max0, max1);
    max0 = XMVectorMax(max0, max2);

    // Divide by the maximum absolute component.
    XMVECTOR normalized = XMVectorDivide(V, max0);

    // Set to zero when the original length is zero.
    XMVECTOR mask = XMVectorNotEqual(g_XMZero, max0);
    normalized = XMVectorAndInt(normalized, mask);

    XMVECTOR t0 = XMVector3LengthSq(normalized);
    XMVECTOR length = XMVectorSqrt(t0);

    // Divide by the length to normalize.
    normalized = XMVectorDivide(normalized, length);

    // Set to zero when the original length is zero or infinity.  In the
    // latter case, this is considered to be an unexpected condition.
    normalized = XMVectorAndInt(normalized, mask);
    return normalized;
}
    

平臺需求

Microsoft Visual Studio 2010 或 Microsoft Visual Studio 2012 搭配 Windows SDK for Windows 8。 支援 Win32 傳統型應用程式、Windows 市集應用程式和 Windows Phone 8 個應用程式。

需求

   
目標平台 Windows
標頭 directxmath.h (包含 DirectXMath.h)

另請參閱

DirectXMath 程式庫 3D 向量幾何函數

XMVector3NormalizeEst