1
\$\begingroup\$

I understand that a 4x4 matrix holds rotation and position data.

The rotation data is held in the 3x3 sub-matrix at the top left of the matrix.

The position data is held in the last column of the matrix.

e.g.

glm::vec3 vParentPos(
    mParent[3][0], 
    mParent[3][1], 
    mParent[3][2] 
);

My question is - am I accessing the parent matrix correctly in the example above? I know that opengl uses a different matrix ordering that directx, (row order instead of column order or something), so, should the mParent be accessed as follows instead?

glm::vec3 vParentPos(
    mParent[0][3], 
    mParent[1][3], 
    mParent[2][3] 
);

thanks!

\$\endgroup\$
5
\$\begingroup\$

Your first method is the correct one. According to the OpenGL FAQ:

The translation components occupy the 13th, 14th, and 15th elements of the 16-element matrix

It can also be seen in the glm source code (from matrix_transform.inl):

inline detail::tmat4x4<T> translate
/*...*/
{
    detail::tmat4x4<T> Result(m);
    Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
    return Result;
}

You can see only Result[3] is affected. This is called column-major storage, but note that OpenGL can be set up in order to use row-major instead (see the OpenGL FAQ on this).

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also be aware that D3D can be set up to use column major too (in fact this is no longer such a religious issue as it was in the fixed pipeline days) - in other words you can't actually rely on anything! \$\endgroup\$ Nov 27 '11 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy