13-combinetransforms (1)
Transcript of 13-combinetransforms (1)
-
7/30/2019 13-combinetransforms (1)
1/15
1
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.
-
7/30/2019 13-combinetransforms (1)
2/15
2
Composite Transformations
How to combine several operations? Ex: rotate, then translate. Or the reverse: translate, then rotate.
Or two rotations: rotate this amount, then thatamount Or even more complex: scale, then rotate, then
translate
-
7/30/2019 13-combinetransforms (1)
3/15
3
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 leftas we read it R first Then T
-
7/30/2019 13-combinetransforms (1)
4/15
4
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*Rfor p in pointlist:
p_ = M*p
...do something...
-
7/30/2019 13-combinetransforms (1)
5/15
5
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 bereverse 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!
-
7/30/2019 13-combinetransforms (1)
6/15
6
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
2: Move back to original
place
-
7/30/2019 13-combinetransforms (1)
7/15
7
Composites
Makes a difference what order we use! Ex: Rotate, then translate
v' = TRv
-
7/30/2019 13-combinetransforms (1)
8/15
8
Composites
Ex: Translate, then rotatev' = RTv
-
7/30/2019 13-combinetransforms (1)
9/15
9
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-1
RTv Do this for every point v that makes up the object.
-
7/30/2019 13-combinetransforms (1)
10/15
10
Composite Transformations
Example: scale an object around point 4,5 Translate, scale, translate Matrix notation: T-1STv
-
7/30/2019 13-combinetransforms (1)
11/15
11
Row Vectors
What we've just seen assumes columnvectors/points: vectors/points are treated as4x1 matrices
If we have row vectors (1x4 matrices), then wecombine like this: To rotate then translate with column vectors:
v' = TRv To rotate then translate with rowvectors:
v' = vRT Notice the order is reversed And now the operations read left-to-right, like we
might expect
-
7/30/2019 13-combinetransforms (1)
12/15
12
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 pointp=(x,y,z) to point p'=(x',y',z'): we do:
x' = x * d / -zy' = y * d / -z //negative since looking down Z axisz' = z (save the old z coord!)
Matrix for this transformation:
[1 0 0 0
0 1 0 0
0 0 01
d
0 01
d0 ] [
x
y
z
q ]=[x
y1
d
z
d]=[
x d
zy d
z
1
z
1
]
-
7/30/2019 13-combinetransforms (1)
13/15
13
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*Mtransformed=[]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).
-
7/30/2019 13-combinetransforms (1)
14/15
14
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 + (w-
1)/2 y' = h-1 - (y+1)/2 * (h-1) = (h-1)/2 + y(1-h)/2
[w1
2 0 0
w1
2
01h
20
h1
2
0 0 1 0
0 0 0 1] [xyz1 ]=[
xw1
2
w1
2
y1h
2
h1
2
z
1
]
-
7/30/2019 13-combinetransforms (1)
15/15
15
Final Operation
W=model-to-world coordinates P=perspective V=viewport mapping
M = VPW for all points:p' = Mp
draw