# OpenGL Extracting camera position from a matrix

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

## 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 on other sites
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 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]);

##### Share on other sites
and how do I do that with DirectX matrices? I'm encountering the same problem

Thanks

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

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 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 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 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 on other sites
Quote:
 Original post by OxyacetyleneSo 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 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?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015730
×