Decomposing model view matrix

I am manipulating model view matrix myself by
concatenating some trackball rotation to the existing model view matrix. I want to extract the camera’s src, dest and up vector from the mv matrix, can anyone know/implemented to get the 3 camera vectors from the mv matrix? pseudo/sample is welcome.
Thanks a lot,

Assuming “ModelView” is a 16-float array holding your matrix:

float9 rotation = upper left part of ModelView;
float9 transpose = transpose of rotation;
vector sight = (0,0,1) transformed by transpose;
vector up = (0,1,0) transformed by transpose;
vector side = (1,0,0) transformed by transpose;

Hey, I have a similar problem and having difficulty solving it.

Let’s say we have something like

glRotatef(…);
glTranslatef(…);
glRotatef(…);

and you end up with a matrix. Has anyone attempted to decompose such a matrix back into the commands?

I’m using Maple to try to solve it but it seems to fail.

For some heavy duty decomposition, see:

Graphic Gem IV
Polar Matrix Decomposition by Ken Shoemaker
decomp/

/* Decompose 4x4 affine matrix A as TFRUK(U transpose), where t contains the

  • translation components, q contains the rotation R, u contains U, k contains
  • scale factors, and f contains the sign of the determinant.
  • Assumes A transforms column vectors in right-handed coordinates.
  • See Ken Shoemake and Tom Duff. Matrix Animation and Polar Decomposition.
  • Proceedings of Graphics Interface 1992.
    */

I don’t really understand what U and K are for, but it seems to work.

Too bad I don’t have that book, but I found a PDF that refrences the Gems book 2 and 4.

Do you have Gems 2? Look at page 320-323.
“Decomposing a matrix into simple transformations”

Thanks, I found the code you were referring to here (unmatrix.c.):
http://www.acm.org/pubs/tog/GraphicsGems/

I haven’t tried it yet. Needs some hacking to fit into my
code. Does it work?

Maybe this can help : http://www.makegames.com/3Drotation/
Although be careful, it is written for left handed coordinate system and OpenGL’s is right handed.

Matrix decomposition and Matrix extraction mean two different things to
me. The original poster was probably looking for some simple matrix
extraction. Just extract the rows or cols whether you are using a LH
or RH matrix. Matrix decomposition was something another poster was after.
As in stripping the matrix down to its purest elements of translation,
rotation, and scale.

Originally posted by V-man:
[b]
Let’s say we have something like

glRotatef(…);
glTranslatef(…);
glRotatef(…);

and you end up with a matrix. Has anyone attempted to decompose such a matrix back into the commands?
[/b]

How do you imagine solution for this?
You could quite easily find one of them, but there’s infinite number of ways of decomposing (no matter how you call it) it to glRotates, glTranslates etc…

You only know start and end points of a dog. It’s not enough to say what way did it go to change its position

Originally posted by gator:
[b]Thanks, I found the code you were referring to here (unmatrix.c.):
http://www.acm.org/pubs/tog/GraphicsGems/

I haven’t tried it yet. Needs some hacking to fit into my
code. Does it work? [/b]

It doesn’t take much hacking, but I didn’t try it out since I don’t know what exactly it attempts to do.
The sequence of transformations seems to be
scale, shear, rotate, translate, P(?)

I’m guessing P means project.

Anyway, I think it is solvable because you can start by one of the elements of the matrix and figure out the rest.

When you have
rotate, translate, rotate

the problem becomes immedialty much more difficult. Someone who knows Maple’s other packages tried it out for me and the result was 1, meaning no trivial solution.

Originally posted by MickeyMouse:
How do you imagine solution for this?
You could quite easily find one of them, but there’s infinite number of ways of decomposing (no matter how you call it) it to glRotates, glTranslates etc…

I don’t imagine! But it’s worth looking at the problem and having an algorithm that attempts to find it (by any mean necessary) would be nice to have.