1 / 50

Computer Graphics

Learn the mathematical fundamentals of matrix operations, transformation sequences, and 2D/3D transformations in computer graphics. Understand how matrices are used to represent equations and efficiently transform shapes. Explore translation, rotation, scaling, reflection, shear, and identity transformations, and how to compose them into transformation sequences. Dive into 3D transformations and discover how to perform translation and rotation in a 3D space.

rickye
Download Presentation

Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Graphics Mathematical Fundamentals

  2. Matrix Math • Matrix dimensions are specified as: rows x cols 2x4

  3. Matrix Addition • The dimensions must match to add + = 2x2 2x2 2x2

  4. Scalar Multiplication • The dimensions can be any size to perform scalar multiplication x = 5

  5. Matrix Multiplication col 2 row 1, col 2 row 1 x = 2x3 3x3 2x3 1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36 • rows from the first matrix • columns from the second matrix

  6. Matrix Multiplication • “inner” dimensions must match • result is “outer” dimension • Examples: • 2x3 X 3x3 = 2x3 • 3x4 X 4x5 = 3x5 • 2x3 X 4x3 = cannot multiply • Question: Does AB = BA? • Hint: let A be 2x3 and B be 3x2

  7. Matrices and Graphics • Matrices are used to represent equations • Used as an efficient way to represent transformation equations in graphics • Multiple transformations can be composed into a single matrix • Matrix multiplications are often optimized in hardware

  8. Transformations • View a complex shape as a set of points • To transform the shape, one transforms each of the points that define the shape • Major types of transformations: • Translation • Rotation • Scaling

  9. Translation • The equation to translate a single point(x, y) by tx in the x direction and ty in they direction: x’ = x + tx y’ = y + ty Resulting in the new point (x’, y’)

  10. Translation • These equation can be represented using matrices: P = P’ = T = • Then using matrix math we simply have: P’ = P + T

  11. Rotation • The equations to rotate a point through an angle, q (ccw), around the origin: x’ = x cos(q) – y sin(q) y’ = x sin(q) + y cos(q) • In matrix form: R = • Using matrix math: P’ = R P

  12. Scaling • The equations to scale a point along the coordinate axes: x’ = x sx y’ = y sy • Using matrix math: P’ = S P • Where S = ?

  13. Scaling • Scaling is used to grow/shrink objects centered on the origin • s numbers bigger than 1 grow objects • s numbers smaller than 1 shrink objects • uniform scaling: both s numbers the same

  14. Transformation Sequences • In general you need to perform a sequence of transformation: This is a rotation followed by a translation P’ = R P + T Do a dimension check to make sure the math will work out: • R is 2x2 and P is 2x1, thus R P is 2x1 • R P is 2x1 and T is 2x1, thus P’ is 2x1

  15. Transformation Sequences • However, it is often more efficient if we can combine operations into a single matrix calculation • How can this be done? • Here are the equations: x’ = (x cos(q) – y sin(q) ) + tx y’ = (x sin(q) + y cos(q) ) + ty

  16. Transformation Sequences • Put them into matrix form: • The only problem is that our original point, P, now need a 3x1 matrix while the resulting point, P’, needs a 2x1 matrix =

  17. Homogeneous Coordinates • We resolve this using homogeneous coordinates • Lots of theory here, but… • Basically boils down to all points being represented as a triple (x, y, 1) • Thus, we need to fix our transformation matrix so that P’ comes out as a triple

  18. Homogeneous Coordinates • This 3x3 matrix is called a transformation matrix • This particular matrix rotates then translates • However, it is often easier to work with transformations matrices that handle one type of transformation at a time, then compose then to obtain our final transformation =

  19. 2D Transformation Matrices • Translation: T(x, y) • Rotation: R(q) • Scaling: S(x, y)

  20. Transformation Composition • Example: How do you rotate point P about a random “pivot point”? • Note that normal rotation has the origin as a pivot • Translate P so pivot point lines up with the origin • Rotate P around origin • Translate P back so pivot point is back on original position

  21. Rotation Around Pivot Point P P pivot pivot P' P pivot pivot

  22. Rotation Around Pivot Point • Assume pivot point is at (x, y) • Assume P is the point you wish to rotate P’ = T(x, y) R(q) T(-x, -y) P • Recall that order make a huge difference!

  23. Other Transformations • Reflection • about x axis (left) • about y axis (right) • Shear • along x axis (left) • along y axis (right) • Identity

  24. Transformation Between Coordinate Systems P y' y x' x • Point P is represented in the original coordinate system. To transform it to the new coordinate system one needs to know how this system is oriented and positioned relative to the original coordinate system.

  25. 3D Transformations • I’ll be using a right-handed coordinate system • Positive z-axis is sticking out of the screen • Dashed lines will be usedfor lines that stick intothe screen y x z

  26. 3D Transformations • Points now have 3 coordinates (x, y, z) • Written in homogeneous coordinates as (x, y, z, 1) • Since the points are now 4x1 matrices, the transformation matrices need to be 4x4

  27. 3D Translation T(x, y, z):

  28. 3D Rotation • 3D rotation is a bit harder than in 2D • In 2D we always rotated about the z-axis • Now we have a choice of 3 axes, Rx, Ry, Rz • Rotation is still ccw about the axis • Looking from the positive half of the axis back towards the origin (same as it was in 2D)

  29. Rz(q) • Extending our 2D rotation equations to 3D: x’ = x cos(q) – y sin(q) y’ = x sin(q) + y cos(q) same as in 2D z’ = z the height stays the same Rz(q):

  30. Rx(q) and Ry(q) Rx(q): Ry(q):

  31. Rotation Around a General Axis • Translate P so that the rotation axis passes through the coordinate system origin • Rotate so that the axis of rotation coincides with one of the coordinate axes • In general, requires 2 rotations (about the other 2 axes) • Rotate around the axis • Perform the reverse of the rotations (2) • Perform the reverse of the translation (1)

  32. Rotation Around a General Axis P’ = T(-a,-b,-c) Rx(-y) Ry(-r) Rz(q) Ry(r) Rx(y) T(a, b, c) P • Requires 7 matrix multiplications • Each one requiring 64 multiplies and 48 adds • For a total of 448 multiplies and 336 adds • Per point! • Recall objects sometimes have >100k points

  33. Pre-multiplication of Transformations • Recall that matrix multiplication is not commutative: AB != BA • However, it is associative: (AB)C = A(BC) • This implies that you don’t have to perform the matrix multiplies right-to-left • Thus, we can pre-multiply the 7 matrices that don’t change between points to obtain:A = T(-a,-b,-c) Rx(-y) Ry(-r) Rz(q) Ry(r) Rx(y) T(a, b, c) • Then we repeat the single multiplication P’= A P for each of the 100k points

  34. Pre-multiplication of Transformations • The old method produced (for 100k points) 44.8M multiplies and 33.6M adds • The pre-multiplication method produces approx 6.4M multiplies and 4.8M adds • Plus, not all the multiplies/adds need to be done since we are assuming 0 0 0 1 in the last row • Plus, these multiplies/adds are often done on optimized graphics hardware

  35. Vectors • Another important mathematical concept used in graphics is the Vector • If P1 = (x1, y1, x1) is the starting point and P2 = (x2, y2, z2) is the ending point, then the vector V = (x2 – x1, y2 – y1, z2 – z1) • This just defines length and direction, butnot position P2 P1

  36. Vector Projections y • Projection of v onto the x-axis (5, 2) v x v' (5, 0)

  37. Vector Projections y • Projection of v onto the xz plane (5, 3, 2) v x v' (5, 0, 2) z

  38. 2D Magnitude and Direction • The magnitude (length) of a vector: |V| = sqrt( Vx2 + Vy2 ) • Derived from the Pythagorean theorem • The direction of a vector: a = tan-1 (Vy / Vx) • a.k.a. arctan or atan • atan vs. atan2 • a is angular displacementfrom the x-axis y |v| (5, 2) a x

  39. 3D Magnitude and Direction • 3D magnitude is a simple extension of 2D |V| = sqrt( Vx2 + Vy2 + Vz2 ) • 3D direction is a bit harder than in 2D • Need 2 angles to fully describe direction • Latitude/longitude is a real-world example • Direction Cosines are often used: • a, b, and g are the positive angles that the vector makes with each positive coordinate axes x, y, and z, respectively cos a = Vx / |V| cos b = Vy / |V| cos g = Vz / |V|

  40. Vector Normalization • “Normalizing” a vector means shrinking or stretching it so its magnitude is 1 • a.k.a. creating unit vectors • Note that this does not change the direction • Normalize by dividing by its magnitude V = (1, 2, 3) |V| = sqrt( 12 + 22 + 32) = sqrt(14) + 3.74 Vnorm = V / |V| = (1, 2, 3) / 3.74 = (1 / 3.74, 2 / 3.74, 3 / 3.74) = (.27, .53, .80) |Vnorm| = sqrt( .272 + .532 + .802) = sqrt( .9 ) = .95 • Note that the last calculation doesn’t come out to exactly 1. This is because of the error introduced by using only 2 decimal places in the calculations above.

  41. Vector Addition • Equation: V3 = V1 + V2 = (V1x + V2x , V1y + V2y , V1z + V2z) • Visually: y y V2 V3 V2 V1 V1 x x

  42. Vector Subtraction • Equation: V3 = V1 - V2 = (V1x - V2x , V1y - V2y , V1z - V2z) • Visually: y y V3 V2 V2 V1 V1 x x

  43. Dot Product • The dot product of 2 vectors is a scalar V1. V2 = (V1x V2x) + (V1y V2y )+ (V1z V2z ) • Or, perhaps more importantly for graphics: V1. V2 = |V1| |V2| cos(q) where q is the angle between the 2 vectors and q is in the range 0 q y V2 q V1 x

  44. Dot Product • Why is dot product important for graphics? • It is zero iff the 2 vectors are perpendicular • cos(90) = 0 y V2 V1 x

  45. Dot Product • The Dot Product computation can be simplified when it is known that the vectors are unit vectors V1. V2 = cos(q) because |V1| and |V2| are both 1 • Saves 6 squares, 4 additions, and 2 sqrts

  46. Cross Product • The cross product of 2 vectors is a vector V1x V2 = ( V1y V2z - V1z V2y , V1z V2x - V1x V2z , V1x V2y - V1y V2x ) • Note that if you are big into linear algebra there is also a way to do the cross product calculation using matrices and determinants

  47. Cross Product • Again, just as with the dot product, there is a more graphical definition: V1x V2 = u |V1| |V2| sin(q) where q is the angle between the 2 vectors and q is in the range 0 q and u is the unit vector that is perpendicular to both vectors • Why u? • |V1| |V2| sin(q) produces a scalar and the result needs to be a vector

  48. Cross Product • The direction of u is determined by the right hand rule • The perpendicular definition leaves an ambiguity in terms of the direction of u • Note that you can’t take the cross product of 2 vectors that are parallel to each other • sin(0) = sin(180) = 0  produces the vector (0, 0, 0) V2 u V1

  49. Forming Coordinate Systems • Cross products are great for forming coordinate system frames (3 vectors that are perpendicular to each other) from 2 random vectors • Cross V1 and V2 to form V3 • V3 is now perpendicular to both V1 and V2 • Cross V2 and V3 to form V4 • V4 is now perpendicular to both V2 and V3 • Then V2, V4, and V3 form your new frame

  50. Forming Coordinate Systems • V1 and V2 are in the new xy plane V2 V2 V1 V3 V1 V2 x V3 z V1 V1 V4 y

More Related