2

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?)

Simon
  • 13,173
  • 14
  • 66
  • 90

1 Answers1

2

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);
}
Community
  • 1
  • 1
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • @InkBlend: I added a code sample. But I don't see, how that makes things clearer. – datenwolf Jun 26 '13 at 21:23
  • I just like to have code snippets in answers wherever possible. It helps with teaching less experienced coders like me, as they say that reading code helps you to write (better) code. – fouric Jun 27 '13 at 03:21
  • @InkBlend: Reading code makes sense only in larger contexts. A code snippet like the above easy enough to produce. But when you write larger projects things become less obvious. In [→this source code](https://github.com/datenwolf/codesamples/blob/master/samples/OpenGL/frustum/frustum.c) I wrote for a tutorial, there is a small portion that does exactly what was asked for in this question. Yet the actual implementation looks very different from what I wrote in the answer (partly because I didn't use linmath.h). *Can you find it?* – datenwolf Jun 27 '13 at 08:41
  • @InkBlend: If you look at frustum.c and just focus on the *math* it should be obvious to see. But if you focus on the code, you'll probably miss it. – datenwolf Jun 27 '13 at 08:43
  • Ah. When I heard that it was good to read code, I didn't know that they meant most or all of the code, not just a snippet. Thanks for clearing that up! – fouric Jun 27 '13 at 16:47
  • instead of code I would like to get some more infos about the theorie of this ;) why is it the inverse? I already tried the inverse and had some problems, but I will try again and check if I did everything correct. I also figured out by testing that the camera position is camPos=R*t, can you confirm this? – Simon Jun 28 '13 at 07:23
  • @Sponge: The theory behind this is, that in OpenGL there is no camera at all. All there is, is a transformation that moves around geometry from some local model space into view space. This transformation is called the modelview. But it can also be thought of (inversely) moving the view space into model space. The upper left 3×3 submatrix contains the orientation, the rightmost column the translation. If you want to know just the view orientation relative to the model, all you have to do it take the inverse of the orientation transform from model to view space. – datenwolf Jun 28 '13 at 09:57
  • @Sponge: What may have stumped you is, that OpenGL uses to use a column major index ordering, which make the matrices look transposed if used by a naive assumption of row major ordering. – datenwolf Jun 28 '13 at 09:59
  • thanks already for your help, I think the solution is correct but sadly i could not confirm it yet because trying to solve this caused new questions, i wrote them down here it would be great to get you feedback on this: http://stackoverflow.com/questions/17414296/inverting-axes-in-opengl-or-rotation-by-180-degree – Simon Jul 01 '13 at 21:57
  • @datenwolf can you please go through this http://stackoverflow.com/questions/39816422/understanding-the-modelview-matrix – user6250837 Oct 02 '16 at 11:47