DirectX9 向量

In the D3DX library, we can use the D3DXVECTOR3 class to represent a vectorin 3-space. Its class definition is:

typedef struct D3DXVECTOR3 : public D3DVECTOR {

public:

D3DXVECTOR3() {};

D3DXVECTOR3( CONST FLOAT * );

D3DXVECTOR3( CONST D3DVECTOR& );

D3DXVECTOR3( FLOAT x, FLOAT y, FLOAT z );

// casting

operator FLOAT* ();

operator CONST FLOAT* () const;

// assignment operators

D3DXVECTOR3& operator += ( CONST D3DXVECTOR3& );

D3DXVECTOR3& operator -= ( CONST D3DXVECTOR3& );

D3DXVECTOR3& operator *= ( FLOAT );

D3DXVECTOR3& operator /= ( FLOAT );

// unary operators

D3DXVECTOR3 operator + () const;

D3DXVECTOR3 operator - () const;

// binary operators

D3DXVECTOR3 operator + ( CONST D3DXVECTOR3& ) const;

D3DXVECTOR3 operator - ( CONST D3DXVECTOR3& ) const;

D3DXVECTOR3 operator * ( FLOAT ) const;

D3DXVECTOR3 operator / ( FLOAT ) const;

friend D3DXVECTOR3 operator * ( FLOAT,

CONST struct D3DXVECTOR3& );

BOOL operator == ( CONST D3DXVECTOR3& ) const;

BOOL operator != ( CONST D3DXVECTOR3& ) const;

} D3DXVECTOR3, *LPD3DXVECTOR3;

 

Note thatD3DXVECTOR3 inherits its component data from D3DVECTOR, whichis defined as:

typedef struct _D3DVECTOR {

float x;

float y;

float z;

} D3DVECTOR;

 

In code we can test if two vectors are equal using the overloaded equalsoperator:

D3DXVECTORu(1.0f, 0.0f, 1.0f);

D3DXVECTORv(0.0f, 1.0f, 0.0f);

if(u == v ) return true;

Similarly, we can test if two vectors are not equal using theoverloaded not equals operator:

if(u != v ) return true;

 

Using the D3DX library, we can compute the magnitude of a vector usingthe following function:

FLOATD3DXVec3Length( // Returns the magnitude.

CONSTD3DXVECTOR3* pV // The vector to compute the length of.

);

D3DXVECTOR3v(1.0f, 2.0f, 3.0f);

floatmagnitude = D3DXVec3Length( &v ); // = sqrt(14)

 

Using the D3DX library, we can normalize a vector using the following function:

D3DXVECTOR3*D3DXVec3Normalize(

D3DXVECTOR3*pOut, // Result.

CONSTD3DXVECTOR3* pV // The vector to normalize.

);

 

To add two vectors in code, we use the overloaded addition operator:

D3DXVECTOR3u(2.0f, 0.0f, 1.0f);

D3DXVECTOR3v(0.0f, -1.0f, 5.0f);

//(2.0 + 0.0, 0.0 + (-1.0), 1.0 + 5.0)

D3DXVECTOR3sum=u+v;//=(2.0f, -1.0f, 6.0f)

 

To subtract two vectors in code, we use the overloaded subtraction operator:

D3DXVECTOR3u(2.0f, 0.0f, 1.0f);

D3DXVECTOR3v(0.0f, -1.0f, 5.0f);

D3DXVECTOR3difference=u-v;//=(2.0f, 1.0f, -4.0f)

 

TheD3DXVECTOR3class provides a scalar multiplication operator:

D3DXVECTOR3u(1.0f, 1.0f, -1.0f);

D3DXVECTOR3scaledVec=u*10.0f; // = (10.0f, 10.0f, -10.0f)

 

We use the following D3DX function to compute the dot product betweentwo vectors:

FLOATD3DXVec3Dot( // Returns the result.

CONSTD3DXVECTOR3* pV1, // Left sided operand.

CONSTD3DXVECTOR3* pV2 // Right sided operand.

);

D3DXVECTOR3u(1.0f, -1.0f, 0.0f);

D3DXVECTOR3v(3.0f, 2.0f, 1.0f);

//1.0*3.0 + -1.0*2.0 + 0.0*1.0

//= 3.0 + -2.0

floatdot = D3DXVec3Dot( &u, &v ); // = 1.0

 

We use the following D3DX function to compute the cross product betweentwo vectors:

D3DXVECTOR3*D3DXVec3Cross(

D3DXVECTOR3*pOut, // Result.

CONSTD3DXVECTOR3* pV1, // Left sided operand.

CONSTD3DXVECTOR3* pV2 // Right sided operand.

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值