Jump to content
• Advertisement

# Extracting target position from 4x4 matrix

This topic is 2991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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.

#### Share this post

##### Share on other sites
Advertisement

oops, please read:

lightCam->lookAt( mainCameraTarget + lightPosition, mainCameraTarget + lightTarget )

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

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.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Now

• 10
• 17
• 9
• 14
• 41
• Advertisement
• Advertisement
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!