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 M−1
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