I’m thinking about calculating the transformations I applied to my modelview matrix at a certain point of the elaboration… is there a clean way to find rotation, translation and scale given a matrix retrieved with glGet(GL_MODELVIEW_MATRIX)?
The real question is: is it possible? I mean, I can think that the solution won’t be unique, but I need only a good one!
V-man is right. You have to make certain assumptions about the order of transformations; but even then, rotations are inherently ambiguous, as there is always more that one sequence of rotations to achieve a desired basis. This is where your knowledge of the order of transforms comes in handy.
Here’s some code that assumes the order is
yaw(z), pitch(y), roll(x):
I’m displaying a hierarchy of objects, and to do this I push matrixes in the stack while I’m going down in the hierarchy, and pop then when I go up.
Then (let’s say I’m in a leaf of my hierarchy), I want to take the accumulated matrix and store it, then retrieve the translation or the rotation or the scale in that matrix.
I need to do this because I have to perform some calculation with this info (like I have 3 matrixes, and I want to find the baricenter of their translation).
Another way to achieve my goal is to accumulate the transformation while I go through the hierarchy, but it looks even more difficult, and I don’t really know how to do it.
So, I was thinking if there is a way to retrieve the info I need from the final matrix.
I tried with the algorithm of Graphic Gems Graphic Gems
but the matrix come out to be singular… that shouldn’t be!
extracting the translate is easy, you multiply M by a matrix A and add the Identity Matrix, A is of the form
[ 0 0 0 1]
| 0 0 0 1|
| 0 0 0 1|
[ 0 0 0 0]
extracting the scale and rotate is a little more difficult. Instead of trying to treat the rotate as 3 individual rotates in the component axises, treat it as a single rotation about an arbitrary axis. That’s all I have for now, check out mathworld
where (ax, ay, az) defines a unitized axis of rotation, and c and s are the cosine and sine of the angle of rotation.
Now, depending on the order in which you originally multiplied your matrix, this could result in two different matrices:
R*S
[CODE}
[SxRxx SyRxy SzRxz 0]
|SxRyx SyRyy SzRyz 0|
|SxRzx SyRzy SzRzz 0|
[ 0 0 0 1]
I hope you can see and appreciate the subtle difference between the two.
The problem is, there are so many unknown values in the equations (i.e. ALL of them). With the translate, it was sitting out their all by itself, only one unknown value. But here, there are so many more, it's probably impossible.
The algorithm uses a well known algebrical decomposition called Singular Value Decomposition.
I found many link about that, the most interesting are the following: MathWorld Math SVDCode
The last link is great, there is a lot of math code, I’m trying to check if it works.
So, I think I need to implement (or to use) the SVD algorithm.
It would be great!