Presentation is loading. Please wait.

Presentation is loading. Please wait.

MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics.

Similar presentations


Presentation on theme: "MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics."— Presentation transcript:

1 MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics

2 Where We’re Going §Today’s lectures: l Mathematical Foundations l The graphics pipeline: the big picture l Rigid-body transforms l Homogeneous coordinates

3 Mathematical Foundations §A brief, informal review of some of the mathematical tools employed in graphics l Geometry (2D, 3D) l Trigonometry l Vector and affine spaces Points, vectors, and coordinates l Dot and cross products l Linear transforms and matrices

4 2D Geometry §Know your high-school geometry: l Total angle around a circle is 360° or 2π radians l When two lines cross: Opposite angles are equivalent Angles along line sum to 180° l Similar triangles: All corresponding angles are equivalent Corresponding pairs of sides have the same length ratio and are separated by equivalent angles Any corresponding pairs of sides have same length ratio

5 Trigonometry §Sine: “opposite over hypotenuse” §Cosine: “adjacent over hypotenuse” §Tangent: “opposite over adjacent” §Unit circle definitions: l sin (  ) = x l cos (  ) = y l tan (  ) = x/y l Etc… (x, y)

6 3D Geometry §To model, animate, and render 3D scenes, we must specify: l Location l Displacement from arbitrary locations l Orientation §We’ll look at two types of spaces: l Vector spaces l Affine spaces §The distinction is often sloppy or loose

7 Vector Spaces §Two types of elements: Scalars (real numbers):  … l Vectors (n-tuples): u, v, w, … §Supports two operations: l Addition operation u + v, with: Identity 0 v + 0 = v Inverse - v + (- v ) = 0 l Scalar multiplication: Distributive rule:  ( u + v ) =  ( u ) +  ( v ) (  +  ) u =  u +  u

8 Vector Spaces §A linear combination of vectors results in a new vector: v =  1 v 1 +  2 v 2 + … +  n v n §If the only set of scalars such that  1 v 1 +  2 v 2 + … +  n v n = 0 is  1 =  2 = … =  3 = 0 then we say the vectors are linearly independent §The dimension of a space is the greatest number of linearly independent vectors possible in a vector set §For a vector space of dimension n, any set of n linearly independent vectors form a basis

9 Vector Spaces: A Familiar Example §Our common notion of vectors in a 2D plane is (you guessed it) a vector space: l Vectors are “arrows” rooted at the origin l Scalar multiplication “streches” the arrow, changing its length (magnitude) but not its direction l Addition uses the “trapezoid rule”: u+v y x u v

10 Vector Spaces: Basis Vectors §Given a basis for a vector space: l Each vector in the space is a unique linear combination of the basis vectors l The coordinates of a vector are the scalars from this linear combination l Best-known example: Cartesian coordinates Draw example on the board l Note that a given vector v will have different coordinates for different bases

11 Vectors And Point §We commonly use vectors to represent: l Points in space (i.e., location) l Displacements from point to point l Direction (i.e., orientation) §But we want points and directions to behave differently l Ex: To translate something means to move it without changing its orientation l Translation of a point = different point l Translation of a direction = same direction

12 Affine Spaces §To be more rigorous, we need an explicit notion of position §Affine spaces add a third element to vector spaces: points (P, Q, R, …) §Points support these operations l Point-point subtraction: Q - P = v Result is a vector pointing from P to Q l Vector-point addition: P + v = Q Result is a new point l Note that the addition of two points is not defined P Q v

13 Affine Spaces §Points, like vectors, can be expressed in coordinates l The definition uses an affine combination l Net effect is same: expressing a point in terms of a basis §Thus the common practice of representing points as vectors with coordinates

14 Affine Lines: An Aside §Parametric representation of a line with a direction vector d and a point P 1 on the line: P(  ) = P origin +  d  Restricting 0   produces a ray  Setting d to P - Q and restricting 0    1 produces a line segment between P and Q

15 Dot Product §The dot product or, more generally, inner product of two vectors is a scalar: v 1 v 2 = x 1 x 2 + y 1 y 2 + z 1 z 2 (in 3D) §Useful for many purposes l Computing the length of a vector: length(v) = sqrt(v v) l Normalizing a vector, making it unit-length l Computing the angle between two vectors: u v = |u| |v| cos(θ) l Checking two vectors for orthogonality l Projecting one vector onto another θ u v

16 Cross Product §The cross product or vector product of two vectors is a vector: §The cross product of two vectors is orthogonal to both §Right-hand rule dictates direction of cross product

17 Linear Transformations §A linear transformation: l Maps one vector to another l Preserves linear combinations §Thus behavior of linear transformation is completely determined by what it does to a basis §Turns out any linear transform can be represented by a matrix

18 Matrices §By convention, matrix element M rc is located at row r and column c: §By (OpenGL) convention, vectors are columns:

19 Matrices §Matrix-vector multiplication applies a linear transformation to a vector: §Recall how to do matrix multiplication

20 Matrix Transformations §A sequence or composition of linear transformations corresponds to the product of the corresponding matrices l Note: the matrices to the right affect vector first l Note: order of matrices matters! §The identity matrix I has no effect in multiplication §Some (not all) matrices have an inverse:

21 3-D Graphics: A Whirlwind Tour Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

22 The Display You Know Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

23 The Framebuffer You Know Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

24 The Rendering Pipeline Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay §Why do we call it a pipeline?

25 2-D Rendering You Know Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

26 The Rendering Pipeline: 3-D Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

27 The Rendering Pipeline: 3-D Modeling Transforms Scene graph Object geometry Lighting Calculations Viewing Transform Clipping Projection Transform Result: All vertices of scene in shared 3-D “world” coordinate system All vertices of scene in shared 3-D “world” coordinate system Vertices shaded according to lighting model Vertices shaded according to lighting model Scene vertices in 3-D “view” or “camera” coordinate system Scene vertices in 3-D “view” or “camera” coordinate system Exactly those vertices & portions of polygons in view frustum Exactly those vertices & portions of polygons in view frustum 2-D screen coordinates of clipped vertices 2-D screen coordinates of clipped vertices

28 The Rendering Pipeline: 3-D Scene graph Object geometry Lighting Calculations Clipping Result: All vertices of scene in shared 3-D “world” coordinate system All vertices of scene in shared 3-D “world” coordinate system Vertices shaded according to lighting model Vertices shaded according to lighting model Scene vertices in 3-D “view” or “camera” coordinate system Scene vertices in 3-D “view” or “camera” coordinate system Exactly those vertices & portions of polygons in view frustum Exactly those vertices & portions of polygons in view frustum 2-D screen coordinates of clipped vertices 2-D screen coordinates of clipped vertices Modeling Transforms Viewing Transform Projection Transform

29 Rendering: Transformations §So far, discussion has been in screen space §But model is stored in model space (a.k.a. object space or world space) §Three sets of geometric transformations: l Modeling transforms l Viewing transforms l Projection transforms

30 Rendering: Transformations §Modeling transforms l Size, place, scale, and rotate objects parts of the model w.r.t. each other l Object coordinates  world coordinates Z X Y X Z Y

31 Rendering: Transformations §Viewing transform l Rotate & translate the world to lie directly in front of the camera Typically place camera at origin Typically looking down -Z axis l World coordinates  view coordinates

32 Rendering: Transformations §Projection transform l Apply perspective foreshortening Distant = small: the pinhole camera model l View coordinates  screen coordinates

33 Rendering: Transformations §All these transformations involve shifting coordinate systems (i.e., basis sets) §That’s what matrices do… §Represent coordinates as vectors, transforms as matrices §Multiply matrices = concatenate transforms!

34 Rendering: Transformations §Homogeneous coordinates: represent coordinates in 3 dimensions with a 4-vector l Denoted [x, y, z, w] Note that typically w = 1 in model coordinates l To get 3-D coordinates, divide by w: [x’, y’, z’] = [x/w, y/w, z/w] §Transformations are 4x4 matrices §Why? To handle translation, rotation and projection uniformly

35 Translations §For convenience we usually describe objects in relation to their own coordinate system l Solar system example §We can translate or move points to a new position by adding offsets to their coordinates: l Note that this translates all points uniformly

36 2-D Rotation  (x, y) (x’, y’) x’ = x cos(  ) - y sin(  ) y’ = x sin(  ) + y cos(  ) (Draw it)

37 2-D Rotations §Rotations in 2-D are easy: §3-D is more complicated l Need to specify an axis of rotation l Common pedagogy: express rotation about this axis as the composition of canonical rotations Canonical rotations: rotation about X-axis, Y-axis, Z-axis

38 3-D Rotations §Basic idea: l Using rotations about X, Y, Z axes, rotate model until desired axis of rotation coincides with Z-axis l Perform rotation in the X-Y plane (i.e., about Z-axis) l Reverse the initial rotations to get back into the initial frame of reference §Objections: l Difficult & error prone l Ambiguous: several combinations about the canonical axis give the same result

39 Scaling §Scaling a coordinate means multiplying each of its components by a scalar §Uniform scaling means this scalar is the same for all components:  2 2

40 Scaling §Non-uniform scaling: different scalars per component: §How can we represent this in matrix form? X  2, Y  0.5

41 Scaling §Scaling operation: §Or, in matrix form: scaling matrix

42 2-D Rotation §This is easy to capture in matrix form: §3-D is more complicated l Need to specify an axis of rotation l Simple cases: rotation about X, Y, Z axes

43 3-D Rotation §What does the 3-D rotation matrix look like for a rotation about the Z-axis? l Build it coordinate-by-coordinate

44 3-D Rotation §What does the 3-D rotation matrix look like for a rotation about the Y-axis? l Build it coordinate-by-coordinate

45 3-D Rotation §What does the 3-D rotation matrix look like for a rotation about the X-axis? l Build it coordinate-by-coordinate

46 Translation Matrices? §We can composite scale matrices just as we did rotation matrices §But how to represent translation as a matrix? §Answer: with homogeneous coordinates

47 Homogeneous Coordinates §Homogeneous coordinates: represent coordinates in 3 dimensions with a 4-vector (Note that typically w = 1 in object coordinates)

48 Homogeneous Coordinates §Homogeneous coordinates seem unintuitive, but they make graphics operations much easier §Our transformation matrices are now 4x4:

49 Homogeneous Coordinates §Homogeneous coordinates seem unintuitive, but they make graphics operations much easier §Our transformation matrices are now 4x4:

50 Homogeneous Coordinates §Homogeneous coordinates seem unintuitive, but they make graphics operations much easier §Our transformation matrices are now 4x4:

51 Homogeneous Coordinates §Homogeneous coordinates seem unintuitive, but they make graphics operations much easier §Our transformation matrices are now 4x4:

52 Homogeneous Coordinates §How can we represent translation as a 4x4 matrix? §A: Using the rightmost column:

53 Rendering: Transformations §OpenGL caveats: l All modeling transforms and the viewing transform are concatenated into the modelview matrix l A stack of modelview matrices is kept l The projection transform is stored separately in the projection matrix §See Chapter 3 of the OpenGL book

54 The Rendering Pipeline: 3-D Modeling Transforms Scene graph Object geometry Lighting Calculations Viewing Transform Clipping Projection Transform Result: All vertices of scene in shared 3-D “world” coordinate system All vertices of scene in shared 3-D “world” coordinate system Vertices shaded according to lighting model Vertices shaded according to lighting model Scene vertices in 3-D “view” or “camera” coordinate system Scene vertices in 3-D “view” or “camera” coordinate system Exactly those vertices & portions of polygons in view frustum Exactly those vertices & portions of polygons in view frustum 2-D screen coordinates of clipped vertices 2-D screen coordinates of clipped vertices

55 Rendering: Lighting §Illuminating a scene: coloring pixels according to some approximation of lighting l Global illumination: solves for lighting of the whole scene at once l Local illumination: local approximation, typically lighting each polygon separately §Interactive graphics (e.g., hardware) does only local illumination at run time

56 The Rendering Pipeline: 3-D Modeling Transforms Scene graph Object geometry Lighting Calculations Viewing Transform Clipping Projection Transform Result: All vertices of scene in shared 3-D “world” coordinate system All vertices of scene in shared 3-D “world” coordinate system Vertices shaded according to lighting model Vertices shaded according to lighting model Scene vertices in 3-D “view” or “camera” coordinate system Scene vertices in 3-D “view” or “camera” coordinate system Exactly those vertices & portions of polygons in view frustum Exactly those vertices & portions of polygons in view frustum 2-D screen coordinates of clipped vertices 2-D screen coordinates of clipped vertices

57 Rendering: Clipping §Clipping a 3-D primitive returns its intersection with the view frustum: §See Foley & van Dam section 19.1

58 Rendering: Clipping §Clipping is tricky! In: 3 vertices Out: 6 vertices Clip In: 1 polygon Out: 2 polygons

59 The Rendering Pipeline: 3-D Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay

60 Modeling: The Basics §Common interactive 3-D primitives: points, lines, polygons (i.e., triangles) §Organized into objects l Collection of primitives, other objects l Associated matrix for transformations §Instancing: using same geometry for multiple objects l 4 wheels on a car, 2 arms on a robot

61 Modeling: The Scene Graph §The scene graph captures transformations and object-object relationships in a DAG §Objects in black; blue arrows indicate instancing and each have a matrix Robot BodyHead ArmTrunkLegEyeMouth

62 Modeling: The Scene Graph §Traverse the scene graph in depth-first order, concatenating transformations §Maintain a matrix stack of transformations ArmTrunk Leg EyeMouth HeadBody Robot Foot Matrix Stack Visited Unvisited Active

63 Modeling: The Camera §Finally: need a model of the virtual camera l Can be very sophisticated Field of view, depth of field, distortion, chromatic aberration… l Interactive graphics (OpenGL): Pinhole camera model –Field of view –Aspect ratio –Near & far clipping planes Camera pose: position & orientation

64 Modeling: The Camera §These parameters are encapsulated in a projection matrix l Homogeneous coordinates  4x4 matrix! l See OpenGL Appendix F for the matrix §The projection matrix premultiplies the viewing matrix, which premultiplies the modeling matrices l Actually, OpenGL lumps viewing and modeling transforms into modelview matrix

65 Rigid-Body Transforms §Goal: object coordinates  world coordinates §Idea: use only transformations that preserve the shape of the object l Rigid-body or Euclidean transforms l Includes rotation, translation, and scale §To reiterate: we will represent points as column vectors:

66 Vectors and Matrices §Vector algebra operations can be expressed in this matrix form l Dot product: l Cross product: Note: use right-hand rule!

67 3-D Rotation §General rotations in 3-D require rotating about an arbitrary axis of rotation §Deriving the rotation matrix for such a rotation directly is difficult l But possible, see McMillan’s lectures §Standard approach: express general rotation as composition of canonical rotations l Rotations about X, Y, Z

68 Composing Canonical Rotations §Goal: rotate about arbitrary vector A by  l Idea: we know how to rotate about X,Y,Z l So, rotate about Y by  until A lies in the YZ plane, Then rotate about X by  until A coincides with +Z, Then rotate about Z by  Then reverse the rotation about X (by -  ) Then reverse the rotation about Y (by -  )

69 Composing Canonical Rotations §First: rotating about Y by  until A lies in YZ l Draw it… §How exactly do we calculate  ? l Project A onto XZ plane l Find angle  to X:  = -(90° -  ) =  - 90 ° §Second: rotating about X by  until A lies on Z §How do we calculate  ?

70 3-D Rotation Matrices §So an arbitrary rotation about A composites several canonical rotations together §We can express each rotation as a matrix §Compositing transforms == multiplying matrices §Thus we can express the final rotation as the product of canonical rotation matrices §Thus we can express the final rotation with a single matrix!

71 Compositing Matrices §So we have the following matrices: p: The point to be rotated about A by  R y  : Rotate about Y by  R x  : Rotate about X by  R z  : Rotate about Z by  R x  -1 : Undo rotation about X by  R y  -1 : Undo rotation about Y by  §In what order should we multiply them?

72 Compositing Matrices §Short answer: the transformations, in order, are written from right to left l In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc. §So in our case: p’ = R y  -1 R x  -1 R z  R x  R y  p

73 Rotation Matrices §Notice these two matrices: R x  : Rotate about X by  R x  -1 : Undo rotation about X by  §How can we calculate R x  -1 ?

74 Rotation Matrices §Notice these two matrices: R x  : Rotate about X by  R x  -1 : Undo rotation about X by  §How can we calculate R x  -1 ? l Obvious answer: calculate R x (-  ) l Clever answer: exploit fact that rotation matrices are orthonormal

75 Rotation Matrices §Notice these two matrices: R x  : Rotate about X by  R x  -1 : Undo rotation about X by  §How can we calculate R x  -1 ? l Obvious answer: calculate R x (-  ) l Clever answer: exploit fact that rotation matrices are orthonormal §What is an orthonormal matrix? §What property are we talking about?

76 Rotation Matrices §Orthonormal matrix: l orthogonal (columns/rows linearly independent) l Columns/rows sum to 1 §The inverse of an orthogonal matrix is just its transpose:


Download ppt "MAE152 Computer Graphicsfor Scientists and Engineers Revision of Matrices Relevance to Graphics."

Similar presentations


Ads by Google