Reputation: 3364
I have some DirectX C++ code that uses xnamath.h
. I wanted to migrate to "brand new" DirectXMath
, so I've changed:
#include <xnamath.h>
to
#include <DirectXMath.h>
I have also added DirectX
namespace, e.g.:
DirectX::XMFLOAT3 vector;
I was ready for troubles, and here they come!
During compilation, I got error:
error C2676: binary '-' : 'DirectX::XMVECTOR' does not define this operator
or a conversion to a type acceptable to the predefined operator
For the line that worked fine for xnamth.h
:
DirectX::XMVECTOR RayDir = CursorObjectSpace - RayOrigin;
I don't really know how to fix it. I don't think that operator-
is "not supported" anymore, but what can cause that error and how to fix it?
Here is more complex source code:
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;
I'm working on Windows 7 x64, project target is x32 debug and it worked fine for xnamath.h
so far.
The working solution would be:
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
But it is soo creepy compare to previous, working for xnamath
:
XMVECTOR RayDir = CursorObjectSpace - RayOrigin;
I really don't belive it's the only way and I cannot just use operator-
like above.
I also have exact the same problem for operator/
.
Upvotes: 0
Views: 3081
Reputation: 2907
Microsoft provides the operator overloads in DirectXMathVector.inl header, which is included at the end of DirectXMath.h. However, to be able to use it, you must have "using namespace DirectX" in the scope where you're trying to use the operator.
For example:
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;
}
Upvotes: 4
Reputation: 370
The minus and divide operators for XMVector aren't overloaded because XMVector isn't a class - it's a typedef for the __m128 data type used for SSE operations.
In the upgrade to DirectXMath, Microsoft intended to speed up vector operations by making them "SSE capable". They also provided the functions XMVectorSubtract et al. to let you use SSE when performing arithmetic operations.
You can find more info here: http://msdn.microsoft.com/en-us/library/windows/desktop/ee415656(v=vs.85).aspx
Upvotes: -1