# Rotation matrix from OpenGL to DirectX

I have an application which uses openGL and i have to port it to DirectX. To sum up my issue : How can I port rotation matrix based on a right-handed coordinate system to a left-handed coordinate system ? I hardly found some documentation on the internet.

This is what i found on MSDN:

• Flip the order of triangle vertices so that the system traverses them clockwise from the front. In other words, if the vertices are v0, v1, v2, pass them to Direct3D as v0, v2, v1.

• Use the view matrix to scale world space by -1 in the z-direction. To do this, flip the sign of the _31, _32, _33, and _34 member of the D3DMATRIX structure that you use for your view matrix.

This is work fine except i can not flip easily my vertices because i got an 3D model from CATIA which is not a primitive, so i can't use it.

(I'm aware of row-major and column-major difference, it does not matter)

Do you know how i can port my rotation matrix from openGL to DirectX ?

Thanks a lot

• If you are using DirectX 10 or higher you can choose to use right-handed – Roy T. Mar 19 '13 at 11:45
• Thanks but actually i'm using the sotware 3DVIA Studio which use directX, so i can't choose the coordinates-systemes. – Pep Mar 19 '13 at 12:22
• "If you are using DirectX 10 or higher you can choose to use right-handed" - the option of using RH has actually been present since at least D3D8. See, e.g. D3DXMatrixPerspectiveFovRH in the D3D8 documentation. – Maximus Minimus Mar 20 '13 at 13:00
• Better than simply porting it would be to write a platform independent layer that abstracts your game code from the actual platform specific libraries. This way you never have to change stuff like this, you simply choose which math and rendering back end to use based on something like a pre-processor directive or something. – Evan Apr 19 '13 at 14:23
• @Evan: there's no need to have different math backends, you really only need to abstract the projection matrix to deal with depth range differences, and a few coordinate differences easily dealt with. Both GL and D3D (when using the programmable pipeline) are neutral to how you do the rest of your math. – Sean Middleditch Jun 18 '13 at 17:26

I believe, the only difference (besides row/column majorness) is that DirectX uses lefthanded coordinate system and openGL uses righthanded system. You simply need to flip one base vector. Rotation matrix (r) is just a definition of those vectors. So making

r._31 = -r._31;
r._32 = -r._32;
r._33 = -r._33;
r._34 = -r._34;


will just flip the z-coordinates.

The other problem is the triangle ordering (and Front/Back face culling). If you can turn backface-culling off (or switch to frontface-culling), you are OK.

• "the only difference (besides row/column majorness) is that DirectX uses lefthanded coordinate system and openGL uses righthanded system" - sorry but that's totally wrong. Neither API is fixed to either LR/RH or RM/CM - you can use either convention with both. See 9.005 here: opengl.org/archives/resources/faq/technical/transformations.htm - "Column-major versus row-major is purely a notational convention...You can use any notation, as long as it's clearly stated". – Maximus Minimus Mar 20 '13 at 13:03
• oh, OK. That's just about column/row-major - is't it? Of course, it's just a notation problem and all the internal operations of either DX or OGL will perform correctly. You just be aware of that when you need to fill/read this matrices by yourself. Of course right/left handedness can be switched in DX (I suppose OGL can do that as well). But the problem is the default behaviour. I still believe that it's different in DX/OGL. And since Pep is not able to directly interact with DX and change this behaviour, I assumed defaults... Is it still wrong? – GPUquant Mar 20 '13 at 13:21
• D3D can also do RH matrices and has been able to do so since D3D8 if not earlier; really, it's not an API difference but more a software matrix library difference. There is no longer any "default behaviour" in either API since the GL matrix stack is deprecated and D3D has never had an equivalent. – Maximus Minimus Mar 20 '13 at 13:25
• Thanks everyone for yours answers. Actually, i just found a way to manage it easily on this website Transformation matrices between OpenGL and Direct3D – Pep Mar 21 '13 at 13:44