1、设置XMVECTOR类型中的数据:
示例工程说明:
#include <Windows.h>
#include <iostream>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
using namespace std;
using namespace DirectX;
ostream& XM_CALLCONV operator<<(ostream& os, FXMVECTOR v)
{
XMFLOAT3 dest;
XMStoreFloat3(&dest, v);
os << "(" << dest.x << "," << dest.y << "," << dest.z << ")";
return os;
}
int main()
{
//允许输出 true 和 false
cout.setf(ios_base::boolalpha);
//检查是否支持SSE2指令集(Pentium4,AMD K8及其后续版本的处理器)
if (!XMVerifyCPUSupport())
{
cout << "directx math not supported" << endl;
return 0;
}
XMVECTOR p = XMVectorZero(); //返回向量(0,0,0)
XMVECTOR q = XMVectorSplatOne(); //返回向量(1,1,1)
XMVECTOR u = XMVectorSet(1.0f,2.0f,3.0f,0.0f); //返回向量(1,2,3)
XMVECTOR v = XMVectorReplicate(-2.0f); //返回向量(-2,-2,-2)
XMVECTOR w = XMVectorSplatZ(u); //返回向量(u.z,u.z,u.z)
cout << "p = " << p << endl;
cout << "q = " << q << endl;
cout << "u = " << u << endl;
cout << "v = " << v << endl;
cout << "w = " << w << endl;
return 0;
}
2、XMVECTOR向量函数:
示例工程说明:
#include <Windows.h>
#include <iostream>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
using namespace std;
using namespace DirectX;
using namespace DirectX::PackedVector;
ostream& XM_CALLCONV operator<<(ostream& os, FXMVECTOR v)
{
XMFLOAT3 dest;
XMStoreFloat3(&dest, v);
os << "(" << dest.x << "," << dest.y << "," << dest.z << ")";
return os;
}
int main()
{
//允许输出 true 和 false
cout.setf(ios_base::boolalpha);
//检查是否支持SSE2指令集(Pentium4,AMD K8及其后续版本的处理器)
if (!XMVerifyCPUSupport())
{
cout << "directx math not supported" << endl;
return 0;
}
XMVECTOR n = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR u = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR v = XMVectorSet(-2.0f, 1.0f, -3.0f, 0.0f);
XMVECTOR w = XMVectorSet(0.707f, 0.707f, 0.0f, 0.0f);
//向量加
XMVECTOR a = u + v; // (-1,3,0)
//向量减
XMVECTOR b = u - v; // (3,1,6)
//标量乘
XMVECTOR c = 10.0f * u; // (10,20,30)
//||u||
XMVECTOR L = XMVector3Length(u); // (3.74166,3.74166,3.74166)
//d = u / ||u||
XMVECTOR d = XMVector3Normalize(u); // (0.267261,0.534522,0.801784)
//s = u dot v
XMVECTOR s = XMVector3Dot(u, v); // (-9,-9,-9)
//e = u × v
XMVECTOR e = XMVector3Cross(u, v); // (-9,-3,5)
//求出proj_n(w)和perp_n(w)
XMVECTOR projW; // (0.707,0,0)
XMVECTOR perpW; // (0,0.707,0)
XMVector3ComponentsFromNormal(&projW, &perpW, w, n);
//projW + perpW == w?
bool equal = XMVector3Equal(projW + perpW, w) != 0; // true
bool notEqual = XMVector3NotEqual(projW + perpW, w) != 0; // false
//projW与perpW之间的夹角应为90度
XMVECTOR angleVec = XMVector3AngleBetweenVectors(projW, perpW); //获得弧度制 (1.5708,1.5708,1.5708)
float angleRadians = XMVectorGetX(angleVec);
float angleDegrees = XMConvertToDegrees(angleRadians); //转成角度90
cout << "n = " << n << endl;
cout << "u = " << u << endl;
cout << "v = " << v << endl;
cout << "w = " << w << endl;
cout << "a = u + v " << a << endl;
cout << "b = u - v " << b << endl;
cout << "c = 10.0f * u " << c << endl;
cout << "L = ||u|| " << L << endl;
cout << "d = u / ||u|| " << d << endl;
cout << "s = u dot v " << s << endl;
cout << "e = u × v " << e << endl;
cout << "proj_n(w) = " << projW << endl;
cout << "perp_n(w) = " << perpW << endl;
cout << "projW + perpW == w " << equal << endl;
cout << "projW + perpW != w " << notEqual << endl;
cout << "angleVec = " << angleVec << endl;
cout << "angleRadians = " << angleRadians << endl;
cout << "angleDegrees = " << angleDegrees << endl;
}