Sign in to follow this  
nomonkeybusiness

Converting matrices between OGL and DX?

Recommended Posts

Hiya. I've been bashing my head lately while trying to convert a row-major matrix to column-major. I have this function in my GLRenderingSystem to do just that.
void ViewToGL(float *p_pfM,Matrix4 &p_xM)
	{
		p_pfM[0] = p_xM.m11; p_pfM[4] = p_xM.m21; p_pfM[8 ] = p_xM.m31; p_pfM[12] = p_xM.m41;
		p_pfM[1] = p_xM.m12; p_pfM[5] = p_xM.m22; p_pfM[9 ] = p_xM.m32; p_pfM[13] = p_xM.m42;
		p_pfM[2] = p_xM.m13; p_pfM[6] = p_xM.m23; p_pfM[10] = p_xM.m33; p_pfM[14] = p_xM.m43;
		p_pfM[3] = p_xM.m14; p_pfM[7] = p_xM.m24; p_pfM[11] = p_xM.m34; p_pfM[15] = p_xM.m44;
	};
This works out fine, the thing is, I also want to negate the Z-axis, so I can rest assured that objects will stay the same whether I'm rendering with OGL or D3D. So it tried changing
p_pfM[14] = p_xM.m43;
to
p_pfM[14] = p_xM.m43 * -1
At first i thought this worked fine, but then I tried rotating the camera, and as a result the camera didn't rotate around itself, but around an unknown point. The rotation in D3D works out as expected though. Can someone help me with the logic here? I'm about to go crazy.

Share this post


Link to post
Share on other sites
If I understand correctly you're just flipping the z axis of the translation vector (located in positions 12, 13, and 14). I think what you want to do is flip the basis vector, so you'd need to multiply components 8, 9, and 10 by -1.

Share this post


Link to post
Share on other sites
If you want to get consistent results between OpenGL and Direct3D, I'd recommend simply using the same coordinate system handedness (right or left) for each API. Then you won't have to do any swapping, flipping, negating, etc. - everything should just work.

Using a right-handed coordinate system with D3D is no problem; I assume it's equally easy to use a left-handed system with OpenGL, although I haven't tried it myself (in my own framework I use a right-handed system for both APIs, which works fine).

If you use the same handedness for both APIs, for the most part, you should be able to use the exact same matrices for each (depending on what you're doing exactly, you might not even have to transpose them). One thing to watch out for though is that the canonical view volumes for the two APIs are different, which means that projection matrices need to be set up a little differently for each API.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this