One vector
A vector is a physical quantity that has both direction and size, and in DirectX10, we mainly focus on the D3DX vector. The D3DXVECTOR3 class is mainly used to store the coordinates of points and vectors relative to a certain coordinate system in the code, and there are many very useful vector processing functions among them:
FLOAT D3DXVec3Length( //return || v ||
CONST D3DXVECTOR3 *pV);
FLOAT D3DXVec3Length3q( //return || v || * || v ||
CONST D3DXVECTOR3 *pV);
FLOAT D3DXVec3Dot( //return v1 . v2
CONST D3DXVECTOR3 *pV1,
CONST D3DXVECTOR3 *pV2,
);
D3DXVECTOR3 D3DXVec3Cross( //return v1 x v2
CONST D3DXVECTOR3 *pV1,
CONST D3DXVECTOR3 *pV2,
}
D3DXVECTOR3 *WINAPI D3DXVec3Normalize(
D3DXVECTOR3 *pOut, //return v / ||v||
CONST D3DXVECTOR3 *pV
);
When programs that need to use D3DX must be linked to the d3dx10. lib library file (when compiling in debug mode, it should be linked to d3d10d.lib).
We can try these functions:
#include <iostream>
//#include <D3DX10.h>
#include <D3DX10math.h>
using namespace std;
ostream& operator<<(ostream& os,D3DXVECTOR3& v)
{
os<<"{"<<v.x<<","<<v.y<<","<<v.z<<"}";
return os;
}
int main()
{
D3DXVECTOR3 u(1.0f,2.0f,3.0f);
float x[3] = {-2.0f,1.0,-3.0f};
D3DXVECTOR3 v(x);
D3DXVECTOR3 a,b,c,d,e;
a = u+v;
b = u-v;
c = u*10;
float length = D3DXVec3Length(&u);
D3DXVec3Normalize(&d,&u);
float s = D3DXVec3Dot(&u,&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<<"length = ||u|| = "<<length<<endl;
cout<<"s = u.v = "<<s<<endl;
system("pause");
return 0;
}
The results are as follows:
.
There are several things we need to pay attention to:
1. The D3DXVECTOR3 and various operation functions we need to use are defined in the header file D3DX10math. h, so we need to include this header file.
2. When configuring the link library, it is best to add the debug library of the library, usually with an additional d at the end, such as d3dx10. lib and d3dx10. lib.
3. It is not recommended to use using namespace std; This form can easily cause interference and errors when using variables or functions from different domains. It is recommended to use (domain:: variable) directly. The program in this article is simple, so it is done for simplicity.
When overloading operators, be sure to pay attention to the parameter form.
5. When error LNK2019 occurs: unresolved external symbols_ D3DXVec3Normalize@ 8. This symbol is used in the function_ When encountering errors such as being referenced in main, be sure to check your linked library to see if it is linked to the X64 library. Just change it back to the X86 library. Don't link to the X64 library just because you are a 64 bit machine, because you are creating a Windows 32 project, you can only link to the X86 library. I will have time to delve deeper into this point later.