DirectX向量与矩阵
配置vs2013项目属性的VC++目录:
比如DirectX SDK安装目录是D:\Program Files (x86)\Microsoft DirectX SDK (June 2010)
则包含目录和库目录配置如下:
/* Include */
$(VC_IncludePath);$(WindowsSDK_IncludePath);D:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Include
/* Lib */
$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86);D:\Program Files %28x86%29\Microsoft DirectX SDK %28June 2010%29\Lib\x86;$(LibraryPath)
使用Lib可以在 项目属性-链接器-输入-附加依赖项 中添加或者在程序中使用预处理指令#pragma comment( comment-type ,["commentstring"] )。
1 #include <d3dx10.h> 2 #include <iostream> 3 using namespace std; 4 5 #pragma comment(lib, "d3dx10.lib") 6 7 ostream& operator<<(ostream& os, D3DXVECTOR4& v) 8 { 9 os << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")"; 10 return os; 11 } 12 ostream& operator<<(ostream& os, D3DXMATRIX& m) 13 { 14 for (int i = 0; i < 4; ++i) 15 { 16 for (int j = 0; j < 4; ++j) 17 os << m(i, j) << " "; 18 os << endl; 19 } 20 return os; 21 } 22 ostream& operator<<(ostream& os, D3DXVECTOR3& v) 23 { 24 os << "(" << v.x << ", " << v.y << ", " << v.z << ")"; 25 return os; 26 } 27 28 int main() 29 { 30 // Using constructor, D3DXVECTOR3(FLOAT x, FLOAT y, FLOAT z); 31 D3DXVECTOR3 u(1.0f, 2.0f, 3.0f); 32 33 // Using constructor, D3DXVECTOR3(CONST FLOAT *); 34 float x[3] = { -2.0f, 1.0f, -3.0f }; 35 D3DXVECTOR3 v(x); 36 37 // Using constructor, D3DXVECTOR3() {}; 38 D3DXVECTOR3 a, b, c, d, e; 39 40 // Vector addition: D3DXVECTOR3 operator + 41 a = u + v; 42 43 // Vector subtraction: D3DXVECTOR3 operator - 44 b = u - v; 45 46 // Scalar multiplication: D3DXVECTOR3 operator * 47 c = u * 10; 48 49 // ||u|| 50 float L = D3DXVec3Length(&u); 51 52 // d = u / ||u|| 53 D3DXVec3Normalize(&d, &u); 54 55 // s = u dot v 56 float s = D3DXVec3Dot(&u, &v); 57 58 // e = u x v 59 D3DXVec3Cross(&e, &u, &v); 60 61 cout << "u = " << u << endl; 62 cout << "v = " << v << endl; 63 cout << "a = u + v = " << a << endl; 64 cout << "b = u - v = " << b << endl; 65 cout << "c = u * 10 = " << c << endl; 66 cout << "d = u / ||u|| = " << d << endl; 67 cout << "e = u x v = " << e << endl; 68 cout << "L = ||u|| = " << L << endl; 69 cout << "s = u.v = " << s << endl; 70 cout << endl; 71 72 73 74 D3DXMATRIX A(1.0f, 0.0f, 0.0f, 0.0f, 75 0.0f, 2.0f, 0.0f, 0.0f, 76 0.0f, 0.0f, 4.0f, 0.0f, 77 1.0f, 2.0f, 3.0f, 1.0f); 78 79 D3DXMATRIX B; 80 D3DXMatrixIdentity(&B); 81 82 // matrix-matrix multiplication 83 D3DXMATRIX C = A*B; 84 85 D3DXMATRIX D, E, F; 86 87 D3DXMatrixTranspose(&D, &A); 88 89 D3DXMatrixInverse(&E, 0, &A); 90 91 F = A * E; 92 93 D3DXVECTOR4 P(2.0f, 2.0f, 2.0f, 1.0f); 94 D3DXVECTOR4 Q(2.0f, 2.0f, 2.0f, 0.0f); 95 D3DXVECTOR4 R, S; 96 97 D3DXVec4Transform(&R, &P, &A); 98 99 D3DXVec4Transform(&S, &Q, &A); 100 101 cout << "A = " << endl << A << endl; 102 cout << "B = " << endl << B << endl; 103 cout << "C = A*B = " << endl << C << endl; 104 cout << "D = transpose(A)= " << endl << D << endl; 105 cout << "E = inverse(A) = " << endl << E << endl; 106 cout << "F = A*E = " << endl << F << endl; 107 cout << "P = " << P << endl; 108 cout << "Q = " << Q << endl; 109 cout << "R = P*A = " << R << endl; 110 cout << "S = Q*A = " << S << endl; 111 112 return 0; 113 }