Math behind LookAt function
Asked Answered
A

1

0

On MSDN(http://msdn.microsoft.com/en-us/library/windows/desktop/bb281710%28v=vs.85%29.aspx) for LookAt function is this math:

zaxis = normal(cameraTarget - cameraPosition)
xaxis = normal(cross(cameraUpVector, 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, cameraPosition)  -dot(yaxis, cameraPosition)  -dot(zaxis, cameraPosition)  1

As far as I know on the last line is the translation. Can you please explain why one can't just put cameraPosition on this line and the dot function is needed?

Apocynthion answered 10/7, 2012 at 20:55 Comment(0)
G
4

That's because the View matrix transforms objects from world space to camera space. It is not the transformation that would place the camera at its position. This would be the inverse transformation. Consider a simple 2D coordinate system:

Coordinate system

The black coordinate system is the world system. The blue one is the camera with its system. The green arrow is the translation vector of the camera in world space.

The View matrix would put the camera from its current position back to the origin. The first step is a rotation, so that the according axes of both systems point in the same direction:

Coordinate system transformed

This is achieved by the other entries of the matrix (m_11 to m_33).

Now there is one translation left. But the translation vector is transformed. Fortunately, we know, how. The translation vector is equal to the camera position (Note, that DirectX uses transposed matrices. In order to show the maths behind, I transposed them back):

/ xAxis.x   xAxis.y  xAxis.z   ...\    / camPos.X \   / camPos.x * xAxis.x + camPos.Y * xAxis.y + camPos.z * xAxis.z \
| yAxis.x   yAxis.y  yAxis.z   ...|    | camPos.Y |   | camPos.x * yAxis.x + camPos.Y * yAxis.y + camPos.z * yAxis.z |
| zAxis.x   zAxis.y  zAxis.z   ...|  * | camPos.Z | = | camPos.x * zAxis.x + camPos.Y * zAxis.y + camPos.z * zAxis.z |
\    0        0         0      .../    \    0     /    \                       0                                     /

And if you have a look at the result:

camPos.X * xAxis.x + xamPos.Y * xAxis.y + camPos.z * xAxis.z

then you see that this is equal to

dot ( camPos, xAxis)

Concluding, the dot product is used to compensate the rotation. Without it, the camera would be somewhat displaced (in the example too far top right).

Goldston answered 10/7, 2012 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.