CS 445 / 645 Introduction to Computer Graphics Lecture 3 Mathematical Primitives Introduction to Transformations Lecture 3 Mathematical Primitives Introduction to Transformations
Show Your Gang Colors Use hand to identify handedness Point thumb, index finger, and middle finger in orthogonal directionsPoint thumb, index finger, and middle finger in orthogonal directions –Thumb = x-axis –Index = y-axis –Middle = z-axis Left and right hands correspond to left and right hand coordinate systemsLeft and right hands correspond to left and right hand coordinate systems Use hand to identify handedness Point thumb, index finger, and middle finger in orthogonal directionsPoint thumb, index finger, and middle finger in orthogonal directions –Thumb = x-axis –Index = y-axis –Middle = z-axis Left and right hands correspond to left and right hand coordinate systemsLeft and right hands correspond to left and right hand coordinate systems
Triangle Arithmetic Consider a triangle, (a, b, c) a,b,c = (x,y,z) tuplesa,b,c = (x,y,z) tuples Surface area = s a = ½ * ||(b –a) X (c-a)|| Unit normal = (1/2s a ) * (b-a) X (c-a) Consider a triangle, (a, b, c) a,b,c = (x,y,z) tuplesa,b,c = (x,y,z) tuples Surface area = s a = ½ * ||(b –a) X (c-a)|| Unit normal = (1/2s a ) * (b-a) X (c-a) a c b
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 1 v 1 + 2 v 2 + … + n v n = 0 is 1 = 2 = … = 3 = 0 is 1 = 2 = … = 3 = 0 then we say the vectors are linearly independent 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 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 1 v 1 + 2 v 2 + … + n v n = 0 is 1 = 2 = … = 3 = 0 is 1 = 2 = … = 3 = 0 then we say the vectors are linearly independent 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
Vector Spaces: Basis Vectors Given a basis for a vector space: Each vector in the space is a unique linear combination of the basis vectorsEach vector in the space is a unique linear combination of the basis vectors The coordinates of a vector are the scalars from this linear combinationThe coordinates of a vector are the scalars from this linear combination If basis vectors are orthogonal and unit length:If basis vectors are orthogonal and unit length: –Vectors comprise orthonormal basis Best-known example: Cartesian coordinatesBest-known example: Cartesian coordinates Note that a given vector v will have different coordinates for different basesNote that a given vector v will have different coordinates for different bases Given a basis for a vector space: Each vector in the space is a unique linear combination of the basis vectorsEach vector in the space is a unique linear combination of the basis vectors The coordinates of a vector are the scalars from this linear combinationThe coordinates of a vector are the scalars from this linear combination If basis vectors are orthogonal and unit length:If basis vectors are orthogonal and unit length: –Vectors comprise orthonormal basis Best-known example: Cartesian coordinatesBest-known example: Cartesian coordinates Note that a given vector v will have different coordinates for different basesNote that a given vector v will have different coordinates for different bases
Matrices By convention, matrix element M rc is located at row r and column c: By (OpenGL) convention, vectors are columns
Matrices Matrix-vector multiplication applies a linear transformation to a vector: Why do we call this a linear transformation?Why do we call this a linear transformation? Recall how to do matrix multiplication? Matrix-vector multiplication applies a linear transformation to a vector: Why do we call this a linear transformation?Why do we call this a linear transformation? Recall how to do matrix multiplication?
Matrix Transformations A sequence or composition of linear transformations corresponds to the product of the corresponding matrices Note: the matrices to the right affect vector firstNote: the matrices to the right affect vector first Note: order of matrices matters!Note: order of matrices matters! The identity matrix I has no effect in multiplication Some (not all) matrices have an inverse: A sequence or composition of linear transformations corresponds to the product of the corresponding matrices Note: the matrices to the right affect vector firstNote: the matrices to the right affect vector first Note: order of matrices matters!Note: order of matrices matters! The identity matrix I has no effect in multiplication Some (not all) matrices have an inverse:
Matrix Transformations A linear transformation: Maps one vector to anotherMaps one vector to another Preserves linear combinationsPreserves 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 A linear transformation: Maps one vector to anotherMaps one vector to another Preserves linear combinationsPreserves 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
Matrix Transformations We hypothesize that all necessary linear transformations can be accomplished with matrix multiplicationWe hypothesize that all necessary linear transformations can be accomplished with matrix multiplication Let’s look at a fewLet’s look at a few –Scaling –Rotation –Translation? We hypothesize that all necessary linear transformations can be accomplished with matrix multiplicationWe hypothesize that all necessary linear transformations can be accomplished with matrix multiplication Let’s look at a fewLet’s look at a few –Scaling –Rotation –Translation?
Scaling Scaling a coordinate means multiplying each of its components by a scalar Uniform scaling means this scalar is the same for all components: 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
Non-uniform scaling: different scalars per component: How can we represent this in matrix form? Non-uniform scaling: different scalars per component: How can we represent this in matrix form? Scaling X 2, Y 0.5
Scaling Scaling operation: Or, in matrix form: Scaling operation: Or, in matrix form: scaling matrix
2-D Rotation (x, y) (x’, y’) x’ = x cos( ) - y sin( ) y’ = x sin( ) + y cos( )
2-D Rotation x = r cos ( ) y = r sin ( ) x’ = r cos ( + ) y’ = r sin ( + ) Trig Identity… x’ = r cos( ) cos( ) – r sin( ) sin( ) y’ = r sin( ) sin( ) + r cos( ) cos( ) Substitute… x’ = x cos( ) - y sin( ) y’ = x sin( ) + y cos( ) (x, y) (x’, y’)
2-D Rotation This is easy to capture in matrix form: Even though sin( ) and cos( ) are nonlinear functions of , x’ is a linear combination of x and yx’ is a linear combination of x and y y’ is a linear combination of x and yy’ is a linear combination of x and y This is easy to capture in matrix form: Even though sin( ) and cos( ) are nonlinear functions of , x’ is a linear combination of x and yx’ is a linear combination of x and y y’ is a linear combination of x and yy’ is a linear combination of x and y
Translation What can you do with a 2x2 matrix transformation? Nothing corresponds to translation… What can you do with a 2x2 matrix transformation? Nothing corresponds to translation…
Homogeneous Coordinates Homogeneous coordinates represent coordinates in 2 dimensions with a 3-vectorrepresent coordinates in 2 dimensions with a 3-vector Homogeneous coordinates represent coordinates in 2 dimensions with a 3-vectorrepresent coordinates in 2 dimensions with a 3-vector Homogeneous coordinates seem unintuitive, but they make graphics operations much easier
Homogeneous Coordinates Our transformation matrices are now 3x3:
Homogeneous Coordinates Q: How can we represent translation as a 3x3 matrix? A: Using the rightmost column: Q: How can we represent translation as a 3x3 matrix? A: Using the rightmost column:
Translation Example of translation T x = 2 T y = 1
Total Picture Think about what happens with manipulating transformation matrix a, e = negative?a, e = negative? Changing d and e?Changing d and e? Changing i != 1?Changing i != 1? Think about what happens with manipulating transformation matrix a, e = negative?a, e = negative? Changing d and e?Changing d and e? Changing i != 1?Changing i != 1?
Composing Transformations What if we want to scale and rotate and translate? We can execute transformations one after the otherWe can execute transformations one after the other Ex: Rotate line segment by 45 degrees about endpoint aEx: Rotate line segment by 45 degrees about endpoint a What if we want to scale and rotate and translate? We can execute transformations one after the otherWe can execute transformations one after the other Ex: Rotate line segment by 45 degrees about endpoint aEx: Rotate line segment by 45 degrees about endpoint a aa
Multiplication Order – Wrong Way Our line is defined by two endpoints Applying a rotation of 45 degrees, R(45), affects both pointsApplying a rotation of 45 degrees, R(45), affects both points We could try to translate both endpoints to return endpoint a to its original position, but by how much?We could try to translate both endpoints to return endpoint a to its original position, but by how much? Our line is defined by two endpoints Applying a rotation of 45 degrees, R(45), affects both pointsApplying a rotation of 45 degrees, R(45), affects both points We could try to translate both endpoints to return endpoint a to its original position, but by how much?We could try to translate both endpoints to return endpoint a to its original position, but by how much? T(-3), R(45), T(3) WrongCorrect T(-3) R(45) T(3) R(45) a a
Multuplication Order - Correct Isolate endpoint a from rotation effects First translate line so a is at origin: T (-3)First translate line so a is at origin: T (-3) Then rotate line 45 degrees: R(45)Then rotate line 45 degrees: R(45) Then translate back so a is where it was: T(3)Then translate back so a is where it was: T(3) Isolate endpoint a from rotation effects First translate line so a is at origin: T (-3)First translate line so a is at origin: T (-3) Then rotate line 45 degrees: R(45)Then rotate line 45 degrees: R(45) Then translate back so a is where it was: T(3)Then translate back so a is where it was: T(3) a a a a
Will this sequence of operations work? Result of first multiply with vector Will this sequence of operations work? Result of first multiply with vector Compositing Matrices
Order of multiplication matters Short answer: the transformations, in order, are written from right to left In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc.In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc. Order of multiplication matters Short answer: the transformations, in order, are written from right to left In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc.In other words, the first matrix to affect the vector goes next to the vector, the second next to the first, etc.
Compositing Matrices After correctly ordering the matrices Multiply matrices together What results is one matrix – store it! Multiply this matrix by the vector of each vertex All vertices easily trasformed with one matrix multiply After correctly ordering the matrices Multiply matrices together What results is one matrix – store it! Multiply this matrix by the vector of each vertex All vertices easily trasformed with one matrix multiply
Frame Buffers A frame buffer may be thought of as computer memory organized as a two-dimensional array with each (x,y) addressable location corresponding to one pixel. Bit Planes or Bit Depth is the number of bits corresponding to each pixel. A typical frame buffer resolution might be 640 x 480 x x 1024 x x 1024 x 24 A frame buffer may be thought of as computer memory organized as a two-dimensional array with each (x,y) addressable location corresponding to one pixel. Bit Planes or Bit Depth is the number of bits corresponding to each pixel. A typical frame buffer resolution might be 640 x 480 x x 1024 x x 1024 x 24
1-Bit Memory, Monochrome Display (Bitmap Display)
3-Bit Color Display
True Color Display Red Blue bitplanes, 8 bits per color gun = 16,777,216
Color Map Look-Up Tables Extends the number of colors that can be displayed by a given number of bit-planes RG B RED GREEN BLUE Pixel displayed at x', y' Pixel in bit map at x', y' 0 x 0 y x max y Frame bufferLook-up tableDisplay
Pseudo color 2 8 x 24 Color Map LUT
Rendering 3D Scenes Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay
The Rendering Pipeline Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay
Rendering: Transformations We’ve learned about transformations But they are used in three ways: Modeling transformsModeling transforms Viewing transforms (Move the camera)Viewing transforms (Move the camera) Projection transforms (Change the type of camera)Projection transforms (Change the type of camera) We’ve learned about transformations But they are used in three ways: Modeling transformsModeling transforms Viewing transforms (Move the camera)Viewing transforms (Move the camera) Projection transforms (Change the type of camera)Projection transforms (Change the type of camera)
The Rendering Pipeline: 3-D 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 Scene graph Object geometry Lighting Calculations Clipping Modeling Transforms Viewing Transform Projection Transform
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
Rendering: Transformations Modeling transforms Size, place, scale, and rotate objects and parts of the model w.r.t. each otherSize, place, scale, and rotate objects and parts of the model w.r.t. each other Object coordinates -> world coordinatesObject coordinates -> world coordinates Modeling transforms Size, place, scale, and rotate objects and parts of the model w.r.t. each otherSize, place, scale, and rotate objects and parts of the model w.r.t. each other Object coordinates -> world coordinatesObject coordinates -> world coordinates Z X Y X Z Y
Modeling Transforms Scene graph Object geometry Lighting Calculations Viewing Transform Clipping Projection Transform Result: Scene vertices in 3-D “view” or “camera” coordinate system Scene vertices in 3-D “view” or “camera” coordinate system The Rendering Pipeline: 3-D
Rendering: Transformations Viewing transform Rotate & translate the world to lie directly in front of the cameraRotate & translate the world to lie directly in front of the camera –Typically place camera at origin –Typically looking down -Z axis World coordinates view coordinatesWorld coordinates view coordinates Viewing transform Rotate & translate the world to lie directly in front of the cameraRotate & translate the world to lie directly in front of the camera –Typically place camera at origin –Typically looking down -Z axis World coordinates view coordinatesWorld coordinates view coordinates
Modeling Transforms Scene graph Object geometry Lighting Calculations Viewing Transform Clipping Projection Transform Result: 2-D screen coordinates of clipped vertices 2-D screen coordinates of clipped vertices The Rendering Pipeline: 3-D
Rendering: Transformations Projection transform Apply perspective foreshorteningApply perspective foreshortening –Distant = small: the pinhole camera model View coordinates screen coordinatesView coordinates screen coordinates Projection transform Apply perspective foreshorteningApply perspective foreshortening –Distant = small: the pinhole camera model View coordinates screen coordinatesView coordinates screen coordinates
Perspective Camera Orthographic Camera Perspective Camera Orthographic Camera Rendering: Transformations
Introducing OpenGL mid-level, device-independent, portable graphics subroutine package developed primarily by SGI 2D/3D graphics, lower-level primitives (polygons) does not include low-level I/O management basis for higher-level libraries/toolkits mid-level, device-independent, portable graphics subroutine package developed primarily by SGI 2D/3D graphics, lower-level primitives (polygons) does not include low-level I/O management basis for higher-level libraries/toolkits
Introducing OpenGL Recall the rendering pipeline: Transform geometry (object world, world eye)Transform geometry (object world, world eye) Apply perspective projection (eye screen)Apply perspective projection (eye screen) Clip to the view frustumClip to the view frustum Perform visible-surface processing (Z-buffer)Perform visible-surface processing (Z-buffer) Calculate surface lightingCalculate surface lighting Implementing all this is a lot of work OpenGL provides a standard implementation So why study the basics?So why study the basics? Recall the rendering pipeline: Transform geometry (object world, world eye)Transform geometry (object world, world eye) Apply perspective projection (eye screen)Apply perspective projection (eye screen) Clip to the view frustumClip to the view frustum Perform visible-surface processing (Z-buffer)Perform visible-surface processing (Z-buffer) Calculate surface lightingCalculate surface lighting Implementing all this is a lot of work OpenGL provides a standard implementation So why study the basics?So why study the basics?