Share via


Fonction XMVector4Normalize (directxmath.h)

Retourne la version normalisée d’un vecteur 4D.

Syntaxe

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

Paramètres

[in] V

Vecteur 4D.

Valeur retournée

Retourne la version normalisée de V.

Remarques

Pour un vecteur de longueur 0, cette fonction retourne un vecteur zéro. Pour un vecteur de longueur infinie, il retourne un vecteur de QNaN.

Notez que pour la plupart des applications graphiques, il est courant de s’assurer que les vecteurs ont des longueurs bien définies qui ne posent pas de problèmes de normalisation. Toutefois, si vous avez besoin d’une normalisation robuste qui fonctionne pour toutes les entrées à virgule flottante, vous pouvez utiliser le code suivant à la place :


inline XMVECTOR XMVector4NormalizeRobust( FXMVECTOR V )
{
    // Compute the maximum absolute value component.
    XMVECTOR vAbs = XMVectorAbs(V);
    XMVECTOR max0 = XMVectorSplatX(vAbs);
    XMVECTOR max1 = XMVectorSplatY(vAbs);
    XMVECTOR max2 = XMVectorSplatZ(vAbs);
    XMVECTOR max3 = XMVectorSplatW(vAbs);
    max0 = XMVectorMax(max0, max1);
    max2 = XMVectorMax(max2, max3);
    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);

    // (sqrLength, sqrLength, sqrLength, sqrLength)
    XMVECTOR t0 = XMVector4Dot(normalized, normalized);

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

Configuration requise pour la plateforme

Microsoft Visual Studio 2010 ou Microsoft Visual Studio 2012 avec le Kit de développement logiciel (SDK) Windows pour Windows 8. Pris en charge pour les applications de bureau Win32, les applications du Windows Store et Windows Phone 8 applications.

Configuration requise

Condition requise Valeur
Plateforme cible Windows
En-tête directxmath.h (inclure DirectXMath.h)

Voir aussi

Fonctions géométriques vectorielles 4D de la bibliothèque DirectXMath

XMVector4NormalizeEst