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?
To find the rotation of a vector we simply multiply the required rotation matrix with the coordinates of the given vector. In 2D space, this is given by ⎡⎢⎣x′y′⎤⎥⎦ [ x ′ y ′ ] = ⎡⎢⎣cosθ−sinθsinθcosθ⎤⎥⎦ [ c o s θ − s i n θ s i n θ c o s θ ] ⎡⎢⎣xy⎤⎥⎦ [ x y ] .
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:
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With