Directx12学习笔记(一)
数学基础:
第一部分:
1、坐标系:
左手坐标系:如Directx使用的坐标系就是左手坐标系
右手坐标系:OpenGl和3dsmax使用的就是右手坐标系
第二部分:向量:(只有标量和方向有关,与位置无关)(自行学习)
运算:
- 点乘:向量与向量进行点乘: (1,2)· (2,4) = 1 * 2 + 2 * 4 = 10
- 叉乘:向量与向量进行叉乘: ()
- 标乘:标量与向量进行想乘
零向量:没有方向,标量为0的向量。
单位向量:标量为1的向量。
三角形或平行四边形法则
运算定律:
交换律
结合律
SEE2硬件加速(使用SIMD寄存器加快运算速度)
步骤如图所示:
64位不需要开启fast,32位需要开启fast。
Directx中的DirectXMath库(微软官网文档)
XMVector类型 实际上是_128m类型 它是一种SIMD类型。
局部变量或全局变量中XMVector类型会自动分为16字节。
类中数据成员中建议使用XMFloat2、XMFloat3、XMFloat4类型。
定义函数使用XM_CALLCONV修饰函数名,函数参数中:
前三个使用FXMVECTOR
第四个使用GXMVECTOR
第五个第六个使用HXMVECTOR
其他使用CXMVECTOR
构造函数中,前三个参数使用FXMVECTOR,其他都使用CXMVECTOR类型,不要使用XM_CALLCONV。
常向量的初始化需要使用XMVECTORF32类型。
DirectXMath使用命名空间DirectX。
XMVectorSet函数用来给XMVector类型赋值。
XMFloat3转换为XMVector类型使用XMLoadFloat3(传递XMFloat3指针),返回值为XMVector。
XMVector类型转换为XMFloat3类型使用XMStroeFloat3(要赋值XMFloat3指针,传递的XMVector类型的变量)。
XMVector3Length:返回向量的模
XMVector3Normalize: 返回单位向量
XMVector3Dot:计算点乘
XMVector3Cross:计算叉乘
XMVector3ComponentsFromNormal(&projW,&perpW,w,n):projW是w平行与n的向量,perpW是w垂直于n的向量。
其他自行查文档!!!
#include <Windows.h>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
#include <iostream>
using namespace std;
using namespace DirectX;
using namespace DirectX::PackedVector;
ostream& XM_CALLCONV operator<<(ostream& ios,FXMVECTOR v)
{
XMFLOAT3 dest;
XMStoreFloat3(&dest, v);
ios << "(" << dest.x << "," << dest.y << "," << dest.z << ")" << endl;
return ios;
}
int main()
{
cout.setf(ios_base::boolalpha);
//检测是否支持SSE2指令集
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.0f,3.0f,0.0f,0.0f)
//向量减法
XMVECTOR b = u - v;//(3.0f,1.0f,6.0f,0.0f)
//向量乘法
XMVECTOR c = 10.0f * u;//(10.0f,20.0f,30.0f,0.0f)
//||u||
XMVECTOR L = XMVector3Length(u);
//d = u / ||u||
XMVECTOR d = XMVector3Normalize(u);
//s = u dot v
XMVECTOR s = XMVector3Dot(u, v);
//e = u x v
XMVECTOR e = XMVector3Cross(u, v);
//求出proj_n(w),perp_n(w) proj平行于n perp垂直于n
XMVECTOR projW;
XMVECTOR perpW;
XMVector3ComponentsFromNormal(&projW,&perpW,w,n);
//projW + perpW == w?
bool equal = XMVector3Equal(projW + perpW, w);
bool notEqual = XMVector3NotEqual(projW + perpW, w);
//projW perpW之间的角度
XMVECTOR angleVec = XMVector3AngleBetweenVectors(projW, perpW);
float angleRadians = XMVectorGetX(angleVec);
float angleDegrees = XMConvertToDegrees(angleRadians);
cout << "n=" << n << endl;
cout << "u=" << u << endl;
cout << "v=" << v << endl;
cout << "w=" << w << endl;
cout << "a=" << a << endl;
cout << "c=" << c << endl;
cout << "L=" << L << endl;
cout << "d=" << d << endl;
cout << "s=" << s << endl;
cout << "e=" << e << endl;
cout << "projW=" << projW << endl;
cout << "perpW=" << perpW << endl;
cout << "equal=" << equal << endl;
cout << "notEqual=" << notEqual << endl;
cout << "angleVec=" << angleVec << endl;
cout << "angleRadians=" << angleRadians << endl;
cout << "angleDegress=" << angleDegrees << endl;
XMVECTOR v1 = XMVectorSet(0.99999f, 0.99999f, 0.99999f, 0.0f);
XMVECTOR v2 = XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f);
equal = XMVector3NearEqual(v1,v2,{0.0001f,0.0001f,0.0001f});
if(equal)
{
cout << "equal" << endl;
}
return 0;
}
第三部分:矩阵
矩阵基础知识(自行学习)
行列式:
矩阵的转置:
伴随矩阵:
逆矩阵:
矩阵的类型:
单位矩阵:
对角矩阵:
对称矩阵
XMMatrix(4x4)类型:
部分定义如下:
union
{
XMVECTOR r[4];
struct
{
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;
};
float m[4][4];
};
其中使用了XMVector数组。
在类中使用XMFLOAT4x4,通过XMStoreFloat4x4和XMLoadFloat4x4相互转换
在函数中的参数时:
第一个使用FXMMATRIX类型,其余使用CXMMATRIX类型。
在构造函数转换,全部使用CXMMATRIX类型,不用XM_CALLCONV注解
Directx中的DirectXMath库(微软官网文档)
#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 << ")"<<endl;
return os;
}
ostream& XM_CALLCONV operator<<(ostream& os,FXMMATRIX m)
{
int i = 0;
for(i = 0;i < 4;++i)
{
os << XMVectorGetX(m.r[i]) << "\t";
os << XMVectorGetY(m.r[i]) << "\t";
os << XMVectorGetZ(m.r[i]) << "\t";
os << XMVectorGetW(m.r[i]) << "\t";
os << endl;
}
return os;
}
int main()
{
if(!XMVerifyCPUSupport())
{
cout << "directx math not support" << endl;
return 0;
}
XMMATRIX 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
);
//单位矩阵
XMMATRIX B = XMMatrixIdentity();
XMMATRIX C = A * B;
XMMATRIX D = XMMatrixTranspose(A);
XMVECTOR det = XMMatrixDeterminant(A);
XMMATRIX E = XMMatrixInverse(&det, A);
XMMATRIX F = A * E;
cout << "A=" << A << endl;
cout << "B=" << B << endl;
cout << "C=" << C << endl;
cout << "D=" << D << endl;
cout << "det=" << det << endl;
cout << "E=" << E << endl;
cout << "F=" << F << endl;
return 0;
}