0

我有一些使用xnamath.h. 我想迁移到“全新” DirectXMath,所以我改变了:

#include <xnamath.h>

#include <DirectXMath.h>

我还添加了DirectX命名空间,例如:

DirectX::XMFLOAT3 vector;

我已经准备好迎接麻烦了,他们来了!

在编译过程中,我得到了错误

error C2676: binary '-' : 'DirectX::XMVECTOR' does not define this operator 
    or a conversion to a type acceptable to the predefined operator 

对于工作正常的线路xnamth.h

DirectX::XMVECTOR RayDir = CursorObjectSpace - RayOrigin;

我真的不知道如何解决它。我认为operator-不再“不支持”了,但是什么会导致该错误以及如何解决

这是更复杂的源代码:

DirectX::XMVECTOR RayOrigin = DirectX::XMVectorSet(cPos.getX(), cPos.getY(), cPos.getZ(), 0.0f); 
POINT mouse;
GetCursorPos(&mouse);

DirectX::XMVECTOR CursorScreenSpace = DirectX::XMVectorSet(mouse.x, mouse.y, 0.0f, 0.0f);

RECT windowRect;
GetWindowRect(*hwnd, &windowRect);
DirectX::XMVECTOR CursorObjectSpace = XMVector3Unproject( CursorScreenSpace, windowRect.left, windowRect.top, screenSize.getX(), screenSize.getY(), 0.0f, 1.0f, XMLoadFloat4x4(&activeCamera->getProjection()), XMLoadFloat4x4(&activeCamera->getView()), DirectX::XMMatrixIdentity());

DirectX::XMVECTOR RayDir = CursorObjectSpace - RayOrigin;

我正在使用Windows 7 x64,项目目标是 x32 调试,xnamath.h到目前为止它运行良好。


可行的解决方案是:

DirectX::XMVECTOR RayDir = DirectX::XMVectorSet( //write more, do less..
    DirectX::XMVectorGetX(CursorObjectSpace) - DirectX::XMVectorGetX(RayOrigin),
    DirectX::XMVectorGetY(CursorObjectSpace) - DirectX::XMVectorGetY(RayOrigin),
    DirectX::XMVectorGetZ(CursorObjectSpace) - DirectX::XMVectorGetZ(RayOrigin),
    DirectX::XMVectorGetW(CursorObjectSpace) - DirectX::XMVectorGetW(RayOrigin)
); //oh my God, I'm so creepy solution

但与以前相比,它太令人毛骨悚然了,为xnamath

    XMVECTOR RayDir = CursorObjectSpace - RayOrigin;

我真的不相信这是唯一的方法,我不能operator-像上面那样使用。

我也有完全相同的问题operator/

4

2 回答 2

4

Microsoft 在 DirectXMathVector.inl 标头中提供运算符重载,该标头包含在 DirectXMath.h 的末尾。但是,为了能够使用它,您必须在尝试使用该运算符的范围内具有“使用命名空间 DirectX”。

例如:

void CalculateRayDirection(const DirectX::XMVECTOR& rayOrigin, DirectX::XMVECTOR& rayDirection)
{
    using namespace DirectX;

    POINT mouse;
    GetCursorPos(&mouse);
    XMVECTOR CursorScreenSpace = XMVectorSet(mouse.x, mouse.y, 0.0f, 0.0f);

    rayDirection = CursorObjectSpace - rayOrigin;
}
于 2014-05-10T22:07:54.307 回答
-1

XMVector 的减号和除法运算符没有重载,因为 XMVector 不是一个类 - 它是用于 SSE 操作的 __m128 数据类型的 typedef。

在升级到 DirectXMath 时,Microsoft 打算通过使其“支持 SSE”来加速矢量运算。他们还提供了 XMVectorSubtract 等功能。让您在执行算术运算时使用 SSE。

您可以在此处找到更多信息:http: //msdn.microsoft.com/en-us/library/windows/desktop/ee415656 (v=vs.85).aspx

于 2014-02-20T20:21:36.807 回答