Ting Yip Math 308A 12/3/2001 Ting Yip Math 308A Abstract
Total Page:16
File Type:pdf, Size:1020Kb
Matrices in Computer Graphics Ting Yip Math 308A 12/3/2001 Ting Yip Math 308A Abstract In this paper, we discuss and explore the basic matrix operation such as translations, rotations, scaling and we will end the discussion with parallel and perspective view. These concepts commonly appear in video game graphics. Introduction The use of matrices in computer graphics is widespread. Many industries like architecture, cartoon, automotive that were formerly done by hand drawing now are done routinely with the aid of computer graphics. Video gaming industry, maybe the earliest industry to rely heavily on computer graphics, is now representing rendered polygon in 3- Dimensions. In video gaming industry, matrices are major mathematic tools to construct and manipulate a realistic animation of a polygonal figure. Examples of matrix operations include translations, rotations, and scaling. Other matrix transformation concepts like field of view, rendering, color transformation and projection. Understanding of matrices is a basic necessity to program 3D video games. Graphics (Screenshots taken from Operation Flashpoint) Polygon figures like these use many flat or conic surfaces to represent a realistic human soldier. The last coordinate is a scalar term. Homogeneous Coordinate Transformation 3 æ x y z ö Points (x, y, z) in R can be identified as a homogeneous vector (x,y,z,h)®ç , , ,1÷with èh h h ø h ¹ 0 on the plane in R4. If we convert a 3D point to a 4D vector, we can represent a transformation to this point with a 4 x 4 matrix. 2 Ting Yip Math 308A EXAMPLE 3 4 Point (2, 5, 6) in R a Vector (2, 5, 6, 1) or (4, 10, 12, 2) in R NOTE It is possible to apply transformation to 3D points without converting them to 4D vectors. The tradeoff is that transformation can be done with a single matrix multiplication after the convertion of points to vectors. (More on this after Translation.) Transformation of Points In general, transformation of points can be represented by this equation: Transformed Point = Transformation Matrix ´ Original Point In a more explicit case, a plane spanned by two vectors can be represented by this equation: Transforme d Plane = Transforma tion Matrix ´ Original Plane ìéaù édùü ï ï Transforma tion Matrix span êbú, êeú = ´ íê ú ê úý ï ï îëêcûú ëêf ûúþ x and y are scalars éa dù ê ú éxù = Transforma tion Matrix ´ b e ê ú ê ú ëyû ëêc f ûú éaù éd ù ê ú ê ú éd ù xêbú + yêe ú ê ú ê e ú ëêcûú ëê f ûú ëê f ûú éaù ê ú êbú ëêcûú Representation of a plane using matrices 3 Ting Yip Math 308A Translation A translation basically means adding a vector to a point, making a point transforms to a new point. This operation can be simplified as a translation in homogeneous coordinate (x, y, z, 1) to (x + tx, y + ty, z + tz, 1). This transformation can be computed using a single matrix multiplication. Translation Matrix for Homogeneous Coordinates in R4 is given by this matrix: é1 0 0 t x ù ê0 1 0 t ú ê y ú T(t x ,t y ,t z ) = ê0 0 1 t z ú ê ú ë0 0 0 1û Given any point (x, y, z) in R3, the following will give the translated point. é1 0 0 t x ùéxù éx + t x ù ê0 1 0 t úêyú êy +t ú ê y úê ú = ê y ú ê0 0 1 tz úêzú êz + t z ú ê úê ú ê ú ë0 0 0 1 ûë1û ë 1 û For a sphere to move to a new position, we can think of this as all the points on the sphere move to the translated sphere by adding the blue vector to each point. 4 Ting Yip Math 308A Graphics (Screenshots taken from Operation Flashpoint) In video game, objects like airplane that doesn’t change its shape dynamically (rigid body) uses Translation to move across the sky. All the points that make up the plane have to be translated by the same vector or the image of the plane will appear to be stretched. NOTE é x ù é i ù é x + i ù ê ú ê ú ê ú ê y ú + ê j ú = ê y + j ú ê z ú ê k ú ê z + k ú ë û ë û ë û If we have more than one point, we would have to apply this addition to every point. éx1 x2 ù éx1 ù éi ù é x1 + i ù éx2 ù éi ù é x2 + i ù é x1 +i x2 + i ù êy y ú a êy ú + ê jú = êy + jú &êy ú + ê jú = êy + jú a êy + j y + jú ê 1 2 ú ê 1ú ê ú ê 1 ú ê 2ú ê ú ê 2 ú ê 1 2 ú êz z ú êz ú êkú êz + kú êz ú êkú êz + kú êz + k z + kú ë 1 2 û ë 1 û ë û ë 1 û ë 2 û ë û ë 2 û ë 1 2 û With homogeneous coordinate, we can use a single matrix multiplication. é1 0 0 i ùéx x ù é x + i x + i ù 1 2 1 2 é x + i x + i ù ê0 1 0 júêy y ú êy + j y + jú 1 2 ê úê 1 2 ú = ê 1 2 ú ® êy + j y + jú ê 1 2 ú ê0 0 1 kúêz1 z2 ú êz1 + k z2 + kú ê úê ú ê ú ëêz1 + k z2 + kûú ë0 0 0 1ûë 1 1 û ë 1 1 û As we can see, linear system is easier to solve with homogenenous coordinate transformation. 5 Ting Yip Math 308A Scaling Scaling of any dimension requires one of the diagonal values of the transformation matrix to equal to a value other than one. This operation can be viewed as a scaling in homogeneous coordinate (x, y, z, 1) to (sxx, syy, szz, 1). Values for sx, sy, sz greater than one will enlarge the objects, values between zero and one will shrink the objects, and negative values will rotate the object and change the size of the objects. Scaling Matrix for Homogeneous Coordinates in R4 is given by this matrix: és x 0 0 0ù ê 0 s 0 0ú ê y ú S(s x ,s y ,sz ) = ê 0 0 sz 0ú ê ú ë 0 0 0 1û Given any point (x, y, z) in R3, the following will give the scaled point. és x 0 0 0ùéxù és x xù ê 0 s 0 0úêyú ês yú ê y úê ú = ê y ú ê 0 0 sz 0úêzú êsz zú ê úê ú ê ú ë 0 0 0 1ûë1û ë 1 û If we want to scale the hexahedron proportionally, we apply the same scaling matrix to each point that makes up the hexahedron. 6 Ting Yip Math 308A Rotations Rotations are defined with respect to an axis. In 3 dimensions, the axis of rotation needs to be specified. A rotation about the x axis is represented by this matrix: é1 0 0 0ù éxù é1 0 0 0ùéxù é x ù ê ú ê ú ê úê ú ê ú 0 cosq - sin q 0 y 0 cosq - sin q 0 y y cosq - z sin q R (q ) = ê ú Þ R (q)ê ú = ê úê ú = ê ú x ê0 sin q cosq 0ú x êzú ê0 sin q cosq 0úêzú êy sin q + z cosq ú ê ú ê ú ê úê ú ê ú ë0 0 0 1û ë1û ë0 0 0 1ûë1û ë 1 û A rotation about the y axis is represented by this matrix: é cosq 0 sin q 0ù éxù é cosq 0 sin q 0ùéxù é xcosq + z sin q ù ê 0 1 0 0ú êyú ê 0 1 0 0úêyú ê y ú R (q ) = ê ú Þ R (q )ê ú = ê úê ú = ê ú y ê- sin q 0 cosq 0ú y êzú ê- sin q 0 cosq 0úêzú ê- xsin q + z cosq ú ê ú ê ú ê úê ú ê ú ë 0 0 0 1û ë1û ë 0 0 0 1ûë1û ë 1 û A rotation about the z axis is represented by this matrix: écosq - sin q 0 0ù éxù écosq - sin q 0 0ùéxù éx cosq - y sin q ù êsin q cosq 0 0ú êyú êsin q cosq 0 0úêyú êx sin q + y cosq ú R (q ) = ê ú Þ R (q )ê ú = ê úê ú = ê ú z ê 0 0 1 0ú z êzú ê 0 0 1 0úêzú ê z ú ê ú ê ú ê úê ú ê ú ë 0 0 0 1û ë1û ë 0 0 0 1ûë1û ë 1 û 3D rotation can be viewed as replacing x1 and x2 with two axes. 7 Ting Yip Math 308A This wire polygon cube is represented by a matrix that contains EXAMPLE its vertex point in every column. If we want to rotate this cube with respect to the x axis by æp ö é5 5 7 7 5 7 5 7ù ç ÷: ê1 1 1 1 3 3 3 3ú è 3 ø ê ú ê9 7 7 9 7 7 9 9ú ê ú é1 0 0 0ù ë1 1 1 1 1 1 1 1û ê æ p ö æp ö ú é5 5 7 7 5 7 5 7ù ê0 cosç ÷ - sin ç ÷ 0ú ê ú è 3 ø è 3 ø 1 1 1 1 3 3 3 3 ê ú ê ú = ê æ p ö æ p ö ú ê9 7 7 9 7 7 9 9ú 0 sin ç ÷ cosç ÷ 0 ê ú ê è 3 ø è 3 ø ú 1 1 1 1 1 1 1 1 ê ú ë û ë0 0 0 1û é 5 5 7 7 5 7 5 7 ù ê ú ê ú ê1 9 1 7 1 7 1 9 3 7 3 7 3 9 3 9 ú ê - 3 - 3 - 3 - 3 - 3 - 3 - 3 - 3ú ê2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ú ê ú ê ú ê9 1 7 1 7 1 9 1 7 3 7 3 9 3 9 3 ú ê + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3ú ê ú ê2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ú ê ú ë 1 1 1 1 1 1 1 1 û Original Cube Rotated Cube 8 Ting Yip Math 308A Projection Transformation Even though we programmed objects in 3-Dimensions, we have to actually view the objects as 2-Dimensions on our computer screens.