您的位置:首页 > 其它

DirectX向量与矩阵

2015-04-22 12:28 141 查看
配置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"] )。

#include <d3dx10.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "d3dx10.lib")

ostream& operator<<(ostream& os, D3DXVECTOR4& v)
{
os << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
return os;
}
ostream& operator<<(ostream& os, D3DXMATRIX& m)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
os << m(i, j) << "  ";
os << endl;
}
return os;
}
ostream& operator<<(ostream& os, D3DXVECTOR3& v)
{
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}

int main()
{
// Using constructor, D3DXVECTOR3(FLOAT x, FLOAT y, FLOAT z);
D3DXVECTOR3 u(1.0f, 2.0f, 3.0f);

// Using constructor, D3DXVECTOR3(CONST FLOAT *);
float x[3] = { -2.0f, 1.0f, -3.0f };
D3DXVECTOR3 v(x);

// Using constructor, D3DXVECTOR3() {};
D3DXVECTOR3 a, b, c, d, e;

// Vector addition: D3DXVECTOR3 operator +
a = u + v;

// Vector subtraction: D3DXVECTOR3 operator -
b = u - v;

// Scalar multiplication: D3DXVECTOR3 operator *
c = u * 10;

// ||u||
float L = D3DXVec3Length(&u);

// d = u / ||u||
D3DXVec3Normalize(&d, &u);

// s = u dot v
float s = D3DXVec3Dot(&u, &v);

// e = u x v
D3DXVec3Cross(&e, &u, &v);

cout << "u             = " << u << endl;
cout << "v             = " << v << endl;
cout << "a = u + v     = " << a << endl;
cout << "b = u - v     = " << b << endl;
cout << "c = u * 10    = " << c << endl;
cout << "d = u / ||u|| = " << d << endl;
cout << "e = u x v     = " << e << endl;
cout << "L = ||u||     = " << L << endl;
cout << "s = u.v       = " << s << endl;
cout << endl;

D3DXMATRIX A(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 4.0f, 0.0f,
1.0f, 2.0f, 3.0f, 1.0f);

D3DXMATRIX B;
D3DXMatrixIdentity(&B);

// matrix-matrix multiplication
D3DXMATRIX C = A*B;

D3DXMATRIX D, E, F;

D3DXMatrixTranspose(&D, &A);

D3DXMatrixInverse(&E, 0, &A);

F = A * E;

D3DXVECTOR4 P(2.0f, 2.0f, 2.0f, 1.0f);
D3DXVECTOR4 Q(2.0f, 2.0f, 2.0f, 0.0f);
D3DXVECTOR4 R, S;

D3DXVec4Transform(&R, &P, &A);

D3DXVec4Transform(&S, &Q, &A);

cout << "A = " << endl << A << endl;
cout << "B = " << endl << B << endl;
cout << "C = A*B = " << endl << C << endl;
cout << "D = transpose(A)= " << endl << D << endl;
cout << "E = inverse(A) = " << endl << E << endl;
cout << "F = A*E = " << endl << F << endl;
cout << "P = " << P << endl;
cout << "Q = " << Q << endl;
cout << "R = P*A = " << R << endl;
cout << "S = Q*A = " << S << endl;

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