Get eye, target and up vectors from the viewing matrix.

In Direct3D, I use the Matrix.LookAtLH function to calculate the view matrix.

I use this for a camera, which I rotate around the target, moving the origin to the target position, rotating and then moving the origin back to (0,0,0).

This is multiplied by the matrix that was originally calculated from LookAtLH.

Is there a way that I can, after several of these operations, decompose the matrix to get the position of the eye, the position of the target and the vector up?

+4
source share
2 answers

Two options:

  • Long and complex: take your eyes up and look from the new compute matrix
  • Simple version: apply your transforms (translation, rotation, translation) to a copy of your eye, up and lookAt. This way you can easily get new vectors.
+3
source

Matrices extract data very easily. Check the documentation for the function you are using (I used the documentation for D3DXMatrixLookAtLH):

http://msdn.microsoft.com/en-us/library/bb205342%28v=vs.85%29.aspx

As you can see at the bottom of the page, the matrix is ​​internally created as follows:

zaxis = normal(At - Eye) xaxis = normal(cross(Up, zaxis)) yaxis = cross(zaxis, xaxis) xaxis.x yaxis.x zaxis.x 0 xaxis.y yaxis.y zaxis.y 0 xaxis.z yaxis.z zaxis.z 0 -dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1 

Remember that this is true only for DirectX; transpose the above matrix when transferring the same calculations to OpenGL, because the coordinate system in DirectX refers to the norm.

+4
source

All Articles