Reputation: 167
I am new to directX programming and Visual C++ and I am having an issue migrating an example I found from xnamath.h to DirectXMath.h. I am using Visual Studio 2012.
The purpose of the code is simply to initialize a XMMATRIX and then display it in the console. The original code is shown below (it works just fine):
#include <windows.h>
#include <xnamath.h>
#include <iostream>
using namespace std;
ostream& operator<<(ostream& os, CXMMATRIX m)
{
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
os << m(i, j) << "\t";
os << endl;
}
return os;
}
int main()
{
XMMATRIX A(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 4.0f, 0.0f,
1.0f, 2.0f, 3.0f, 1.0f);
cout << "A = " << endl << A << endl;
return 0;
}
When I run the program it gives the following output:
A =
1 0 0 0
0 2 0 0
0 0 4 0
1 2 3 1
Press any key to continue . . .
However, when I change the headers to DirectXMath it no longer works:
#include <windows.h>
#include <iostream>
#include <DirectXMath.h>
#include <DirectXPackedVector.h>
using namespace DirectX;
using namespace DirectX::PackedVector;
using namespace std;
ostream& operator<<(ostream& os, CXMMATRIX m)
{
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
os << m(i, j) << "\t";
os << endl;
}
return os;
}
int main()
{
XMMATRIX A(1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 2.0f, 0.0f, 0.0f,
0.0f, 0.0f, 4.0f, 0.0f,
1.0f, 2.0f, 3.0f, 1.0f);
cout << "A = " << endl << A << endl;
return 0;
}
When I try to compile I get an error with os << m(i, j) << "\t";
which says:
error C2064: term does not evaluate to a function taking 2 arguments
When I hover over the red squiggly line under m(i, j)
it tells me:
DirectX::CXMMATRIX m
Error: call of an object of a class type without appropriate operator() or conversion function to pointer-to-function type
Any advice would be greatly appreciated.
Upvotes: 5
Views: 2480
Reputation: 84
In Direct X 11+, the ability to access a matrix directly as
matrix (row, column)
has been removed, due to performance issues. Microsoft recommends accessing the values via the r
member. I suggest using
XMStoreFloat4 (row, column)
for a 4x4 matrix, as then you do not need to worry about the data type.
ostream& operator<< (ostream& os, CXMMATRIX m)
{
for (int i = 0; i < 4; i++)
{
XMVECTOR row = m.r[i];
XMFLOAT4 frow;
XMStoreFloat4(&frow, row);
os << frow.x << "\t" << frow.y << "\t" << frow.z << "\t" << frow.w << endl;
}
return os;
}
Be careful when using _XM_NO_INTRINSICS_
as this affects more than just accessing matrix values, may have performance hits in performance sensitive code and may impact other operations. DirectXMath is a jump from XNAMath... and when upgrading old code, it can be a pain, but it is worth it.
Upvotes: 1
Reputation: 31
I changed the example code to use
ostream& operator<<(ostream& os, CXMMATRIX m)
{
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
os << m.r[i].m128_f32[j] << "\t";
os << endl;
}
return os;
}
This will have the same effect as the old xnamath had.
Upvotes: 3
Reputation: 403
It depends on the version you are using for DirectXMath, you can define _XM_NO_INTRINSICS_ to get the result you want. See http://msdn.microsoft.com/en-us/library/windows/desktop/microsoft.directx_sdk.reference.xmmatrix(v=vs.85).aspx for more information
Upvotes: 2