Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I detect if a view matrix is left-handed or right-handed?

I have a camera view matrix which I received from a GL program. I know that this matrix is right-handed since this is the way GL works, but how can I check whether this matrix is right-handed or left-handed programmatically?

For the projection matrix I check if matrix[3][4] (row major) is positive to see if it's left handed. Is this correct?

Thanks.

EDIT:

I have tried the determinant solution, but unfortunately it is not true (at least according to my experiments):

I have used DX9 math functions to test it (to avoid any possible bugs in my code). I have run the following code:

 D3DXVECTOR3 vEye(0,0,0);
 D3DXVECTOR3 vTarget(6,3,0);
 D3DXVECTOR3 vUp(0,0,1);

 D3DXMATRIX matViewLH;
 D3DXMATRIX matViewRH;

 D3DXMatrixLookAtLH(&matViewLH, &vEye, &vTarget, &vUp);
 D3DXMatrixLookAtRH(&matViewRH, &vEye, &vTarget, &vUp);

 float fLHDet = D3DXMatrixDeterminant(&matViewLH);
 float fRHDet = D3DXMatrixDeterminant(&matViewRH);

And the two determinants were equal (both equal to 0.99999994), and obviously had the same sign.

As for my problem - Since I get both the view matrix and projection matrix, and it's relatively easy for me to test whether the projection matrix is LH or RH - I use this information to identify the coordinate system.

like image 807
Asaf Avatar asked Dec 16 '10 18:12

Asaf


People also ask

How do you determine if a coordinate system is right-handed?

Coordinates are usually right-handed. For right-handed coordinates the right thumb points along the z axis in the positive direction and the curl of the fingers represents a motion from the first or x axis to the second or y axis. When viewed from the top or z axis the system is counter-clockwise.

What is the difference between a left-handed coordinate system and a right-handed coordinate system?

In this case, right-handedness is defined as any positive axis (x, y, or z) pointing toward the viewer. Left-handedness is defined as any positive axis (x, y, or z) pointing away from the viewer.

Is OpenGL left-handed or right-handed?

By convention, OpenGL is a right-handed system. What this basically says is that the positive x-axis is to your right, the positive y-axis is up and the positive z-axis is backwards. Think of your screen being the center of the 3 axes and the positive z-axis going through your screen towards you.

Is Direct3D left-handed?

Direct3D uses a left-handed coordinate system. If you are porting an application that is based on a right-handed coordinate system, you must make two changes to the data passed to Direct3D.


2 Answers

You should take the determinant of the matrix. Other things being equal, left-handed and right-handed matrices should have determinants with opposite signs.

It's a little weird because "left-handed" and "right-handed" are pretty much arbitrary conventions. But, matrices that reflect the world like a mirror have negative determinants, so multiplying by such a matrix will change the sign of the determinant.


Edit (ref. updated OQ): I suspect the difference between the D3DX*LH() and D3DX*RH() functions is that they support 2 different conventions.

If so, you should be aware that "left-handed" and "right-handed" is not an attribute of each individual matrix, but of how the matrices generated by these sets of functions fit together. If you stick with the *LH() functions, everything should work out properly; similarly with the *RH() functions -- but if you mix them, you will likely get unexpected results.

In any case, neither of these conventions is relevant to GL, which has its own set of conventions, not fully compatible with either of D3D's. For example, GL's convention for Z clipping is different from D3D's; this means that the projection matrix to generate the same view will necessarily differ between the systems.

So, the short answer to your question is "probably neither".

like image 68
comingstorm Avatar answered Nov 15 '22 12:11

comingstorm


Just bound this same task (just for detecting mirror-transformed objects), found an easy solution, sharing it. If the matrix has a 3x3 part containing 3 perpendicular axes, then you can just make a cross product of axes 1 & 2, then do a scalar product of this result and the remaining 3rd axle. Check if it's negative or positive - you can decide about right/left handedness / object mirrored or not. If I understood the question right...

like image 25
kgz Avatar answered Nov 15 '22 11:11

kgz