Pro OpenGL ES for iOS

(singke) #1

CHAPTER 2: All That Math Jazz (^37)
form is called homogeneous coordinates, and in this case it helps to create a 3x3 matrix
that can now be combined or concatenated to other 3x3 matrices.
Why would we want to do this? What if we wanted to do a rotation and translation
together? Two separate matrices could be used for each point, and that would work just
fine. But instead, we can precalculate a single matrix out of several using matrix
multiplication (also known as concatenation) that in turn represents the cumulative effect
of the individual transformations. Not only can this save some space, but it can
substantially increase performance.
In Core Animation and Core Graphics, you will see a number of transformation methods
with affine in their names. You can think of those as transformations (in this case, 2D)
that can be decomposed into one or more of the following: rotation, translation, shear,
and scale. All of the possible 2D affine transformations can be expressed as
x′ =ax+cy+e and y ′ =bx+dy+f. That makes for a very nice matrix, a lovely one
at that:
T=
a b 0
c d 0
e f 1










so
x'
y'
1










a b 0
c d 0
e f 1










x
y
1










Now take a look at the structure CGAffineTransform:
struct CGAffineTransform {
CGFloat a;
CGFloat b;
CGFloat c;
CGFloat d;
CGFloat tx; //translation in x
CGFloat ty; //translation in y
};
Look familiar?
Scaling
Of the other two transforms, let’s just take a look at the scaling, or simple resizing, of an
object:
x'=xSx and y′ =ySy
In matrix form, this becomes as follows:


⎥⎦



⎢⎣


0 0 1
0 0
0 0
y
x
S
S
S

Free download pdf