DirectX9 矩阵

D3DXMatrices

To represent 4×4 matrices in D3DX, weuse the D3DXMATRIX class, defined as follows:

typedef struct D3DXMATRIX : publicD3DMATRIX

{

public:

D3DXMATRIX() {};

D3DXMATRIX(CONST FLOAT*);

D3DXMATRIX(CONST D3DMATRIX&);

D3DXMATRIX(FLOAT _11, FLOAT _12, FLOAT_13, FLOAT _14,

FLOAT _21, FLOAT _22, FLOAT _23, FLOAT_24,

FLOAT _31, FLOAT _32, FLOAT _33, FLOAT_34,

FLOAT _41, FLOAT _42, FLOAT _43, FLOAT_44);

// access grants

FLOAT& operator () (UINT Row, UINTCol);

FLOAT operator () (UINT Row, UINT Col)const;

// casting operators

operator FLOAT* ();

operator CONST FLOAT* () const;

// assignment operators

D3DXMATRIX& operator *= (CONSTD3DXMATRIX&);

D3DXMATRIX& operator += (CONSTD3DXMATRIX&);

D3DXMATRIX& operator -= (CONSTD3DXMATRIX&);

D3DXMATRIX& operator *= (FLOAT);

D3DXMATRIX& operator /= (FLOAT);

// unary operators

D3DXMATRIX operator + () const;

D3DXMATRIX operator - () const;

// binary operators

D3DXMATRIX operator * (CONSTD3DXMATRIX&) const;

D3DXMATRIX operator + (CONSTD3DXMATRIX&) const;

D3DXMATRIX operator - (CONSTD3DXMATRIX&) const;

D3DXMATRIX operator * (FLOAT) const;

D3DXMATRIX operator / (FLOAT) const;

friend D3DXMATRIX operator * (FLOAT,CONST D3DXMATRIX&);

BOOL operator == (CONST D3DXMATRIX&)const;

BOOL operator != (CONST D3DXMATRIX&)const;

} D3DXMATRIX, *LPD3DXMATRIX;

 

TheD3DXMATRIXclass inherits its dataentries from the simpler D3DMATRIX structure,which is defined as:

typedef struct _D3DMATRIX {

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];

};

} D3DMATRIX;

 

Because matrixmultiplication is so important, we give a code example of using thisoperator:

D3DXMATRIX A(…); // initialize A

D3DXMATRIX B(…); // initialize B

D3DXMATRIXC=A*B;//C=AB

 

Note that when using the parenthesisoperator, we index starting into the matrix starting at zero like a C-array.For example, to access entryij= 11 of a matrix, we wouldwrite:

D3DXMATRIX M;

M(0, 0) = 5.0f; // Set entryij= 11 to 5.0f.

 

The D3DX library also provides thefollowing useful functions that set a D3DXMATRIXto the identity matrix,take the transpose of a D3DXMATRIX, and find the inverse of aD3DXMATRIX:

D3DXMATRIX *D3DXMatrixIdentity(

D3DXMATRIX *pout // The matrix to be setto the identity.

);

D3DXMATRIX M;

D3DXMatrixIdentity( &M ); // M =identity matrix

D3DXMATRIX *D3DXMatrixTranspose(

D3DXMATRIX *pOut, // The resultingtransposed matrix.

CONST D3DXMATRIX *pM // The matrix totake the transpose of.

);

D3DXMATRIX A(...); // initialize A

D3DXMATRIX B;

D3DXMatrixTranspose( &B, &A ); //B = transpose(A)

D3DXMATRIX *D3DXMatrixInverse(

D3DXMATRIX *pOut, // returns inverse ofpM

FLOAT *pDeterminant, // determinant, ifrequired, else pass 0

CONST D3DXMATRIX *pM // matrix to invert

);

 

Theinverse function returns null if the matrix that we are trying to

invertdoes not have an inverse. Also, for this book we can ignore the

secondparameter and set it to 0 every time.

D3DXMATRIX A(...); // initialize A

D3DXMATRIX B;

D3DXMatrixInverse( &B, 0, &A );// B = inverse(A)

 

The D3DX function to build a translation matrix is:

D3DXMATRIX *D3DXMatrixTranslation(

D3DXMATRIX* pOut, // Result.

FLOAT x, // Number of units to translateon x-axis.

FLOAT y, // Number of units to translateon y-axis.

FLOAT z // Number of units to translateon z-axis.

);

 

The D3DX function to build an x-axis rotation matrix is:

D3DXMATRIX *D3DXMatrixRotationX(

D3DXMATRIX* pOut, // Result.

FLOAT Angle // Angle of rotation measuredin radians.

);

 

The D3DX function to build a y-axis rotation matrix is:

D3DXMATRIX *D3DXMatrixRotationY(

D3DXMATRIX* pOut, // Result.

FLOAT Angle // Angle of rotation measuredin radians.

);

 

The D3DX function to build a z-axis rotation matrix is:

D3DXMATRIX *D3DXMatrixRotationZ(

D3DXMATRIX* pOut, // Result.

FLOAT Angle // Angle of rotation measuredin radians.

);

 

The D3DX function to build a scaling matrix is:

D3DXMATRIX *D3DXMatrixScaling(

D3DXMATRIX* pOut, // Result.

FLOAT sx, // Number of units to scale onthe x-axis.

FLOAT sy, // Number of units to scale onthe y-axis.

FLOAT sz // Number of units to scale onthe z-axis.

);

 

The D3DXVec3TransformCoord function transforms points and assumes the fourth component of the vectoris an understood 1. TheD3DXVec3TransformNormal function transforms vectorsand assumes the fourth component of the vector is an understood 0.

D3DXVECTOR3 *D3DXVec3TransformCoord(

D3DXVECTOR3* pOut, // Result.

CONST D3DXVECTOR3* pV, // The point totransform.

CONST D3DXMATRIX* pM // The transformationmatrix.

);

D3DXMATRIX T(...); // initialize atransformation matrix

D3DXVECTOR3 p(...); // initialize a point

D3DXVec3TransformCoord( &p, &p,&T); // transform the point

D3DXVECTOR3 *WINAPID3DXVec3TransformNormal(

D3DXVECTOR3 *pOut, // Result.

CONST D3DXVECTOR3 *pV, // The vector totransform.

CONST D3DXMATRIX *pM // Thetransformation matrix.

);

D3DXMATRIX T(...); // initialize atransformation matrix

D3DXVECTOR3 v(...); // initialize avector

D3DXVec3TransformNormal( &v, &v,&T); // transform the vector

 

Note:The D3DX library also providesD3DXVec3TransformCoordArray and D3DXVec3TransformNormalArrayfor transforming an array of points and an array of vectors, respectively.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值