4

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?

Bartosz Boczula
  • 349
  • 1
  • 2
  • 9
  • 1
    Is this a ModelView Matrix? If yes, see https://www.opengl.org/discussion_boards/showthread.php/178484-Extracting-camera-position-from-a-ModelView-Matrix – Arnav Borborah Sep 01 '16 at 20:10
  • Not sure what do you mean by ModelView matrix :P By the View matrix I mean the result of DirectX::XMMatrixLookAtLH(position, target, up) – Bartosz Boczula Sep 01 '16 at 20:15
  • I've added some debug prints and it doesn't add up. Here's the position: `BaseCamera::updateVewMatrix(): position 6.000000,6.000000-10.000000 0.000000` And here is the View matrix: `[[-0.000000][0.000000][1.000000][0.000000]]` `[0.000000][1.000000][0.000000][0.000000]]` `[[-1.000000][0.000000][-0.000000][0.000000]]` `[[0.000000][0.000000][19.999998][1.000000]]` – Bartosz Boczula Sep 01 '16 at 20:31
  • So I guess this is not just simple "grab third row" situation – Bartosz Boczula Sep 01 '16 at 20:36

1 Answers1

7

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).
Sprimesson
  • 137
  • 5
  • thank you, the first method totally works! However, i store my View matrix as XMFLOAT4x4, so in order to get the data, I have to convert it to XMMATRIX, inverse, convert back to XMFLOAT4x4 and then get my data... Do you know how it can be done better? Also, is this InvView matrix used for something else than just to get the camera position? – Bartosz Boczula Sep 02 '16 at 07:00
  • 1
    The matrix would be stored as XMMATRIX to let to compiler store it as high-speed CPU floating point registers (see [SSE](https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions), [3DNow!](https://en.wikipedia.org/wiki/3DNow!), [NEON](https://en.wikipedia.org/wiki/ARM_architecture#NEON), etc.). You may convert XMMATRIX to XMFLOAT4X4 using XMStoreFloat4x4 (and vice versa using XMLoadStoreFloat4x4) – Sprimesson Sep 02 '16 at 07:05
  • So you're saying that it would be better to store XMMATRIX and convert to XMFLOAT4x4 on demand, when I have to do some calculations? – Bartosz Boczula Sep 02 '16 at 07:11
  • 1
    That is a big NO. If you're a new bie with programming, convert them to XMFLOAT4X4 and do simple calculations. But if the calculations are complex and heavy, you must perform them with XMMATRIX instead. – Sprimesson Sep 02 '16 at 07:20
  • 1
    For the record, **be aware that XMMATRIX variables are not usually stored on RAM. So do note play and make funny referencing/dereferencing with them!!** If you want to store data on RAM, you have to use XMFLOAT4X4 or XMFLOAT4X4A (the latter is better for speed but you should be careful when using them.) – Sprimesson Sep 02 '16 at 07:21
  • 1
    _In your case it is a good practice to convert it to XMFLOAT4X4 (also you **can get the forth row of the inverted XMMATRIX using m.r[3] and directly convert it to XMFLOAT3 and get exactly what you want**)_ – Sprimesson Sep 02 '16 at 07:26
  • 1
    You might also find it easier to just use the [SimpleMath](https://github.com/Microsoft/DirectXTK/wiki/SimpleMath) wrapper for DirectXMath from the [DirectX Tool Kit](https://github.com/Microsoft/DirectXTK) which takes care of all the ``XMLoadFloat4x4`` / ``XMStoreFloat4x4`` stuff for you with C++ operators. Great for simplifying usage particularly if you are new to SIMD math libraries. – Chuck Walbourn Sep 08 '16 at 06:21