Advertisement

Left/Right coordinate system and rotation

Started by September 16, 2018 12:12 AM
7 comments, last by Kinjal Kishor 6 years, 4 months ago

I tried to port DirectX program to OpenGL version.  My planets were inverted horizontally (opposite rotation Y axis). Z-coordinate was inverted too.  I figured them out for some months.  I finally recognized that there are two different coordinate systems - left-handed and right-handed.   DirectX uses left-handed coordinate but OpenGL uses right-handed coordinate as default.  I googled it and learned a lot.  I prefer left-handed coordinate system for easy programming instead.

Also I tried google projection matrix formula for left-handed coordinate but did not find any sources.  Does anyone know any matrix formula for left-handed coordinate system?

Does anyone know any good guides for DirectX to OpenGL conversion for porting?

Tim

You can use a left-handed coordinate system in OpenGL (it's entirely up to you).

You'll still need to make sure your projection transform is set up correctly with respect to OpenGL's clip-space z range. If you're still not sure how to do that, post back and I or someone else can probably help.

Advertisement

To go back and forth between left/right handed, jut throw in a scale matrix with (1,1,-1) or (1,-1,1) or (-1,1,1), depending on which axis is different in the two systems. 

The other gotcha is the GL projection matrices must map Z to the [-1,1] range, while D3D uses a Z range of [0,1]. You can convert one kind of projection matrix to the other by concatenating a z scale of x0.5 (or x2) and a translation of +0.5 (or -1).

Ok, how about perspective project matrix? I prefer projection matrix for glLoadMatrix and GLSL.

1 hour ago, Sword7 said:

Ok, how about perspective project matrix? I prefer projection matrix for glLoadMatrix and GLSL.

Are you asking how to construct a left-handed perspective projection matrix for OpenGL?

1 minute ago, Zakwayda said:

Are you asking how to construct a left-handed perspective projection matrix for OpenGL?

Yes, left-handed.

Advertisement

You can find the right-handed version here, among other places (in the section titled 'Remarks'):

https://docs.microsoft.com/en-us/windows/desktop/opengl/glfrustum

For the left-handed version, just negate the 3rd column of the matrix.

There's also a 'clip control' feature available in OpenGL 4.5, and via extension:

https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glClipControl.xhtml

I haven't used it myself, but based on the documentation at least, it appears it allows for matching Direct3D's coordinate system conventions, which might be useful to you.

image.png.6c6802bac6175e18962bd5197462e97d.png

This code converts opengl y up -z forward axis to z up y forward axis. check axis of LHS and RHS Coordinate systems and try to work out what rotation is needed and finally for shortcut which axis needs to reassigned with what sign. Basically difference is in directx +z, becomes -z in open gl. Rotations are anticlockwise in opengl and clockwise in directx. Also NDC space is different in both. Look in matrix_transform.inl file in glm::math library for NDC space in perspective transform. the functions are perspectiveFovRH_ZO and perspectiveFovRH_ZO

image.thumb.png.bf0f2a108cf19b1156062e4473d99999.png

image.thumb.png.83c582c37a0442260a367638b9d8723f.png

You can use glm:: instead of gm::

This topic is closed to new replies.

Advertisement