Skip navigation.
 
mlCATransform3D perspective question
FROM : Nicko van Someren
DATE : Wed Nov 07 20:27:15 2007

I'm having some trouble with using Core Animation's 3D transforms to 
get a perspective effect.  Part of the problem is that the exact 
nature of the transformations is not documented as far as I can tell, 
and it seems not to behave the way that I've seen these transforms 
used before.

The CATransform3D type is a 4 by 4 matrix.  Typically these matrices 
are multiplied by the vector (x,y,z,1) to return a vector (x', y', z', 
s) and the display co-ordinates are (x'/s, y'/s), with z'/s being used 
for z-buffer values if drawing is done with z-buffering (which I don't 
think CA supports).

My initial tests supported my view that this was the way CA was going 
to use the matrix.  Rotations about the z axis put sin(t) and cos(t) 
values into the m11, m12, m21 and m22 slots; translations effect the 
m4{1,2,3} slots; scales work as expected.  Most importantly, the only 
example I could find for applying a perspective transformation was 
three lines of code in the CA "Layer Geometry and Transforms" docs, 
listing 2, which uses the standard technique of putting -(1/distance) 
into the m34 slot (this exact same code also appears in the CoverFlow 
example).  Adjusting the zPosition value for a layer zooms the layer 
in and out.  So far so good.

The problem is simply this; applying a 90 degree rotation about the y 
axis does NOT turn the image edge-on.  I've hooked up a rotary 
NSSlider to an action that makes a transform thus:
   flip = CATransform3DMakeRotation(rotation, 0, 1, 0);
I'm printing this transform and then setting the transform on a layer 
containing an image.  Rotating the slider rotates the image and if the 
slider is set to 90 degrees then the transform is the expected: 1.0 in 
m13, m22, m31 and m44, with zeros everywhere else.  This _should_ 
produce a transform where the output 'x' co-ordinates are invariant of 
the 'x' input (and in fact should be solely dependent on the 'z' 
position).  Unfortunately, when I do this I get an image which looks 
like it's turned by about 75 degrees, definitely not edge-on, and very 
much with the output x co-ordinates dependent on the input x value.  I 
have to turn the slider to about 115 degrees to get the image edge-on.

So, my question is what exactly is the process by which the layer co-
ordinates are converted to the display co-ordinates?  The 
documentation on this seems to be missing and while it looks like it 
should be fairly standard it does not function as expected.  Any help 
would be much appreciated.

   Nicko

Related mailsAuthorDate
mlCATransform3D perspective question Nicko van Someren Nov 7, 20:27
mlRe: CATransform3D perspective question John Harper Dec 1, 18:10
mlRe: CATransform3D perspective question Nicko van Someren Dec 1, 23:43