Extracting target position from 4x4 matrix

Started by
3 comments, last by Nik02 14 years, 1 month ago
Hello, The context of this question is to setup Light Camera matrix in vue to support shadow maps. I control my main camera by changing its view angles and its position. At the moment for shadow maps, all my lights are directional. So when I want to setup my Camera Light Matrix, I do something like: lightTarget = vec3(0) lightPosition = -lightDirection lightCam->setPosition( mainCameraTarget + lightPosition, mainCameraTarget + lightTarget ) I do this so my light camera looks at the same target as the main camera. My problem is, as stated above, that I change main camera's angles, and loose where the main camera is actually looking... so I guess is there a way to extract a camera's target using its 4x4 matrix ? Btw, if my approach regarding setting up the Camera for the light is wrong, please let me know. In advance thank you for your help, Dan.
Advertisement

oops, please read:

lightCam->lookAt( mainCameraTarget + lightPosition, mainCameraTarget + lightTarget )
If you consider the camera as a world-space object, you can take the inverse of the view matrix to get both the position and the orientation of the camera object.

The "lookat" functions construct the orientation frame's z vector by normalizing the (target-position) vector, crossing it with the "up" vector to get x vector and finally crossing z and x to get the y vector.

The normalization means that the view matrix doesn't have all the info of the original "target point"; rather, it does store a ray from the camera pointing to the target, but the matrix doesn't encode how far the target is from the camera. The ray is [m1,2, m2,2, m3,2] (zero-based subsets) and the origin is the last row of the matrix negated.

Niko Suni


Thanks Nik02!

To keep going with my test, I've hardcoded my vectors. I'm sorting out some shader issues right now, but I'll clean this up base on your recommendation above.

Thanks again,
Dan.
I would solve the problem by storing the camera parameters (target, origin etc.) in your camera class (you do have a camera class, right?), rotating the target point about the origin when you need to rotate the camera about, and constructing the view matrix as the last step before sending it to the shader constants.

This way, you always have access to the actual points of interest without trying to hack them from the matrix. In addition, it is not possible to obtain the actual target point on the ray from the matrix, as I explained in the previous reply.

Niko Suni

This topic is closed to new replies.

Advertisement