二进制 '-' : 'DirectX::XMVECTOR' 没有定义这个运算符或转换(从 xnamath 迁移到 DirectXMath 并不容易)

binary '-' : 'DirectX::XMVECTOR' does not define this operator or a conversion (migrating from xnamath to DirectXMath is not so easy)

本文关键字:迁移 xnamath 不容易 转换 DirectXMath XMVECTOR DirectX 定义 运算符 二进制      更新时间:2023-10-16

我有一些使用xnamath.h的DirectX C++代码。我想迁移到"全新"的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;

我在Windows7x64上工作,项目目标是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/,我也有完全相同的问题。

Microsoft在DirectXMathVector.inl标头中提供运算符重载,该标头包含在DirectXMath.h的末尾。但是,为了能够使用它,您必须在尝试使用运算符的范围中具有"using namespace 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;
}

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

在升级到DirectXMath的过程中,微软打算通过使矢量运算"具有SSE功能"来加快矢量运算的速度。他们还提供了XMVectorSubtract等函数,让您在执行算术运算时使用SSE。

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