Transcript PowerPoint
CS 445 / 645 Introduction to Computer Graphics 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 directions – Thumb = x-axis – Index = y-axis – Middle = z-axis • Left and right hands correspond to left and right hand coordinate systems Triangle Arithmetic Consider a triangle, (a, b, c) b • a,b,c = (x,y,z) tuples a Surface area = sa = ½ * ||(b –a) X (c-a)|| Unit normal = (1/2sa) * (b-a) X (c-a) c Vector Spaces A linear combination of vectors results in a new vector: v = a1v1 + a2v2 + … + anvn If the only set of scalars such that a1v1 + a2v2 + … + anvn = 0 is a1 = a2 = … = a3 = 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 Vector Spaces: Basis Vectors Given a basis for a vector space: • Each vector in the space is a unique linear combination of the basis vectors • The coordinates of a vector are the scalars from this linear combination • If basis vectors are orthogonal and unit length: – Vectors comprise orthonormal basis • Best-known example: Cartesian coordinates • Note that a given vector v will have different coordinates for different bases Matrices By convention, matrix element Mrc is located at row r and column c: M11 M21 M Mm1 v1 By (OpenGL) convention, vectors v v 2 are columns v 3 M12 M22 Mm2 M1n M2n Mmn Matrices Matrix-vector multiplication applies a linear transformation to a vector: M11 M12 M v M 21 M 22 M 31 M 32 M13 vx a M 23 vy b M 33 vz c • 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 M1M 2 M 3vold vnew • Note: the matrices to the right affect vector first • Note: order of matrices matters! The identity matrix I has no effect in multiplication Some (not all) matrices have an inverse: M 1 Mv v Matrix Transformations A linear transformation: • Maps one vector to another • 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 Matrix Transformations • We hypothesize that all necessary linear transformations can be accomplished with matrix multiplication • Let’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: 2 Scaling Non-uniform scaling: different scalars per component: X 2, Y 0.5 How can we represent this in matrix form? Scaling Scaling operation: Or, in matrix form: x' ax y ' by x' a y ' 0 0 x b y scaling matrix 2-D Rotation (x’, y’) (x, y) x’ = x cos() - y sin() y’ = x sin() + y cos() 2-D Rotation (x’, y’) (x, y) f x = r cos (f) y = r sin (f) x’ = r cos (f + ) y’ = r sin (f + ) Trig Identity… x’ = r cos(f) cos() – r sin(f) sin() y’ = r sin(f) sin() + r cos(f) cos() Substitute… x’ = x cos() - y sin() y’ = x sin() + y cos() 2-D Rotation This is easy to capture in matrix form: x' cos sin x y ' sin cos y Even though sin() and cos() are nonlinear functions of , • x’ is a linear combination of x and y • y’ is a linear combination of x and y Translation What can you do with a 2x2 matrix transformation? a b x x' c d y y ' Nothing corresponds to translation… Homogeneous Coordinates Homogeneous coordinates • represent coordinates in 2 dimensions with a 3-vector x x homogeneous coords y y 1 Homogeneous coordinates seem unintuitive, but they make graphics operations much easier Homogeneous Coordinates Our transformation matrices are now 3x3: cos( ) Rotation sin( ) 0 sin( ) cos( ) 0 a 0 0 Scale 0 b 0 0 0 1 0 0 1 Homogeneous Coordinates Q: How can we represent translation as a 3x3 matrix? A: Using the rightmost column: 1 Translation 0 0 0 Tx 1 Ty 0 1 Translation Example of translation Tx = 2 Ty = 1 1 0 Tx x x Tx 0 1 T y y y T y 0 0 1 1 1 Total Picture Think about what happens with manipulating transformation matrix • a, e = negative? • Changing d and e? • Changing i != 1? a b d e g h c f i Composing Transformations What if we want to scale and rotate and translate? • We can execute transformations one after the other • Ex: Rotate line segment by 45 degrees about endpoint a a a Multiplication Order – Wrong Way Our line is defined by two endpoints • Applying 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? T(-3), R(45), T(3) a a Wrong R(45) Correct T(-3) R(45) T(3) Multuplication Order - Correct Isolate endpoint a from rotation effects a • First translate line so a is at origin: T (-3) a • Then rotate line 45 degrees: R(45) a • Then translate back so a is where it was: T(3) a Compositing Matrices Will this sequence of operations work? 1 0 3 cos( 45) sin( 45) 0 1 0 3 ax a' x 0 1 0 sin( 45) cos( 45) 0 0 1 0 a a' y y 0 0 1 0 0 1 0 0 1 1 1 Result of first multiply with vector 1 0 3 a x a x 3 0 1 0 a a y y 0 0 1 1 1 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. 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 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 8 1280 x 1024 x 8 1280 x 1024 x 24 1-Bit Memory, Monochrome Display (Bitmap Display) 1 bit 2 levels Electron Gun 3-Bit Color Display 3 red green blue COLOR: black red green blue yellow cyan magenta white R G B 0 0 0 1 0 0 0 1 0 0 0 1 1 1 0 0 1 1 1 0 1 1 1 1 True Color Display 24 bitplanes, 8 bits per color gun. 8 2 24 8 8 Red Green Blue = 16,777,216 Color Map Look-Up Tables Extends the number of colors that can be displayed by a given number of bit-planes. y RED max GREEN 255 BLUE y 7 6 1001 1010 67 100110100001 R Pixel in bit map at x', y' 0 0 x Frame buffer 0001 G Pixel displayed at x', y' B 0 x max Look-up table Display Pseudo color RED 28 x 24 Color Map LUT 255 254 256 colors chosen from a palette of 16,777,216. Each entry in the color map LUT can be user defined. 3 2 1 0 GREEN BLUE Rendering 3D Scenes Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display The Rendering Pipeline Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline Framebuffer Display Rendering: Transformations We’ve learned about transformations But they are used in three ways: • Modeling transforms • Viewing transforms (Move the camera) • Projection transforms (Change the type of camera) The Rendering Pipeline: 3-D Scene graph Object geometry Result: Modeling Transforms • All vertices of scene in shared 3-D “world” coordinate system Lighting Calculations • Vertices shaded according to lighting model Viewing Transform • Scene vertices in 3-D “view” or “camera” coordinate system Clipping Projection Transform • Exactly those vertices & portions of polygons in view frustum • 2-D screen coordinates of clipped vertices The Rendering Pipeline: 3-D Scene graph Object geometry Modeling Transforms Lighting Calculations Viewing Transform Clipping Projection Transform Result: • 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 other • Object coordinates -> world coordinates Y Y X Z Z X The Rendering Pipeline: 3-D Scene graph Object geometry Modeling Transforms Lighting Calculations Viewing Transform Clipping Projection Transform Result: • Scene vertices in 3-D “view” or “camera” coordinate system Rendering: Transformations Viewing transform • Rotate & translate the world to lie directly in front of the camera – Typically place camera at origin – Typically looking down -Z axis • World coordinates a view coordinates The Rendering Pipeline: 3-D Scene graph Object geometry Modeling Transforms Lighting Calculations Viewing Transform Clipping Projection Transform Result: • 2-D screen coordinates of clipped vertices Rendering: Transformations Projection transform • Apply perspective foreshortening – Distant = small: the pinhole camera model • View coordinates a screen coordinates Rendering: Transformations Perspective Camera Orthographic Camera 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 Introducing OpenGL Recall the rendering pipeline: • Transform geometry (object aworld, world aeye) • Apply perspective projection (eye ascreen) • Clip to the view frustum • Perform visible-surface processing (Z-buffer) • Calculate surface lighting Implementing all this is a lot of work OpenGL provides a standard implementation • So why study the basics?