I have datasets with cameras represented by OpenGL Matrix4x4.
How can I apply these matrixes to an Unity 3D camera?
What I do now is trying to extract the position and rotation, using this code:
public static Quaternion ExtractRotation(Matrix4x4 matrix,bool convertFromRighthanded=true)
{
float s = 1; if (convertFromRighthanded) s = -1;
Vector3 forward;
forward.x = matrix.m02;
forward.y = matrix.m12;
forward.z = matrix.m22*s;
Vector3 upwards;
upwards.x = matrix.m01;
upwards.y = matrix.m11;
upwards.z = matrix.m21*s;
return Quaternion.LookRotation(forward, upwards);
}
public static Vector3 ExtractPosition(Matrix4x4 matrix, bool convertFromRighthanded = true)
{
float s = 1; if (convertFromRighthanded) s = -1;
Vector3 position;
position.x = matrix.m03;
position.y = matrix.m13;
position.z = matrix.m23*s;
return position;
}
//Then, to extract camera rotation and position:
Camera.main.tranform.position=ExtractPosition(openGLcamMatrix);
Camera.main.tranform.rotation=ExtractRotation(openGLcamMatrix);
Note that I am taking into account the handedness being different in Unity and OpenGL.
Still, the code above is not able to recreate the cameras in my opengl data. Am I doing this wrong?