Download presentation
Presentation is loading. Please wait.
1
Matrix Transforms Lecture 9 References:
Matrix Transforms Lecture 9 References: “3D Math Primer for Graphics and Game Development, 2nd ed.”
2
Transform Matrices (4.2 + 5.1)
Take this vector: 𝑣 = − Recall, if we think of this as a point, it means: Start at the origin. Move 0.5 units in the opposite direction of the world x-axis. Move 3 units in the direction of the world y-axis Move 9 units in the direction of the world z-axis. Said mathematically: 𝑣 = −0.5∗ 3∗ 9∗ The unit vectors are the basis vectors of our standard coordinate system. Expressing this as a matrix equation: 𝑣 = − Said another way: “we’re transforming the vector [ ] by the identity matrix.
3
Transform Matrices, cont.
Big deal, right? Yes…but…what if we express that point relative to a “non-standard” coordinate system? In other words, use *non-standard* basis vectors. Example: Let’s assume +y and +z are [0,1,0] and [0,0,1], respectively (as before) But…let’s make +x be [-1,0,0] Now the matrix is:
4
Transform Matrices, cont.
Let’s watch what happens as we “transform” a few points with this matrix… 𝑝 1 = 𝑝 1 ′ = −5 1 2 𝑝 2 = −1 4 3 𝑝 2 ′ = 𝑝 3 ′ = −3 6 0 𝑝 3 = P3’ p3 MysteryMat does an x-axis “Mirror”. p2 P2’ P1’ p1
5
Transform Matrices, cont.
Let’s try to apply this idea of basis vectors and local coordinate systems to a new matrix (4.2 in the book) What does this matrix do? Method1: Try to plot a few points…OK, but not the best solution (we might not pick points that show the transform in action. Also, it takes a lot of time!) Method2: Look at the local coordinate system defined by M…(see the next slide)
6
Transform Matrices, cont.
𝑏 0 = 𝑏 1 = −1 2 0 𝑏 2 = Imagine “deforming” worldX into b0, worldY into b1, worldZ into b2. This is the same “deformation” the matrix will apply to all points that we transform using this matrix. b1 = localY The localX and localY axes are rotated (~27 degrees) and scaled from their worldX and worldY counter-parts. localZ = worldZ, so this transformation leaves the z-values alone. worldY b0 = localX worldX
7
Transform Matrices, cont.
Let’s say these points are defined in world space. 𝑝 0 = 𝑝 1 = 𝑝 2 = 𝑝 3 = 𝑝 4 = When we multiply (transform) them by M (just as we did on the last slide), these points are transformed in the same way the world axes are transformed into the basis vectors of M. 𝑝 0 ′ = 𝑝 1 ′ = 𝑝 2 ′ = 𝑝 3 ′ = − 𝑝 4 ′ = − b1 = localY worldY worldX worldY p0 p1 p2 p3 p4 b0 = localX worldX
8
Rotation Transform (5.1) Now the problem of defining new transforms just boils down to defining the (transformed) basis vectors. Let’s apply this to the problem of creating a matrix which rotates θ degrees around the z axis.
9
Rotation Transform (5.1), cont.
We’re looking in the +z direction: Now, let’s define the basis vectors… worldX worldY localY Cos(θ) localX θ Sin(θ) θ -Sin(θ) Cos(θ)
10
Rotation Transformation (5.1), cont.
So, if we collect the 3 local vectors (basis vectors into a matrix), we have… We can similarly come up with matrices for x-axis and y- axis rotations… Note: These matrices are for Left-Handed Systems. A Right-handed system would use the transpose.
11
Scale Transform (5.2) The scale transform matrix is even simpler:
worldX worldY worldZ Let’s say we want to scale: 2x in the x-direction 1x in the y-direction ½ in the z-direction localX localY localZ In general: sx in the x-direction sy in the y-direction sz in the z-direction If sx = sy = sz, the operation is called a universal scale.
12
Shear (5.5) Left-hand is an identity matrix Middle: 𝑥 1 = 𝑥 = 𝑧 1 = 𝑧 = 𝑦 1 ≈ Right: 𝑦 1 ≈ 𝑥 𝑦 𝑥1 𝑦1 𝑥2 𝑦2 𝑆𝐻 1 = 𝟎.𝟑 𝑆𝐻 2 = 𝟎.𝟖
13
Shear in 2 directions 𝑥′ ≈ 0.9 0.1 0 𝑦′ ≈ 0.5 1 0
𝑥′ ≈ 𝑦′ ≈ 𝑆𝐻 𝑥𝑦 =
14
Translation (6.4) Let’s say we want to develop a matrix for translation (let’s say by amounts tx, ty, and tz). Seems simple, right? Wrong! Try it [I’ll wait] Why can’t we do this? If we translate [tx, ty, tz], our world basis vectors are the same (identity matrix) That doesn’t work… Up to this point we've been looking at matrices that operate about the origin. These have all been do-able Translation is an example of a matrix that changes the origin (i.e. we move). We can’t do this is 3d with a 3x3 matrix. You need to look at the problem in a higher dimension… …enter homogeneous space!
15
Translations, cont. 2D Homogeneous (2d) space y w y x x
Represent 2d points in 2d homogeneous space, with w as 1.0 Note: I’m not calling that other axis z for a reason! We’re still in 2d. If a viewer exists in a 2d world, they are viewing down along the –w axis in homogeneous space… x y 2D 1.5 2 3.5 2 x y w Homogeneous (2d) space
16
Translations, cont. 2D Homogeneous (2d) space 1 0 0 0 1 0 2 0 1 y w w’
Now, to create the illusion that we’re translating in 2d space, we shear in homogeneous space! …it appears to the 2d viewer that a translation (of +2x just happened)! y w w’ y x 2D x Homogeneous (2d) space
17
Translations, cont. Homogeneous (2d) space 2D
We can do a x/y translation using a xy shear matrix in homogeneous space. w y y x x Homogeneous (2d) space 2D
18
Tanslations, cont. So…a shear in 3d, produces a translation in 2d.
We can do a translation in 3d by shearing in 4d. We can't really visualize it – you just have to trust the math
19
Translation, cont. Note, if you put a w of 0, the translation doesn't affect the vector Useful for vectors. W should be: 1 if this is a point. 0 if this is a vector (direction + magnitude). Example Translating points (w=1) = ∗ − = − ∗ − Translating vectors (w=0) − = − ∗ − No change (drawing a vector somewhere else – it’s still the same vector!) Subtracting points: note how it produces a vector!
20
4x4 Transformation matrices
OK, so now that we’re using 4x4 matrices for translation, what about all the other matrices? You can use 4x4 matrix, with the “old” 3x3 (linear) matrix embedded in the upper-left. Example: RotZ now becomes:
21
Transform Matrix Concatenation (5.6)
Suppose you want to apply the following transforms to an object (in this order): Translate (10,0,0) Scales (2, 2, 2) Rotate 15 degrees around the z-axis. P0’’’ P1’’’ P2’’’ P3’’’ P4’’’ P0’’ P1’’ P2’’ P3’’ P4’’ P0’ P1’ P2’ P3’ P4’ p0 p1 p2 p3 p4
22
Transform Matrix Concatenation (8.7)
𝑝 0 = − 𝑝 1 = 𝑝 2 = 𝑝 3 = 𝑝 4 = − After Multiplying by the translation (10,0,0) matrix: 𝑝 0 ′ = 𝑝 1 ′ = 𝑝 2 ′ = 𝑝 3 ′ = 𝑝 4 ′ = After multiplying by the scale (2,2,2) matrix : 𝑝 0 ′′ = 𝑝 1 ′′ = 𝑝 2 ′′ = 𝑝 3 ′′ = 𝑝 4 ′′ = After multiplying by the rotation of 15 degrees around the z axis: 𝑝 0 ′′′ = 𝑝 1 ′′′ = 𝑝 2 ′′′ = 𝑝 3 ′′′ = 𝑝 4 ′′′ =
23
Transform Matrix Concatenation, cont.
You can get the same net effect by concatenating these 3 transforms. VERY IMPORTANT: In a right-handed system, multiply in the reverse order you want to apply the transform (In a left-handed system, you do it in the forward order). You won’t get the same composite matrix if you don’t follow this rule.
24
Transform Matrix Concatenation, cont.
Let’s test it. 𝑝 0 = − 𝑝 1 = 𝑝 2 = 𝑝 3 = 𝑝 4 = − Transform each of the points by C, and we get… 𝑝 0 ′′′ = 𝑝 1 ′′′ = 𝑝 2 ′′′ = 𝑝 3 ′′′ = 𝑝 4 ′′′ = Which is the same points we got by transforming by the individual matrices! Big Deal, right? No! To see why, we’ll need to analyze the costs…[enough time?]
25
Scene Graphs Every game engine worth its salt uses scene graphs
Limits the amount of geometry rendered Allows hierarchical transformations. The skeletal system [Other uses]
26
Scene graphs Basic idea The graph is a tree of nodes
Matrix: a transformation from the parent node Maybe a renderable (Polymesh) – this is often empty, though. [Not actually stored] a local coordinate system – the cumulative effect of all ancestor transforms. Each node can have 0 or more child nodes Their transforms are relative to the current node.
27
Scene graph example y x y y E x D B x y A x
𝑀 𝐴 =𝐼 Desk mesh x y B 𝑀 𝐵 =𝑇(−5,5,0) 𝑀 𝐶 =𝑇(5,5,0) 𝑀 𝐷 =𝑅𝑧( −45 ○ ) BBall mesh 𝑀 𝐷 =𝑅𝑧( 45 ○ ) x y A 𝑀 𝐺 =𝑆(0.1, 0.1, 0.1) Lamp mesh 𝑀 𝐸 =𝑇(4,0,0) Vader mesh 𝑀 𝐹 =𝑇(0,4,0) Gnome mesh 𝑀 𝐻 =𝑇(−5,5,0) CCat mesh Note: Any of these transform matrices could be a concatenated transform. [Show the other local coordinate systems on the board]
28
Scene graph example If an interior node’s transform is changed…
𝑀 𝐴 =𝐼 Desk mesh 𝑀 𝐵 =𝑇(−6,4,0) * 𝑅𝑧( −10 ○ ) 𝑀 𝐵 =𝑇(−5,5,0) 𝑀 𝐶 =𝑇(5,5,0) 𝑀 𝐷 =𝑅𝑧( −45 ○ ) BBall mesh 𝑀 𝐷 =𝑅𝑧( 45 ○ ) 𝑀 𝐺 =𝑆(0.1, 0.1, 0.1) Lamp mesh 𝑀 𝐸 =𝑇(1,0,0) Vader mesh 𝑀 𝐹 =𝑇(0,1,0) Gnome mesh 𝑀 𝐻 =𝑇(−5,5,0) CCat mesh If an interior node’s transform is changed… (e.g.) MB is set to Rz(-10○) * T(-6, 4, 0)…
29
Scene graph example The net transformation can be calculated by traversing the tree. 𝐷𝑒𝑠𝑘𝑇= 𝑀 𝐴 𝐵𝐵𝑎𝑙𝑙𝑇= 𝑀 𝐷 ∗ 𝑀 𝐵 ∗ 𝑀 𝐴 𝑉𝑎𝑑𝑒𝑟𝑇= 𝑀 𝐹 ∗ 𝑀 𝐷 ∗ 𝑀 𝐵 ∗𝑀 𝐴 = 𝑀 𝐹 ∗𝐵𝐵𝑎𝑙𝑙𝑇 LampT = 𝑀 𝐻 * 𝑀 𝐸 ∗ 𝑀 𝐶 ∗ 𝑀 𝐴 𝐶𝐶𝑎𝑡𝑇= 𝑀 𝐽 ∗ 𝑀 𝐻 ∗ 𝑀 𝐸 ∗ 𝑀 𝐶 ∗𝑀 𝐴 = 𝑀 𝐽 ∗𝐿𝑎𝑚𝑝𝑇 … You can do this in code: def render(self, M): C = self.mMat * M # Transform my verts w/ C # “Draw” my faces for bob in self.mChildren: bob.render(C) # Recursion! 𝑀 𝐴 =𝐼 Desk mesh 𝑀 𝐵 =𝑇(−5,5,0) 𝑀 𝐶 =𝑇(5,5,0) 𝑀 𝐷 =𝑅𝑧( −45 ○ ) BBall mesh 𝑀 𝐸 =𝑅𝑧( 45 ○ ) 𝑀 𝐻 =𝑆(0.1, 0.1, 0.1) Lamp mesh 𝑀 𝐹 =𝑇(1,0,0) Vader mesh 𝑀 𝐺 =𝑇(0,1,0) Gnome mesh 𝑀 𝐽 =𝑇(−5,5,0) CCat mesh
30
LHS / RHS “cheat-sheet”
Differences in LHS (Left-handed systems) / RHS (right-handed systems) The way you draw objects and the axes (duh!) Direction of result of cross-product The way we “draw” vectors (as row or column) The way we transform vectors (vector * Matrix in LHS; Matrix * vector in RHS) Basis vectors are rows (LHS) / columns (RHS) in a transformation matrix. When looking opposite to the axis of rotation, rotations looks clockwise in LHS / counter-clockwise in RHS. Transpose a transformation matrix to convert to / from LHS and RHS. Matrix concatenation is in-order in LHS / reversed order in RHS.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.