Jump to content
  • Advertisement
Sign in to follow this  
ElectroDruid

OpenGL Extracting camera position from a matrix

This topic is 4909 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

I've searched around Gamedev.net (and a few other places) but I can't seem to find an answer to this. I'm doing all manner of crazy matrix manipulations to move my camera about in the world - specifically, doing a sort of opposite quaternion arcball thing (so the camera rotates around the object rather than the object spinning) which involves building a rotation matrix and then applying a transformation in z to move the camera backwards/upwards (depending on how you think about it) to see the object. My matrix class is laid out in the same way the OpenGL ones are, i.e.
0  4  8 12
1  5  9 13
2  6 10 14
3  7 11 15
Where elements 12, 13, and 14 would contain the position, assuming there are no rotations involved (which, of course, there are, which is where things get tricky and weird). So, what do I need to do to get my camera's position in the world from its transformation matrix?

Share this post


Link to post
Share on other sites
Advertisement
I'm not the greatest person with regards to math, but can't you just set a vector to the contents of -12,-13,-14, and multiply it by the 3x3 rotation portion of the matrix?

Share this post


Link to post
Share on other sites
To extract the camera position:


glGetFloatv(GL_MODELVIEW_MATRIX, mdl);
cam_org[0] = -(mdl[0] * mdl[12] + mdl[1] * mdl[13] + mdl[2] * mdl[14]);
cam_org[1] = -(mdl[4] * mdl[12] + mdl[5] * mdl[13] + mdl[6] * mdl[14]);
cam_org[2] = -(mdl[8] * mdl[12] + mdl[9] * mdl[13] + mdl[10] * mdl[14]);


Do that after you've already done your rotations+translations.

Share this post


Link to post
Share on other sites
Quote:
Original post by 999999999
and how do I do that with DirectX matrices? I'm encountering the same problem


Thanks


DirectX matrices are just the same, except that they're transposed, so

0 4 8 12
1 5 9 13
2 6 10 14
3 7 11 15

in DirectX is

0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

typedef struct D3DXMATRIX {
FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44 );
} D3DXMATRIX;

So where it says mdl[12], in DirectX you'd use the _41 member of the D3DXMATRIX structure. (assuming you're using D3DXMATRIX)

Apply this principle to the whole calculation, and you've got your answer.

Share this post


Link to post
Share on other sites
but that calculation is monstruous! I think I'll keep a copy of the view position as I think that calculation would slow the rendering if done many times every frame

Share this post


Link to post
Share on other sites
Not really. It looks kinda ugly from a coder's perspective, but mathmatically it's pretty light. (No Sqrt's or anything like that.) PLUS you'll probably only be doing it once per frame (Can't imagine why you would do it more), so in the end your rendering and other calculations will render this insignificant. If matrix math like this is your bottleneck, you've got serious problems.

That said, in my framework I simply track the camera seprately from the local transformation matrix. Works out quite nicely when doing things like frustum culling.

Share this post


Link to post
Share on other sites
No, I have not problems in that kind of math (though I'm a 15 year old student), but I prefer not using matrices for things like that. I like more vectors...I think I'll keep a copy of the view translation vector and use it instead of taking it from the view matrix

Thanks,999999999

Share this post


Link to post
Share on other sites
Quote:
Original post by Oxyacetylene
So where it says mdl[12], in DirectX you'd use the _41 member of the D3DXMATRIX structure. (assuming you're using D3DXMATRIX)


An observant person might notice that mdl[12] and _41 both refer to the same location in memory. Thus when you say that OpenGL and DX matrices are transposed, you are really just talking about the notation.

Share this post


Link to post
Share on other sites
999.. : Actually, that's all I track about my camera, a single 3D Vector. But I don't think it would provide a good general solution as it doesn't support rotation at all (I don't need it to). You COULD create a fully functional (rotate/translate) camera with two vectors: one for position, one for direction or view target. Then you could just use gluLookAt() and not mess with the matrix stuff.

JohnBolton: Good observation, but doesn't that run the risk of breaking forward/backward compatibility if a driver implements it differently?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!