《dx12 龙书》第二部分学习笔记

矩阵相关的一些实用函数

#include <Windows.h>
#include <iostream>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>

using namespace std;
using namespace DirectX;
using namespace DirectX::PackedVector;

int main()
{
    
    
	//通过下列方法将数据从XMFLOAT4X4类型加载到XMMATRIX类型中
	 XMMATRIX XM_CALLCONV XMLOADFLOAT4x4(const XMFLOAT4X4 * pSource);

	 //通过下列方法将数据从FXMMATRIX类型加载到XMFLOAT4X4类型中
	 void XM_CALLCONV XMSTOREFLOAT4X4(XMFLOAT4X4 * pDestination, FXMMATRIX M);

	 //返回单位矩阵I
	 XMMATRIX XM_CALLCONV XMMatrixIdentity();

	 //如果M是单位矩阵则返回true
	 //输入矩阵M
	 bool XM_CALLCONV XMMatrixIsIdentity(FXMMATRIX M);

	 //返回矩阵乘积AB
	 //输入矩阵A
	 //输入矩阵B
	 XMMATRIX XM_CALLCONV XMMatrixMultiply(FXMMATRIX A, CXMMATRIX B);

	 //返回M的转置矩阵
	 //输入矩阵M
	 XMMATRIX XM_CALLCONV XMMatrixTranspose(FXMMATRIX M);

	 //返回(det M,det M,det M,det M)
	 //输入矩阵M
	 XMVECTOR XM_CALLCONV XMMatrixDeterminant(FXMMATRIX M);

	 //返回M的逆矩阵
	 //输入(det M,det M,det M,det M)
	 //输入矩阵M
	 XMMATRIX XM_CALLCONV XMMatrixInverse(XMVECTOR * pDeterminant, FXMMATRIX M);
}

示例

#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& os, FXMVECTOR v)
{
    
    
    XMFLOAT4 dest;
    XMStoreFloat4(&dest, v);

    os << "(" << dest.x << "," << dest.y << "," << dest.z << "," << dest.w << ")";
    return os;
}


ostream& XM_CALLCONV operator << (ostream& os, FXMMATRIX m)
{
    
    
    for (int 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]);
        os << endl;
    }

    return os;
}

int main()
{
    
    
    //检查是否支持SSE2指令集(Pentium4,AMD K8及其后续版本的处理器)
    if (!XMVerifyCPUSupport())
    {
    
    
        cout << "directx math not supported" << 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 = " << endl << A << endl;
    cout << "B = " << endl << B << endl;
    cout << "C = A*B = " << endl << C << endl;
    cout << "D = transpose(A) = " << endl << D << endl;
    cout << "det = determinant(A) = " << endl << det << endl;
    cout << "E = inverse(A) = " << endl << E << endl;
    cout << "F = A*E = " << endl << F << endl;

    return 0;
}

结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47819574/article/details/131307663