Você está na página 1de 15

Composite Transformations

Sources: * D. Brackeen. Game Programming in Java. * D. Hearn & M. P. Baker. Computer Graphics: C edition * D. Shreiner, M. Woo, J Neider, T. Davis. OpenGL Programming Guide: 5th Edition.

Composite Transformations

How to combine several operations? Ex: rotate, then translate. Or the reverse: translate, then rotate. Or two rotations: rotate this amount, then that amount Or even more complex: scale, then rotate, then translate

Example

Suppose we have a point or vector v We want to rotate, then translate Let R be rotation matrix, T be translation matrix. We would have: v = ...; vr = R * v; vtr = T * vr; Or, all in one step: vtr = T * (R * v) Or, more simply: vtr = T*R*v Notice operations are applied right to left as we read it R first Then T
3

Example

Matrix multiplication is associative! Can move parentheses around however we want! So T * (R * v) is the same as (T*R) * v So what? Suppose we have lots of points to transform Instead of: for p in pointlist: p_ = T*R*p ...do something... We do: M = T*R for p in pointlist: p_ = M*p ...do something...

Examples

Scale an object NOT at origin... Translate to origin (so it doesn't "fly away") Scale Translate back Let S be scale matrix, T be translate matrix, T2 be reverse translation matrix p_ = T2STp Note: if we did it the other way: TST2v it wouldn't work! Matrix multiplication is NOT commutative!!! Again, when we read it, it's applied in RIGHT to LEFT order!
5

Order of Transformations

Another way of looking at it: Suppose we have: p_ = T2STp Rewrite with parens (multiplication is associative): p_ = (T2(S(Tp))) Compute Tp : Translate to origin Then multiply result with S: Scale it Then multiply result with T : Move back to original 2 place

Composites

Makes a difference what order we use! Ex: Rotate, then translate v' = TRv

Composites

Ex: Translate, then rotate v' = RTv

Composite Transformations

Example: rotate object 45 degrees around the point (5,7) Translate object by -5,-7 Call this matrix T Rotate 45 degrees Call this matrix R Translate object +5,+7 Call this matrix T-1 Expressed as matrices: T-1RTv Do this for every point v that makes up the object.

Composite Transformations

Example: scale an object around point 4,5


Translate, scale, translate Matrix notation: T-1STv

10

Row Vectors

What we've just seen assumes column vectors/points: vectors/points are treated as 4x1 matrices If we have row vectors (1x4 matrices), then we combine like this:

To rotate then translate with column vectors: v' = TRv To rotate then translate with row vectors: v' = vRT Notice the order is reversed And now the operations read left-to-right, like we might expect
11

Perspective

What about our perspective transformation? If "virtual view screen" is located d units from origin and we're looking down Z axis: to project point p=(x,y,z) to point p'=(x',y',z'): we do: x' = x * d / -z y' = y * d / -z //negative since looking down Z axis z' = z (save the old z coord!) Matrix for this transformation:

1 0 0 1 0 0

0 0 0

0 0 1 d 0

1 0 0 d

[] ] [

x d x z y x y d 1 y = = z z d 1 q z z d 1

][ ]

12

View Pipeline

Now we can write out our whole view pipeline:

Suppose M represents the combined transformations (any rotations, translations, scale) on the vertices of the object Suppose P represents the perspective matrix. PM = P*M transformed=[] for p in points: transformed.append( PM * p ) And now draw polygon using transformed[...]

Ignore z coordinates; just use x and y Note: will still need to do viewport transform (i.e., map -1...1 into range 0...w-1 and 0...h-1).
13

Viewport Mapping

Viewport mapping can be done with a matrix too! Screen is w x h We map x=-1...1 to range 0...w-1 We map y=-1...1 to range 0...h-1, but need to flip x' = (x+1)/2 * (w-1) = (x+1) * (w-1)/2 = x(w-1)/2 + (w1)/2 y' = h-1 - (y+1)/2 * (h-1) = (h-1)/2 + y(1-h)/2

w1 2 0 0 0

0 1h 2 0 0

0 0 1 0

w 1 2 h1 2 0 1

[] ] [

x w 1 w 1 x 2 2 y y 1h h1 = z 2 2 1 z 1

14

Final Operation

W=model-to-world coordinates P=perspective V=viewport mapping M = VPW for all points: p' = Mp draw

15

Você também pode gostar