Answered by:
Convert XNA Matrix to DirectX Matrix

Question
-
It seems that the XNA Matrix and the DirectX Matrix's are different, but I cannot figure out what?
Does anyone know how to convert one to the other?
Dexter
Monday, January 20, 2014 6:00 PM
Answers
-
Can you explain in more detail what you mean? What are you trying to do? You cannot use XNA in a Windows Store app, so a runtime conversion doesn't make sense. There are also several different matrices one can use in Direct3D.
In general the Matrix will have four rows and columns and you can map row to row and column to column between matrices.
- Marked as answer by Xiaoliang Chen - MSFTModerator Tuesday, January 28, 2014 2:33 AM
Monday, January 20, 2014 8:27 PMModerator -
More detail would be helpful. You may be running into convention differences: XNA Game Studio uses Right-Handed Coordinates using a row-major convention. DirectX historically has used Left-Handed, row-major coordinates. Since Direct3D 11 does not have the 'fixed-function' graphics pipeline of Direct3D 9, the choice of graphics math conventions (left-handed vs. right-handed, row-major vs. column-major matrices, etc.) is entirely up to the developer. DirectXMath can be used to create both Direct3D-style "Left-Hand Coordinate" transformations as well as OpenGL-style "Right-Handed Coordinate" transformations using a row-major matrix convention which can be used directly with row-major shaders or transposed to use column-major shaders.
Note: You should look at the SimpleMath wrapper for DirectXMath that is included in the DirectX Tool Kit. It is an adapter that looks very much like the original XNA Game Studio math types implemented using DirectXMath: Vector2, Vector3, Vector4, Matrix, etc.
- Proposed as answer by Chuck Walbourn - MSFTMicrosoft employee Tuesday, January 21, 2014 7:48 AM
- Marked as answer by Xiaoliang Chen - MSFTModerator Tuesday, January 28, 2014 2:33 AM
- Edited by Chuck Walbourn - MSFTMicrosoft employee Thursday, January 22, 2015 8:15 AM
Tuesday, January 21, 2014 7:47 AM
All replies
-
Can you explain in more detail what you mean? What are you trying to do? You cannot use XNA in a Windows Store app, so a runtime conversion doesn't make sense. There are also several different matrices one can use in Direct3D.
In general the Matrix will have four rows and columns and you can map row to row and column to column between matrices.
- Marked as answer by Xiaoliang Chen - MSFTModerator Tuesday, January 28, 2014 2:33 AM
Monday, January 20, 2014 8:27 PMModerator -
More detail would be helpful. You may be running into convention differences: XNA Game Studio uses Right-Handed Coordinates using a row-major convention. DirectX historically has used Left-Handed, row-major coordinates. Since Direct3D 11 does not have the 'fixed-function' graphics pipeline of Direct3D 9, the choice of graphics math conventions (left-handed vs. right-handed, row-major vs. column-major matrices, etc.) is entirely up to the developer. DirectXMath can be used to create both Direct3D-style "Left-Hand Coordinate" transformations as well as OpenGL-style "Right-Handed Coordinate" transformations using a row-major matrix convention which can be used directly with row-major shaders or transposed to use column-major shaders.
Note: You should look at the SimpleMath wrapper for DirectXMath that is included in the DirectX Tool Kit. It is an adapter that looks very much like the original XNA Game Studio math types implemented using DirectXMath: Vector2, Vector3, Vector4, Matrix, etc.
- Proposed as answer by Chuck Walbourn - MSFTMicrosoft employee Tuesday, January 21, 2014 7:48 AM
- Marked as answer by Xiaoliang Chen - MSFTModerator Tuesday, January 28, 2014 2:33 AM
- Edited by Chuck Walbourn - MSFTMicrosoft employee Thursday, January 22, 2015 8:15 AM
Tuesday, January 21, 2014 7:47 AM -
Hi Dexter!!!
DirectX 11 has two Matrix Classes:
* XMFLOAT4X4 : This structure is pretty compatible with HLSL, to the point that you can use the same shaders that you use in XNA (there is a change in texture sampling but that's the only difference I've found).
* XMMATRIX : This structure is "row-major", which means that the rows are equivalent to the columns of an XNA Matrix. More over, it is rather painful to access individual elements in this structure, in contrast to an XNA Matrix where you could access and change individual members.
Now, when it comes to math operations, all the usefull functions in DirectXMath are for XMMATRIX. XMFLOAT4X4 is rather inconvenient to use.
The good news is that it doesn't matter that there is a difference between XNA and DirectX when it comes to Matrixes, as the math is pretty much the same. DirectXMath has functions that perform the same operations as the methods in an XNA Matrix class (even the names are very similar). The only trick to remembers is that you need to transpose and then conver to XMFLOAT4X4 stucture before sending this information to your shaders. This sounds very complex but all the statements come included in the DirectXTK project template.
Best Regards,
Tarh Ik
PS: This posting has been posted "AS IS"
Tarh ik
Sunday, September 21, 2014 2:49 AM