Download presentation
Presentation is loading. Please wait.
Published byFelix May Modified over 8 years ago
1
Matrix Transformations Reference: “3D Math Primer for Graphics and Game Development” 7.2.1 – 7.2.2 8-8.2.2, 8.3-8.3.1, 8.4-8.4.1, 8.5-8.7 9.2, 9.4 http://www.teamten.com/lawrence/graphics/homogeneous/
2
Transform Matrices (7.2.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: Expressing this as a matrix*vector expression: (assuming +x = [1,0,0], +y = [0,1,0], +z = [0,0,1])
3
Transform Matrices, cont. Some notation: – We’ll call the 3 columns of M basic vectors – M is a transformation matrix (just because we’re using it to “transform” a vector) Although technically in this case, the matrix doesn’t actually change the original vector. – The first one,, represents the world +x direction. – The second one,, represents the world +y direction. – The third one,, represents the world +z direction.
4
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 a “non-standard” set of 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:
5
Transform Matrices, cont. Let’s watch what happens as we “transform” a few points with this matrix… p3 P3’ p2 P2’ p1 P1’ MysteryMat does a x-axis “Mirror”.
6
Transform Matrices, cont. Another way to look at the X- Mirror matrix we just developed: When we transform v… – We’re taking v, as expressed relative to MirrX’s local coordinate system. The 3 basis vectors = MirrX’s local coordinate system. – By transforming it (multiplying it), we’re converting it to a point in the global coordinate system. Note: In the book, the basis vectors are the rows of MirrX. For us (in a right-handed system), they are the columns, as shown above.
7
Transform Matrices, cont. Let’s try to apply this idea of basis vectors and local coordinate systems to a new matrix (7.2.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)
8
Transform Matrices, cont. worldX worldY b0 = localX 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. 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.
9
Transform Matrices, cont. worldX worldY b0 = localX b1 = localY worldX worldY Let’s take 5 points defined relative to M’s local coordinate system… …and transform (multiply) them by M… …these are the coordinates of the object in world space! p0 p1 p2 p3 p4
10
Transform Matrices, cont. worldX worldY b0 = localX b1 = localY worldX worldY p0 p1 p2 p3 p4 Let’s say these are defined in world space. 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.
11
Rotation Transform (8.2) 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.
12
Rotation Transform (8.2), cont. Put yourself in the direction of +z (looking at the origin): worldX worldY localX localY θ θ Now, let’s define the basis vectors… Sin(θ) Cos(θ) -Sin(θ) Cos(θ)
13
Rotation Transformation (8.2), 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 the transpose of the matrices in the book (since the author uses a Left-handed system)
14
Scale Transform (8.3) 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.
15
Mirror and Shear (8.5 + 8.6) Look in the book… …or you should be able to figure it out yourself
16
Translation (9.4 – 9.4.2) Let’s say we want to develop a matrix for translation (let’s say by amounts tx, ty, and tz). – Seems simple, right? – Wrong! One reason: we can’t phrase this as a change of basis vectors. Try it
17
Translation (9.4 – 9.4.2) Try it another way: Try to come up with values for a-i such that: You can’t do it! and Hint: we’re translating by [+4, +5, +6].
18
Translation, cont.
19
Translations, cont. Represent 2d points in 3d space, with w as 1.0 x y x y w 2D 3D
20
Translations, cont. Now transform by a 3d shear in the x direction by 2.0 units x y x y w 2D 3D
21
Tanslations, cont. So…a shear in 3d, produces a translation in 2d. We can do a translation in 3d by shearing in 4d.
22
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). We'll see another application of this in projections.
23
Translation, cont. Now, we can do translation, by using a 4x4 translation matrix like this: Take our problem cases from before: Because it’s a point It’s still a point
24
Translation, cont. An extra bonus: This one’s a vector It’s still a vector Note how the vector is unchanged. Remember that you can re-draw a vector anywhere and it still has the same length and magnitude – here’s proof!
25
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 matrix embedded in the upper-left. Example: RotZ now becomes:
26
Matrix Inverse (9.3) The inverse of the matrix ( )has the property: If M is a transform matrix, its inverse is the opposite transform.
27
Matrix Inverse, cont. Rotation matrices are an example of an orthonormal matrix. The basis vectors of these matrices have unit length and are perpendicular to each other. Q: How can you determine if three axes: p, q, and r are perpendicular? A: p-dot-q, p-dot-r, and q-dot-r are all 0. The special thing about orthonormal matrices is:
28
Transform Matrix Concatenation (8.7) p0 p1 p2 p3 p4 P0’ P1’ P2’ P3’ P4’ P0’’ P1’’ P2’’ P3’’ P4’’ P0’’’ P1’’’ P2’’’ P3’’’ P4’’’ 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.
29
Transform Matrix Concatenation (8.7) Numerically: After Multiplying by the translation (10,0,0) matrix: After multiplying by the scale (2,2,2) matrix: After multiplying by the rotation of 15 degrees around the z axis:
30
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.
31
Transform Matrix Concatenation, cont. Let’s test it. Transform each of the points by C, and we get… 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…(see next slide)
32
Matrix multiplication costs Matrix4x4 * Matrix4x4 uses 16 dot-products of Vector4’s. Matrix4x4 * Vector4 uses 4 dot-products of Vector4’s. Each Vector4-dot-Vector4 operation involves: – 4 multiplies – 3 adds So… – Matrix4x4 * Matrix4 = 64 multiplies, 48 adds – Matrix4x4 * Vector4 = 16 multiplies, 12 adds Note: The GPU has specialized circuitry for doing this – often much more quickly than the CPU, but it still takes time.
33
Analyzing the costs Let’s say we have 3 transform matrices we want to apply: M1, M2, M3 to n points. – MethodI: Apply the transforms individually. for i in range(n): p1 = M1 * points[i] p2 = M2 * p1 p3 = M3 * p2 # The transformed point – MethodII: Create a combined transform. Then use it to transform all n points. C = M3 * M2 * M1 for i in range(n): p3 = C * points[i] # The transformed point
34
Analyzing the costs, cont What are the costs of the two methods? – MethodI: N * 3 * (Matrix4x4 times Vector4 cost) Which is: – N * 3 * 16 multiplies – N * 3 * 12 adds – MethodII: 2 * (Matrix4x4 times Matrix4x4 cost) + N * (Matrix4x4 times Vector4 cost) Which is: – 2 * 64 + N * 16 multiplies – 2 * 48 + N * 12 adds Which is better?
35
Analyzing the costs, cont Answer: It depends on N? – MethodI: N * 3 * 16 multiplies N * 3 * 12 adds – MethodII: 2 * 64 + N * 16 multiplies 2 * 48 + N * 12 adds Comparison of Method1 vs Method2 (using 3 transforms) Method1 Method2 Verdict? How much better? Nmultaddmultadd 14836144108Method133.3%1/3 of Method2's mult's 29672160120Method166.7%2/3 of Method2's mult's 4192144192144Draw 8384288256192Method266.7%2/3 of Method1's mult's 16768576384288Method250.0%1/2 of Method1's mult's 3215361152640480Method241.7% 64307223041152864Method237.5% 1286144460821761632Method235.4% 25612288921642243168Method234.4% 512245761843283206240Method233.9% 102449152368641651212384Method233.6% 204898304737283289624672Method233.5% 40961966081474566566449248Method233.4% 819239321629491213120098400Method233.4% 16384786432589824262272196704Method233.3% 3276815728641179648524416393312Method233.3% 65536314572823592961048704786528Method233.3% 1310726291456471859220972801572960Method233.3% 26214412582912943718441944323145824Method233.3% 524288251658241887436883887366291552Method233.3% 104857650331648377487361677734412583008Method233.3% 2097152100663296754974723355456025165920Method233.3% 41943042013265921509949446710899250331744Method233.3%1/3 of Method1's mults Note: the 3 above and in the 1/3 below is because we’re doing 3 transforms. If concatenating more than p matrices, we would have 1/p of the cost of MethodI. Verdict: Matrix concatenation, except for very small meshes, is always faster than individual transforms.
36
Rotation Transform, not around the origin A good example of matrix concatenation. Normally, a z-axis rotation of 45 degrees on this: Produces this: 45
37
Rotation Transform, not around the origin, cont. Rotation always happens about the world origin. But what if we want this: To transform to this (rotate about a local origin)? 45
38
Rotation Transform, not around the origin, cont. We can’t change how rotation works… Hint: there are 3 (basic) transforms we need to apply. – 1) Translate the center of the object to the world origin. – 2) Rotate around the z-axis. – 3) Translate the object by the inverse of Step1.
39
Rotation Transform, not around the origin, cont. 1) Translate the center of the object to the world origin. 2) Rotate around the z-axis. 3) Translate the object by the inverse of Step1.
40
Rotation Transform, not around the origin, cont. Now, we can combine these transforms into one composite matrix. Remember to multiply in the reverse order. First step. Second step. Third step. So…to rotate θ radians around the point (tx,ty,tz), use this matrix…
41
Rotation about an arbitrary axis. Let’s say you want to rotate θ around a non- standard axis. Let’s say the axis is the vector
42
Rotation about an arbitrary axis, cont. Three steps: – Align the purple axis with (any) of the 3 (world) axes – this is a rotation. – Rotate θ degrees about that world axis. – Undo the first rotation.
43
Rotation about an arbitrary axis, cont. R1 = Align the purple axis with (any) of the 3 (world) axes – this is a rotation. R2 = Rotate θ degrees about that world axis. R3 = Undo the first rotation. R axis = R3 * R2 * R1 R2 is easy – we’ve already seen that. Let’s assume we’re rotating around the x-axis (but it could be any of the 3 world axes) R1 and R3 seem hard unless you think in terms of basis vectors…
44
Rotation about an arbitrary axis, cont. Let’s take R3 (Undo the first rotation) first. – A rotation. – Rotates the worldX axis to the axis we’re rotating about. So…localX = given axis. – The other local (basis) vectors aren’t shown, but they need to be perpendicular to localX. It actually doesn’t matter what they are – just that they’re perpendicular to localX. Have we ever done anything like this before? YES! With the camera’s local coordinate system in the raytracer!
45
Rotation about an arbitrary axis, cont. 1.LocalX = b0=given axis. (make sure it’s normalized) 2.LocalY = b1=LocalX x (normalize it afterwards) 3.LocalZ = b2=LocalX x LocalY One problem: In Step2, if LocalX is [0,1,0]T, the result of cross-product’ing it will be the zero vector (which means we won’t have a valid rotation matrix). Because of this, you often use this rule instead: if where
46
Rotation about an arbitrary axis, cont. Now, for R1. – Recall: This is the opposite of R3. – In other words, the inverse. – Recall also: The inverse of an orthonormal matrix is simply its transpose! All rotation matrices are orthonormal (test it if you’re not sure)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.