DirectX:Matrix

Blog under Tag DirectX DirectX is mainly used for recording the learning process, the main reference "DirectX 12 3D games combat development."

Matrix in DirectX

3D mathematical description is the most important transformation geometry, which will certainly involve matrix. Benpian main content that is related to the matrix DirectXMath library part.

matrix

First, review the following characteristics matrix operations.

  • Matrix addition is commutative and associative
  • Matrices A, B are multiplied meaningful if and only if the number of columns A and B of the same number of rows
  • Matrix multiplication is generally not commutative
  • Scalar multiplication of matrix addition, matrix multiplication distributes over the scalar adder
  • Vector matrix multiplication is actually a linear combination of
  • I.e., the main diagonal matrix are a remainder of zero matrix
  • Phalanx phalanx reversible if and only if the determinant is not equal to zero
  • On a square matrix row i and column j of the matrix cofactor i.e. removing the i-th row and the rest j-th column of the matrix configuration, i.e. cofactor i on the basis of the sub-formulas I-1 + j times superior square
  • The matrix row i and column j cofactor are placed on the corresponding row and column matrix is ​​referred to as cofactor square matrices, the matrix is ​​the transpose of the matrix accompanied cofactor matrix
  • Square inverse matrix for the matrix divided by the square accompanied by the determinant

DirectXMath library matrix type

In 3D graphics, typically it involves up to 4-dimensional content, corresponding to the 4-dimensional vector and matrix of 4 * 4. DirectXMath Library provides XMMATRIXtypes to represent matrix.

#if (defined(_M_IX86) || defined(_M_X64) || defined(_M_ARM)) && defined(_XM_NO_INTRINSICS)
struct XMMATRIX
#else
__declspec(align(16)) struct XMMATRIX
#endif
{
    XMVECTOR R[4];
    
    XMMATRIX() {}
    
    XMMATRIX(FXMVECTOR R0, FXMVECTOR R1, FXMVECTOR R2, CXMVECTOR R3)
    { r[0] = R0; r[1] = R1; r[2] = R2; r[3] = R3; }
    
    XMMATRIX(float m00, float m01, float m02, float m03,
            float m10, float m11, float m12, float m13,
            float m20, float m21, float m22, float m23,
            float m30, float m31, float m32, float m33);
    
    explicit XMMATRIX(_In_reads_(16) const float *pArray);
    
    XMMATRIX& operator= (const XMMATRIX& M)
    { r[0] = M.r[0]; r[1] = M.r[1]; r[2] = M.r[2]; r[3] = M.r[3];
    return *this; }
}

Can be seen, DirectXMath library structure XMMATRIXrepresented by a matrix, including four 4-dimensional vectors XMVECTOR, whereby a SIMD. When the matrix configuration, can be generated by another matrix, it may be 4 dimensional vectors by 4 configuration, by a further array of length 16 or 16 directly floatconfiguration.

Load / Store

And XMFLOATnthe like, but also the matrix corresponds to a storage structure XMFLOAT4X4.

struct XMFLOAT4X4
{
    union
    {
        struct
        {
            float _11, _12, _13, _14;
            float _21, _22, _23, _24;
            float _31, _32, _33, _34;
            float _41, _42, _43, _44;
        };
        float m[4][4];
    };
    
    XMFLOAT4X4() {}
    
    XMFLOAT4X4(float m00, float m01, float m02, float m03,
            float m10, float m11, float m12, float m13,
            float m20, float m21, float m22, float m23,
            float m30, float m31, float m32, float m33);
    
    explicit XMFLOAT4X4(_In_reads_(16) const float *pArray);
    
    float operator() (size_t Row, size_t Column) const
    { return m[Row][Column]; }
    
    float& operator() (size_t Row, size_t Column)
    { return m[Row][Column]; }
    
    XMFLOAT4X4& operator= (const XMFLOAT4X4& Float4x4);
};

The following methods can be used to load / store matrix.

inline XMMATRIX XM_CALLCONV XMLoadFloat4x4(const XMFLOAT4X4* pSource);
inline void XM_CALLCONV XMStoreFloat4x4(XMFLOAT4X4* pDestination, FXMMATRIX M);

Parameter passing

It has said in a statement XMMATRIXwhen the type of function, should be a XMMATRIXdeemed four XMVECTOR. In the Incoming XMVECTORwhen no more than two, the first one XMMATRIXshould be declared FXMVECTOR, the rest are CXMMATRIX. These same conventions by the annotation XM_CALLCONVsupport. Similarly, the constructor is a special case, always using the constructor CXMMATRIXtype acquisition XMMATRIXparameters, and do not use XM_CALLCONVannotations.

// 在32位的Windows系统上,XM_CALLCONV转化为__fastcall调用约定
// __fastcall调用约定通过寄存器传递前3个XMVECTOR参数,但由于
// 1个XMMATRIX类型包含4个XMVECTOR,故此时所有的XMMATRIX都只能通过堆栈引用
typedef const XMMATRIX& FXMMATRIX;
typedef const XMMATRIX& CXMMATRIX;

Operator overloading

XMMATRIXIt provides information about the type of matrix operation Operator.

XMMATRIX operator+ () const { return *this; }
XMMATRIX operator- () const;

XMMATRIX& XM_CALLCONV operator+= (FXMMATRIX M);
XMMATRIX& XM_CALLCONV operator-= (FXMMATRIX M);
XMMATRIX& XM_CALLCONV operator*= (FXMMATRIX M);

XMMATRIX& operator*= (float S);
XMMATRIX& operator/= (float S);

XMMATRIX XM_CALLCONV operator+ ( FXMMATRIX M) const;
XMMATRIX XM_CALLCONV operator- ( FXMMATRIX M) const;
XMMATRIX XM_CALLCONV operator* ( FXMMATRIX M) const;

XMMATRIX& operator*= (float S) const;
XMMATRIX& operator/= (float S) const;

friend XMMATRIX XM_CALLCONV operator* (float S, FXMMATRIX M);

Matrix Functions

DirectXMath library also contains a matrix of nature-related utility functions.

// 单位矩阵
XMMATRIX XM_CALLCONV XMMatrixIdentity();

// 判断是否是单位矩阵
bool XM_CALLCONV XMMatrixIsIdentity(FXMMATRIX M);

// 矩阵乘法
XMMATRIX XM_CALLCONV XMMatrixMultiply(FXMMATRIX A, CXMMATRIX B);

// 求转置
XMMATRIX XM_CALLCONV XMMatrixTranspose(FXMMATRIX M);

// 求行列式,返回的XMVECTOR的每一个分量都是行列式值
XMVECTOR XM_CALLCONV XMMatrixDeterminant(FXMMATRIX M);

// 求逆矩阵,其中输入的pDeterminant为M的行列式
XMMATRIX XM_CALLCONV XMMatrixInverse(XMVECTOR* pDeterminant, FXMMATRIX M);

Guess you like

Origin www.cnblogs.com/Li-F/p/11494776.html