【D3D Review】D3DX基础数学知识

来源:互联网 发布:淘宝仓库一般要做什么 编辑:程序博客网 时间:2024/05/15 13:26
上周参加了搜狐畅游的笔试,有点鄙视自己了,发现很多东西都忘了,基础非常薄弱,手太低,于是重学D3D和OpenGL。

1.FLOAT D3DXVec3Length( // Returns the magnitude.
CONST D3DXVECTOR3* pV // The vector to compute the length of.
);
D3DXVECTOR3 v(1.0f, 2.0f, 3.0f);
float magnitude = D3DXVec3Length( &v ); // = sqrt(14)   计算向量长度 模

2.归一化向量
D3DXVECTOR3 *D3DXVec3Normalize(
D3DXVECTOR3* pOut, // Result.
CONST D3DXVECTOR3* pV // The vector to normalize.
);
3.向量加
D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);
D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);
// (2.0 + 0.0, 0.0 + (-1.0), 1.0 + 5.0)
D3DXVECTOR3 sum = u + v; // = (2.0f, -1.0f, 6.0f)

4.向量减法
D3DXVECTOR3 u(2.0f, 0.0f, 1.0f);
D3DXVECTOR3 v(0.0f, -1.0f, 5.0f);
D3DXVECTOR3 difference = u - v; // = (2.0f, 1.0f, -4.0f)

5.向量缩放
D3DXVECTOR3 u(1.0f, 1.0f, -1.0f);
D3DXVECTOR3 scaledVec = u * 10.0f; // = (10.0f, 10.0f, -10.0f)

6.向量的点积

U.V = Ux*Vx + Uy*Vy + UzVz = S
U.V = |U||V|cos Theta

如果u · v = 0 则u v垂直;
如果u · v > 0 则u v夹角小于90度;
如果u · v < 0 则u v夹角大于90度。

FLOAT D3DXVec3Dot( // Returns the result.
CONST D3DXVECTOR3* pV1, // Left sided operand.
CONST D3DXVECTOR3* pV2 // Right sided operand.
);
D3DXVECTOR3 u(1.0f, -1.0f, 0.0f);
D3DXVECTOR3 v(3.0f, 2.0f, 1.0f);
// 1.0*3.0 + -1.0*2.0 + 0.0*1.0
// = 3.0 + -2.0
float dot = D3DXVec3Dot( &u, &v ); // = 1.0

7.向量叉积

p = u X v = [(UyVz-UzVy), (UzVx-UxVz), (UxVy-UyVx)]

Px = UyVz-UzVy;
Py = UzVx-UxVz;
Pz = UxVy-UyVx;

u X v = -(v X U) 可见叉乘不可交换

D3DXVECTOR3 *D3DXVec3Cross(
D3DXVECTOR3* pOut, // Result.
CONST D3DXVECTOR3* pV1, // Left sided operand.
CONST D3DXVECTOR3* pV2 // Right sided operand.
);

8.矩阵的数乘、加法、乘法

二、D3DX矩阵
D3D关注4X4矩阵和1X4矩阵(行向量)
1.
向量-矩阵乘法:1X4行向量乘以4X4矩阵,结果仍为1X4行向量,比如向量的平移、旋转、变换
矩阵-矩阵乘法:4X4矩阵右乘4X4矩阵,结果仍为4X4矩阵,比如变换的叠加等。

2.D3DX中矩阵的定义

  1. typedef struct D3DXMATRIX : public D3DMATRIX
  2. {
  3. public:
  4. D3DXMATRIX() {};
  5. D3DXMATRIX(CONST FLOAT*);
  6. D3DXMATRIX(CONST D3DMATRIX&);
  7. // 矩阵元素
  8. D3DXMATRIX(FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
  9. FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
  10. FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
  11. FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44);

  1. // access grants
  2. FLOAT& operator () (UINT Row, UINT Col);
  3. FLOAT operator () (UINT Row, UINT Col) const;
  4. // casting operators
  5. operator FLOAT* ();
  6. operator CONST FLOAT* () const;
  7. // assignment operators
  8. D3DXMATRIX& operator *= (CONST D3DXMATRIX&);
  9. D3DXMATRIX& operator += (CONST D3DXMATRIX&);
  10. D3DXMATRIX& operator -= (CONST D3DXMATRIX&);
  11. D3DXMATRIX& operator *= (FLOAT);
  12. D3DXMATRIX& operator /= (FLOAT);
  13. // unary operators
  14. D3DXMATRIX operator + () const;
  15. D3DXMATRIX operator - () const;
  16. // binary operators
  17. D3DXMATRIX operator * (CONST D3DXMATRIX&) const;
  18. D3DXMATRIX operator + (CONST D3DXMATRIX&) const;
  19. D3DXMATRIX operator - (CONST D3DXMATRIX&) const;
  20. D3DXMATRIX operator * (FLOATconst;
  21. D3DXMATRIX operator / (FLOATconst;
  22. friend D3DXMATRIX operator * (FLOAT, CONST D3DXMATRIX&);
  23. BOOL operator == (CONST D3DXMATRIX&) const;
  24. BOOL operator != (CONST D3DXMATRIX&) const;
  25. } D3DXMATRIX, *LPD3DXMATRIX;
简单D3DMATRIX结构:
  1. typedef struct _D3DMATRIX {
  2.   union {
  3.     struct {
  4.       float _11, _12, _13, _14;
  5.       float _21, _22, _23, _24;
  6.       float _31, _32, _33, _34;
  7.       float _41, _42, _43, _44;
  8.     };
  9.     float m[4][4];
  10.   };
  11. } D3DMATRIX;
初始化和乘法:
  1. D3DXMATRIX A(…); // initialize A
  2. D3DXMATRIX B(…); // initialize B
  3. D3DXMATRIX C = A * B; // C = AB
元素访问:
  1. D3DXMATRIX M;
  2. M(0, 0) = 5.0f; // Set entry (i=1 j = 1) to 5.0f. 将左上角元素设为5.0f
  1. // 求单位矩阵
  2. D3DXMATRIX *D3DXMatrixIdentity(
  3.                                 D3DXMATRIX *pout // The matrix to be set to the identity.
  4.                                 );
  5. D3DXMATRIX M;
  6. D3DXMatrixIdentity( &M ); // M = identity matrix
  7. // 求矩阵的转置
  8. D3DXMATRIX *D3DXMatrixTranspose(
  9.                                 D3DXMATRIX *pOut, // The resulting transposed matrix.
  10.                                 CONST D3DXMATRIX *pM // The matrix to take the transpose of.
  11.                                 );
  12. D3DXMATRIX A(...); // initialize A
  13. D3DXMATRIX B;
  14. D3DXMatrixTranspose( &B, &A ); // B = transpose(A)
  15. // 求矩阵的逆矩阵
  16. D3DXMATRIX *D3DXMatrixInverse(
  17.                                 D3DXMATRIX *pOut, // returns inverse of pM
  18.                                 FLOAT *pDeterminant, // determinant, if required, else pass 0
  19.                                 CONST D3DXMATRIX *pM // matrix to invert
  20.                                 );


原创粉丝点击