Cilenco
Cilenco

Reputation: 7179

Get position, rotation and scale from matrix in OpenGL

At the moment I'm learning OpenGL ES for Android development. Now I'm at the point where I have to deal with the ModelMatrix and the CameraMatrix but I have some questions for it: In OpenGL we always use 4x4 matrix and I understand why we do this but I do not know where the different values are stored in this matrix. For example the rotation and the position. Let's say we have this 4x4 matrix:

      0   1   2   3
    -----------------
 0  |   |   |   |   |
    -----------------
 1  |   |   |   |   |
    -----------------
 2  |   |   |   |   |
    -----------------
 3  |   |   |   |   |
    -----------------

Can you please tell me where I can find the current rotation, position and the scale of the object / camera in this matrix? And is this for every matrix in the same position?

Upvotes: 8

Views: 23001

Answers (2)

Reto Koradi
Reto Koradi

Reputation: 54642

To keep things simple, I assume that the matrix only contains a combination of rotation, translation, and uniform scaling. General transformation matrices can represent other types of translations, like:

  • Non-uniform scaling.
  • Shear transformation.

Rotation and scale are in the top-left 3x3 part of the matrix, which are the elements with index 0 to 3 in each direction. If there is no scaling, each row vector of this 3x3 matrix has length 1.0. If there is scaling, you can calculate the scaling factor by calculating the length of any of these row vectors:

scalingFactor = sqrt(m00 * m00 + m01 * m01 + m02 * m02);

The rotation matrix is then obtained by dividing all elements of the 3x3 sub-matrix by the scaling factor:

                                         [ m00 m01 m02 ]
rotationMatrix = (1.0 / scalingFactor) * [ m10 m11 m12 ]
                                         [ m20 m21 m22 ]

The translation vector is in the first 3 elements of the last column:

                    [ m03 ]
translationVector = [ m13 ]
                    [ m23 ]

With all of this, keep in mind that OpenGL expects matrices stored in column-major order. So the arrangement of the matrix in memory is:

m00 m10 m20 m30 m01 m11 ...

Upvotes: 17

datenwolf
datenwolf

Reputation: 162327

Scale and rotation share the upper left 3×3 submatrix; let's call this T for the following.

To separate rotation from scaling, orthonormalize the upper left 3×3 matrix. This is the rotation part, R. Then solve the system of equations

T = R · S

or

T = S · R

depending on if you're looking for a pre or a post rotation scaling S.

Your typical scaling matrix would a diagonal matrix, so you can solve for either case and look which of them zero except for the diagonal.


Translation is the 4th column.


The 4th row is the homogenous coordinate row and unless you're dealing with a perspective projective matrix (i.e. a matrix that incorporates a perspective projection) should always read [0 0 0 1].



Can you please tell me where I can find the current rotation, position and the scale of the object

This is not how OpenGL works. There are no object and no cameras in OpenGL. There are just points, lines and triangles which are drawn, one by one to the screen. And the transformation matrix is used to place them into screen space from their local space coordinates.

Upvotes: 4

Related Questions