基于DirectX11的3D图形程序设计案例教程——重庆大学出版社
第三章 XNA数学库
MatrixTrans
#include<iostream>
#include<d3dcompiler.h>
#include<xnamath.h>
using namespace std;
//重载"<<",cout<<(XMVECTOR)
ostream & operator <<(ostream& os, XMVECTOR u)
{
os << "(" << XMVectorGetX(u) << ","
<< XMVectorGetY(u) << ","
<< XMVectorGetZ(u) << ","
<< XMVectorGetW(u) << ")"
<< endl;
return os;
}
//重载"<<",cout<<(XMFLOAT4X4)
ostream & operator <<(ostream& os, XMFLOAT4X4 m)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
os << "\t" << m(i, j) << " ";
}
os << endl;
}
os << endl;
return os;
}
int main()
{
//平移、旋转、缩放矩阵
XMMATRIX mTrans, mRota, mScal;
mScal = XMMatrixScaling(0.2f, 0.2f, 0.2f);
cout << "缩放矩阵为:" << endl;
XMFLOAT4X4 mScalFL;
XMStoreFloat4x4(&mScalFL, mScal);
cout << mScalFL;
mRota = XMMatrixRotationY(XM_PIDIV4);
cout << "旋转矩阵为:" << endl;
XMFLOAT4X4 mRotaFL;
XMStoreFloat4x4(&mRotaFL, mRota);
cout << mRotaFL;
mTrans = XMMatrixTranslation(1.0f, 2.0f, -3.0f);
cout << "平移矩阵为:" << endl;
XMFLOAT4X4 mTransFL;
XMStoreFloat4x4(&mTransFL, mTrans);
cout << mTransFL;
//利用平移、旋转、缩放矩阵得到最终变换矩阵
XMMATRIX mFinal;
mFinal = XMMatrixMultiply(mScal, mRota);
mFinal = XMMatrixMultiply(mFinal, mTrans);
cout << "最终变换矩阵为:" << endl;
XMFLOAT4X4 mFinalFL;
XMStoreFloat4x4(&mFinalFL, mFinal);
cout << mFinalFL;
//向量变换
XMVECTOR mVector = XMVectorSet(5.0f, 0.0f, 0.0f, 1.0f);
cout << "变换前的向量为:" << endl;
cout << mVector;
mVector = XMVector4Transform(mVector, mFinal);
cout << "变换后的向量为:" << endl;
cout << mVector;
system("pause");
return 0;
}