Sunday, March 8, 2009

Transformation matrices between OpenGL and Direct3D

In these days, I am working on an augmented reality (AR) applications on mobile phones. The mobile phone I'm working on adopted Windows Mobile as its OS. I'd prefer iPhone, but the project manager chose the WM6-based mobile phones. :-(.
Anyway, at first, I started to port conventional OpenGL codes to WM6. As the 3D rendering library, I had two choices, OpenGL ES and Direct3D Mobile.

I tried OpenGL ES, since I could re-use my marker tracking codes I wrote on my desktop. However, the mobile phone I'm using shows very poor performance for AR application. The low performance may be due to the lack of floating point unit and 3D graphics acceleration chip.

Then, I tried Direct3D Mobile (D3DM) samples provided with SDK and D3DM seems to show better performance (faster framerate). Finally, I decided to port my OpenGL codes to D3DM.

I could use most of my codes since they are platform-independent C++ codes. The only problem is using my marker tracking module. The marker tracking module assumes the right-handed coordinate system, but D3DM uses left-handed coordinate system. Of course, Direct3D provides functions for right-handed coordinate system, but D3DM does not have those functions (Some of functionalities are missing in D3DM).

There are many questions and discussions about this problem on communities on the web. However, most of them does not give me a clear answer. After I searched and wrote some equations, I finally found how to change a transformation between OpenGL and Direct3D (well, between right-handed coordinate system to left-handed coordinate system).

The answer is quite simple.

1. From OpenGL Modelview matrix to D3DM view matrix : We just need to transpose the matrix, and change the signs of some elements.



2. From OpenGL projection matrix to D3DM projection matrix : In the same way, we transpose the matrix and change some elements' signs.



Now, we can use your parameters or transformations in both OpenGL and Direct3D.

2 comments:

  1. what is the difference between opengl and opencv matrix

    ReplyDelete
  2. Hi Wonwoo Lee, thanks for the post. Can you please include a snapshot of the equations you wrote to do achieve the matrix conversions? I am asking, since it will help with conversions between any two 3D systems, like OpenCV and Unity.

    ReplyDelete