0

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/.

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • Are you sure that both `CursorObjectSpace` and `RayOrigin` are of type `DirectX::XMVECTOR`? – ciamej Feb 10 '14 at 21:56
  • I'm 100% sure, I have also posted complex code, where you can see the declaration of both `CursorObjectSpace` and `RayOrigin`. – PolGraphic Feb 10 '14 at 22:21
  • You may be wise to use [`SimpleMath`](http://blogs.msdn.com/b/shawnhar/archive/2013/01/08/simplemath-a-simplified-wrapper-for-directxmath.aspx) instead, it's much easier. – Roger Rowland Feb 11 '14 at 06:19
  • Thanks for telling about SimpleMath, but still I look for solution for DirectXMath and I don't find it too hard (except for that operator- problem...). Anyway I use it only for few things and just want to migrate from xnamath to it. – PolGraphic Feb 11 '14 at 23:02

2 Answers2

4

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;
}
Sunius
  • 2,789
  • 18
  • 30
-1

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

Mrfence
  • 350
  • 2
  • 12