您的位置:首页 > 其它

directX 数学基础-矩阵

2013-09-08 13:20 316 查看
//directx9.0在点dx9math.h头文件中定义了对应于4*4的D3DMATRIX结构体和一个提供了4*4矩阵的各种运算处理的D3DMATRIX结构体
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;

typedef struct D3DXMATRIX : public D3DMATRIX
{
public:
    D3DXMATRIX() {};
    D3DXMATRIX( CONST FLOAT * );
    D3DXMATRIX( CONST D3DMATRIX& );
    D3DXMATRIX( CONST D3DXFLOAT16 * );
    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, UINT Col );
    FLOAT  operator () ( UINT Row, UINT Col ) const;

    // casting operators
    operator FLOAT* ();
    operator CONST FLOAT* () const;

    // assignment operators
    D3DXMATRIX& operator *= ( CONST D3DXMATRIX& );
    D3DXMATRIX& operator += ( CONST D3DXMATRIX& );
    D3DXMATRIX& operator -= ( CONST D3DXMATRIX& );
    D3DXMATRIX& operator *= ( FLOAT );
    D3DXMATRIX& operator /= ( FLOAT );

    // unary operators
    D3DXMATRIX operator + () const;
    D3DXMATRIX operator - () const;

    // binary operators
    D3DXMATRIX operator * ( CONST D3DXMATRIX& ) const;
    D3DXMATRIX operator + ( CONST D3DXMATRIX& ) const;
    D3DXMATRIX operator - ( CONST D3DXMATRIX& ) 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;


矩阵加法、减法、乘法

#include<stdio.h>
#include<d3dx9math.h>

int main()
{
	//初始化A矩阵
	D3DXMATRIX A(11.0f,12.0f,13.0f,14.0f,
				 21.0f,22.0f,23.0f,24.0f,
				 31.0f,32.0f,33.0f,34.0f,
				 41.0f,42.0f,43.0f,44.0f
				);
	//初始化B矩阵
	D3DXMATRIX B(-11.0f,-12.0f,-13.0f,-14.0f,
				 -21.0f,-22.0f,-23.0f,-24.0f,
				 -31.0f,-32.0f,-33.0f,-34.0f,
				 -41.0f,-42.0f,-43.0f,-44.0f
				);

	//计算A+B
	D3DXMATRIX C=A+B;
	for(int i=0;i<4;i++)
	{
		printf("%f %f %f %f\n",C(i,0),C(i,1),C(i,2),C(i,3));
	}

	//计算A-B
	D3DXMATRIX D=A-B;
	for(int i=0;i<4;i++)
	{
		printf("%f %f %f %f\n",D(i,0),D(i,1),D(i,2),D(i,3));
	}

	//计算C*D   定义2*3的矩阵C和3*2的矩阵D的乘积为:
	//   ( C11,C12,C13 )     (D11,D12)
	// C=( C21,c22,C23 )   D=(D21.D22)
    //                       (D31,D32)

    //CD=(C11D11+C12D21+C13D31,C11D12+C12D22+C13D32)
    //   (C21D11+C22D21+C23D31,C21D12+C22D22+C23D32)
	D3DXMATRIX E=A*B;
	for(int i=0;i<4;i++)
	{
		printf("%f %f %f %f\n",E(i,0),E(i,1),E(i,2),E(i,3));
	}

	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: