Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CATransform3D row/column order

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?

like image 510
Martin Cote Avatar asked Feb 07 '11 04:02

Martin Cote


2 Answers

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.

like image 131
Nathan Eror Avatar answered Oct 05 '22 23:10

Nathan Eror


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).

like image 36
idz Avatar answered Oct 05 '22 23:10

idz