CATransform3D row/column order

1.5k views Asked by At

I have a bit of confusion regarding the matrix row/column order of the CATransform3D struct. The struct defines a matrix like this:

[m11 m12 m13 m14]
[m21 m22 m23 m24]
[m31 m32 m33 m34]
[m41 m42 m43 m44]

At first, it would seem that the values define rows (so that [m11 m12 m13 m14] forms the first row), but when you create a translation matrix by (tx, ty, tz), the matrix will look like this:

[ 1  0  0  0]
[ 0  1  0  0]
[ 0  0  1  0]
[tx ty tz  1]

My confusion comes from the fact that this is not a valid translation matrix; multiplying it with a 4-elements column-vector will not translate the point.

My guess is that the CATransform3D struct stores the values in column-order, so that the values [m11 m12 m13 m14] form the first column (and not the first row).

Can anyone confirm?

2

There are 2 answers

5
Nathan Eror On BEST ANSWER

Yes, CATransform3D is in column major order because that is how OpenGL(ES) wants it. Core Animation uses GL in the background for it's rendering. If you want proof, check out the man page for glMultMatrix:

PARAMETERS

m Points to 16 consecutive values that are used as the elements of a 4 x 4 column-major matrix.16 consecutive values that are used as the elements of a 4 x 4 column-major matrix.

This really should be more clear in the docs for CALayer.

0
idz On

Your initial interpretation was correct; CATransform3D does define the matrix below:

[m11 m12 m13 m14]
[m21 m22 m23 m24]
[m31 m32 m33 m34]
[m41 m42 m43 m44]

And yes, although it may be confusing (if you are used to pre-multiplying transform matrices), this yields the translation matrix:

[ 1  0  0  0]
[ 0  1  0  0]
[ 0  0  1  0]
[tx ty tz  1]

See Figure 1-8 in the "Core Animation Programming Guide".

This is a valid transformation matrix if you post-multiply your transform matrices, which is what Apple does in Core Animation (see Figure 1-7 in the same guide, although beware the equation is missing transpose operations).