5

我是 DirectX 编程和 Visual C++ 的新手,在将我从 xnamath.h 找到的示例迁移到 DirectXMath.h 时遇到问题。我正在使用 Visual Studio 2012。

代码的目的只是初始化一个 XMMATRIX,然后在控制台中显示它。原始代码如下所示(它工作得很好):

#include <windows.h>
#include <xnamath.h>
#include <iostream>
using namespace std;

ostream& operator<<(ostream& os, CXMMATRIX m)
{
    for(int i = 0; i < 4; ++i)
    {
        for(int j = 0; j < 4; ++j)
            os << m(i, j) << "\t";
        os << endl;
    }
    return os;
}

int main()
{
    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);

    cout << "A = " << endl << A << endl;

    return 0;
}

当我运行程序时,它会给出以下输出:

A =
1       0       0       0
0       2       0       0
0       0       4       0
1       2       3       1

Press any key to continue . . .

但是,当我将标头更改为 DirectXMath 时,它不再起作用:

#include <windows.h>
#include <iostream>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
using namespace DirectX; 
using namespace DirectX::PackedVector;
using namespace std;

ostream& operator<<(ostream& os, CXMMATRIX m)
{
    for(int i = 0; i < 4; ++i)
    {
        for(int j = 0; j < 4; ++j)
            os << m(i, j) << "\t";
        os << endl;
    }
    return os;
}

int main()
{
    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);

    cout << "A = " << endl << A << endl;

    return 0;
}

当我尝试编译时,我收到一条错误os << m(i, j) << "\t";消息:

error C2064: term does not evaluate to a function taking 2 arguments

当我将鼠标悬停在它下方的红色波浪线上时,m(i, j)它告诉我:

DirectX::CXMMATRIX m
Error: call of an object of a class type without appropriate operator() or conversion function to pointer-to-function type

任何建议将不胜感激。

4

3 回答 3

3

我更改了示例代码以使用

ostream& operator<<(ostream& os, CXMMATRIX m)
{
    for(int i = 0; i < 4; ++i)
    {
            for(int j = 0; j < 4; ++j)
                os << m.r[i].m128_f32[j] << "\t";
            os << endl;
    }
    return os;
}

这将具有与旧 xnamath 相同的效果。

于 2013-07-29T04:27:36.597 回答
2

这取决于您用于 DirectXMath 的版本,您可以定义 _XM_NO_INTRINSICS_ 以获得您想要的结果。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.xmmatrix(v=vs.85).aspx

于 2013-01-10T18:02:54.283 回答
1

在 Direct X 11+ 中,直接访问矩阵的能力为

matrix (row, column)

由于性能问题,已被删除。Microsoft 建议通过r成员访问这些值。我建议使用

XMStoreFloat4 (row, column)

对于 4x4 矩阵,您无需担心数据类型。

ostream& operator<< (ostream& os, CXMMATRIX m)
{
    for (int i = 0; i < 4; i++)
    {
        XMVECTOR row = m.r[i];
        XMFLOAT4 frow;
        XMStoreFloat4(&frow, row);

        os << frow.x << "\t" << frow.y << "\t" << frow.z << "\t" << frow.w << endl;
    }

    return os;
}

使用时要小心,_XM_NO_INTRINSICS_因为这不仅会影响访问矩阵值,还可能会影响性能敏感代码中的性能,并可能影响其他操作。DirectXMath 是 XNAMath 的一个跳跃……升级旧代码时,可能会很痛苦,但值得。

于 2013-08-06T20:34:00.787 回答