2014-02-10 62 views
0

我有一些使用xnamath.h的DirectX C++代碼。我想遷移到 「全新」 DirectXMath,所以我已經改變了:binary' - ':'DirectX :: XMVECTOR'沒有定義該運算符或轉換(從xnamath遷移到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的 64,項目目標是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/

+0

你確定這兩個'CursorObjectSpace'和'RayOrigin'是類型'的DirectX :: XMVECTOR'? – ciamej

+0

我100%確定,我也發佈了複雜的代碼,您可以在這裏看到「CursorObjectSpace」和「RayOrigin」的聲明。 – PolGraphic

+0

您可能會明智地使用['SimpleMath'](http://blogs.msdn.com/b/shawnhar/archive/2013/01/08/simplemath-a-simplified-wrapper-for-directxmath.aspx) ,它更容易。 –

回答

-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

3

微軟提供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; 
} 
相關問題