Bartosz Boczula
Bartosz Boczula

Reputation: 349

How to get current camera position from view matrix?

I've implemented a simple arc-ball camera and it works well - when I use the mouse, I update the View matrix with roll, pitch and yawn.

However, in order to properly calculate specular reflection, I need current camera position in world space which is not updated per-se when applying rotation.

Do you guys know how can retrieve current position from the View matrix? Or is there another way to update this?

Upvotes: 4

Views: 18642

Answers (1)

Sprimesson
Sprimesson

Reputation: 137

You have some simple choices:

  1. Inverse the view matrix by calling XMMatrixInverse or D3DXMatrixInverse so you get the "CameraWorld" matrix. Then its (_41,_42,_43) elements would be the position vector.

  2. Inverse the view matrix (such as last time) but instead of reading forth roe, use XMMatrixDecompose or D3DXMatrixDecompose to get camera position and orientation.

A great idea is to have both View and InvView matrices in your program memory so as to access these info quickly: the matrix inversion is computationally expensive.

  • The view matrix is simply the inverse transform of the camera's world matrix (just forgetting about the Left Handed or Right Handedness of the system).

Upvotes: 7

Related Questions