directxdirectxmath

How to dump XMMATRIX member value?


Is there a any good way to dump XMMATRIX and XMVECTOR ?

XMMATRIX mx = XMMatrixScaling(...) * XMMatrixTranslation(...);

DumpMatrix(mx); // printf the 4x4 matrix member

I want DumpMatrix like function.

I checked DirectMath.h. And found

typedef __m128 XMVECTOR;

How to extract (x, y, z, w) from __m128 ?


Solution

  • DirectX Math library design doesn't allow direct access to XMMATRIX and XMVECTOR members. It is likely because they store values in special SIMD data types.

    To read the components of an XMVECTOR you can use XMVectorGet* acces functions, for example:

    XMVECTOR V;
    float x = XMVectorGetX(V);
    float w;
    XMVectorGetWPtr(&w, V);
    

    or XMStore* functions to store it into a XMFLOAT4, which has scalar members with direct access:

    XMVECTOR vPosition;
    XMFLOAT4 fPosition;
    XMStoreFloat4(&fPosition, vPosition);
    float x = fPosition.x;
    

    XMMATRIX you can store into a XMFLOAT4X4:

    XMMATRIX mtxView;
    XMFLOAT4X4 fView;
    XMStoreFloat4x4(&fView, mtxView);
    float fView_11 = fView._11;
    

    There are also Load functions to make the opposite: write to XMVECTOR and XMMATRIX.

    For further information, refer to DirectXMath Programming Reference.

    Happy coding!