DirectX11 XNA数学库之矩阵

XNA数学库之矩阵
1. XNA数学库之矩阵介绍
跟XNA向量一样为了使用SSE2优化指令,XNA矩阵XMMATRIX的实现使用了四个向量。

2. 矩阵类型
XMMATRIX的定义如下:

//
Matrix type: Sixteen 32 bit floating point components aligned on a
    //
    16 byte boundary and mapped to four hardware vector registers
#if (defined(_XM_X86_) || defined(_XM_X64_)) && defined(_XM_NO_INTRINSICS_)
    typedef struct _XMMATRIX
#else
    typedef _DECLSPEC_ALIGN_16_ struct _XMMATRIX
#endif
{
    union
    {//
        Use 4 XMVECTORs to represent the matrix for SIMD.
            XM
            VE
            CTOR r[4];
        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];
    };
#ifdef __cplusplus
    _XMMATRIX() {};
    //
    Initialize matrix by specifying 4 row vectors.
        _
        XM
        MATRIX(FXMVECTOR R0, FXMVECTOR R1, FXMVECTOR R2, CXMVECTOR R3);
    //
    Initialize matrix by specifying the 16 elements.
        _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);
    //
    Pass array of sixteen floats to construct matrix.
        _XMMATRIX(CONST FLOAT *pArray);
    FLOAT operator() (UINT Row, UINT Column) CONST { return m[Row][Column]; }
    FLOAT& operator() (UINT Row, UINT Column) { return m[Row][Column]; }
    _XMMATRIX& operator= (CONST _XMMATRIX& M);
#ifndef XM_NO_OPERATOR_OVERLOADS
    _XMMATRIX& operator*= (CONST _XMMATRIX& M);
    _XMMATRIX operator* (CONST _XMMATRIX& M) CONST;
#endif // !XM_NO_OPERATOR_OVERLOADS
#endif // __cplusplus
} XMMATRIX;

除了使用上面不同的构造函数,你还可以使用:

XMMATRIX XMMatrixSet(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);

通样地,我们使用XMMATRIX来计算,使用XMFLOAT4X4来储存,XMFLOAT4X4的定义如下:

//
4x4 Matrix: 32 bit floating point components
    typedef 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];
    };
#ifdef __cplusplus
    _
        XM
        FLOAT4X4() {};
    _
        XM
        FLOAT4X4(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);
    _XMFLOAT4X4(CONST FLOAT *pArray);
    FLOAT operator() (UINT Row, UINT Column) CONST { return m[Row][Column]; }
    FLOAT& operator() (UINT Row, UINT Column) { return m[Row][Column]; }
    _XMFLOAT4X4& operator= (CONST _XMFLOAT4X4& Float4x4);
#endif // __cplusplus
} XMFLOAT4X4;

3. 矩阵函数
矩阵使用如下计算函数:

MMATRIX XMMatrixIdentity(); // Returns the identity matrix I
BOOL XMMatrixIsIdentity( // Returns true if M is the identity matrix
    CXMMATRIX M); // Input M
XMMATRIX XMMatrixMultiply( // Returns the matrix product AB
    CXMMATRIX A, // Input A
    CXMMATRIX B); // Input B
XMMATRIX XMMatrixTranspose( // Returns Mτ
    CXMMATRIX M); // Input M
XMVECTOR XMMatrixDeterminant( // Returns (det M, det M, det M, det M)
    CXMMATRIX M); // Input M
XMMATRIX XMMatrixInverse( // Returns M1
XMVECTOR* pDeterminant, // Input (det M, det M, det M, det M)
    CXMMATRIX M); // Input M

XMMATRIX作为形参的时候,应该使用XMMATRIX来使得平台具有兼容性。

3. 矩阵变换函数

// Constructs a scaling matrix:
XMMATRIX XMMatrixScaling(
FLOAT ScaleX,
FLOAT ScaleY,
FLOAT ScaleZ); // Scaling factors
// Constructs a scaling matrix from components in vector:
XMMATRIX XMMatrixScalingFromVector(
FXMVECTOR Scale); // Scaling factors (sx , sy , sz)
// Constructs a x-axis rotation matrix : Rx
XMMATRIX XMMatrixRotationX(
FLOAT Angle); // Clockwise angle θ to rotate
// Constructs a y-axis rotation matrix : Ry
XMMATRIX XMMatrixRotationY(
FLOAT Angle); // Clockwise angle θ to rotate
// Constructs a z-axis rotation matrix : Rz
XMMATRIX XMMatrixRotationZ(
FLOAT Angle); // Clockwise angle θ to rotate
// Constructs an arbitrary axis rotation matrix : Rn
XMMATRIX XMMatrixRotationAxis(
FXMVECTOR Axis,
FLOAT Angle);
//Axis n to rotate about
//Clockwise angle θ to rotate Constructs a translation matrix:
XMMATRIX XMMatrixTranslation(
FLOAT OffsetX,
FLOAT OffsetY,
FLOAT OffsetZ); // Translation factors Constructs a translation matrix from components in a vector:
XMMATRIX XMMatrixTranslationFromVector(
FXMVECTOR Offset); // Translation factors (tx , ty ,tz)
// Computes the vector-matrix product vM:
XMVECTOR XMVector3Transform(
FXMVECTOR V,
CXMMATRIX M);
//Input v
//Input M
// Computes the vector-matrix product vM where vw = 1 for transforming points:
XMVECTOR XMVector3TransformCoord(
FXMVECTOR V,
CXMMATRIX M);
//Input v
//Input M
// Computes the vector-matrix product vM where vw = 0 for transforming vectors:
XMVECTOR XMVector3TransformNormal(
FXMVECTOR V,
CXMMATRIX M);
//Input v
//Input M
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值