Simon
Simon

Reputation: 13283

How to get the camera rotation matrix out of the model view matrix

If I have the modelview rotation matrix (which I also can use in glMultMatrixf(rtMatrix, 0) for example) then how can I calculate out of this the rotation around the virtual camera. I think converting the position t is just camPos=-1*t but how do I get the rotation around the camera when i have the rotation around the world center (which is the model view matrix right?)

Upvotes: 2

Views: 2501

Answers (1)

datenwolf
datenwolf

Reputation: 162164

Take the inverse of the upper left 3×3 submatrix of the modelview matrix:

                    -1
    / M00 M10 M20 \
O = | M01 M11 M21 |
    \ M02 M12 M22 /

Done.

Update Code Sample due to request in comments by InkBlend

/* datenwolf's linmath.h, available at
 * https://github.com/datenwolf/linmath.h
 */
#include <linmath.h>

void view_orientation(mat4x4 O, mat4x4 MV)
{
    mat4x4_dup(O, MV);
    O[0][3] = O[1][3] = O[2][3] =
    O[3][0] = O[3][1] = O[3][2] = 0.;
    O[3][3] = 1;
    mat4x4_invert(O, O);
}

Upvotes: 2

Related Questions