CS 551 / 645: Introductory Computer Graphics Review for Midterm David Luebke 3/10/2016 Administrivia Hand out assignment 2 Hand in assignment 3 Late day policy: – 1 late day = due tomorrow at noon – Subsequent late days add 24 hours each – Weekends and holidays count Compiling with C++ – UNIX C++ compilers: g++ and /bin/CC – I’ll make sure it works before next assignment David Luebke 3/10/2016 Midterm Examination Midterm is this Thursday (March 9) Study aids: – This lecture – Earlier lectures (available on course page) – Last semester’s midterm See http://www.cs.virginia.edu/~luebke/old_cs551/ But, its not quite the same material No calculators! (you won’t need them) David Luebke 3/10/2016 Display Technologies Cathode Ray Tubes – Earliest, still most common graphical display – Understand the basic mechanism Vacuum tube, phosphors, electron beam – Pros: bright, fairly high-res, leverages TV tech – Cons: bulky, size-limited, finicky David Luebke 3/10/2016 Display Technologies Vector versus raster display – Vector: traces lines like an oscilloscope Pros: bright, crisp, uniform lines Cons: wireframe only, flicker for complex scenes – Raster: fixed scan pattern for electron beam, intensity controlled by scan-out from frame buffer David Luebke Pros: display solid objects, image complexity limited only by framebuffer resolution Cons: discreet sampling (aliasing), memory cost 3/10/2016 Display Technologies LCDs – Understand the basic mechanism Polarized light, crystals twist 90º unless excited Basically a light valve: reflective or transmissive – Pros: light-weight and thin – Cons: expensive, high-power (when backlit), limited in size David Luebke 3/10/2016 Display Technologies Also know: – Plasma display panels – Digital Micromirror Devices David Luebke 3/10/2016 Framebuffers Memory array storing image in pixels Issues: memory speed, size, bus contention Different types in common use, motivated mainly by memory cost – True-Color: 24 bits, 8 per RGB (or 32 bits with ) – Hi-Color: 16 bits (R = 6, G = 6, B = 4) – Pseudo-Color: 8 bits index into 256-entiry color lookup table (entries typically 24-bits) David Luebke 3/10/2016 Mathematical Foundations Geometry (2-D, 3-D) Trigonometry Vector spaces – Elements: scalars and vectors – Operations: Addition (identity & inverse) Scalar multiplication (distributive rule) – Linear combinations, dimension, basis sets – Inner (dot) product, vector (cross) product David Luebke 3/10/2016 Mathematical Foundations Affine spaces – Elements: points – Operations: Subtraction (point - point = vector) Addition (vector + point = point) Matrices – Linear transforms, vector-matrix multiplication – Matrix-matrix multiplication – Composition of linear transforms = matrix concatenation David Luebke 3/10/2016 The Rendering Pipeline Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters David Luebke Rendering Pipeline Framebuffer Display 3/10/2016 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 David Luebke • Exactly those vertices & portions of polygons in view frustum • 2-D screen coordinates of clipped vertices 3/10/2016 Geometric Transforms Modeling transforms: object coordinates world coordinates Viewing transform: world coordinates eye coordinates – eye coordinates == camera coordinates == view coordinates Projection transform: eye coordinates 2-D screen coordinates David Luebke 3/10/2016 Geometric Transforms Understand homogeneous coordinates – [x, y, z, w]T == (x/w, y/w, z/w) – Allows us to capture translation and projection as matrices Know your 4x4 Euclidean transform matrices: – Translation, scale, rotation about X, Y, Z Understand rotation about an arbitrary axis Understand order of composition for matrices – In OpenGL: using column vectors as points order from right to left David Luebke 3/10/2016 Perspective Projection Geometry of the perspective projection: View plane X P (x, y, z) x’ = ? (0,0,0) Z d David Luebke 3/10/2016 Perspective Projection Desired result: x' x , d z dx x x' , z z d David Luebke y' y d z dy y y' , zd z z d 3/10/2016 Perspective Projection A matrix that accomplishes this: 1 0 Mperspective 0 0 David Luebke 0 1 0 0 0 1 0 1d 0 0 0 0 3/10/2016 Rasterizing Lines Review McMillan’s great java-enabled lecture First stab: slope-intercept + symmetry A case study in optimization – Special case boundary conditions if necessary – Optimize inner loops Incremental update using DDA (biggest win) Low-level tricks: integer arithmetic, compare to 0, etc. Culmination: Bresenham’s algorithm – Be aware of diminishing returns and readability/portability tradeoffs David Luebke 3/10/2016 Rasterizing Triangles Triangles are nice to deal with because they are always planar and always convex Triangle rasterization techniques: – – – – David Luebke REYES: recursive subdivision of primitive Warnock: recursive subdivision of screen Edge walking Edge equations 3/10/2016 Rasterizing Triangles Edge walking: – – – – Draw edges vertically Fill in horizontal spans for each scanline Interpolate colors down edges At each scanline, interpolate edge colors across span – Pros: Fast: touch only lit pixels, touch pixels only once – Cons: David Luebke Finicky: lots of special cases, hard to get just right 3/10/2016 Rasterizing Triangles Edge Equations – Equation of a line defines two half-spaces – Triangle can be represented as intersection of three half-spaces: David Luebke 3/10/2016 Rasterizing Triangles Basic algorithm: – Walk pixels in bounding box – Evaluate three edge equations – If all are greater than zero, shade pixel Issues: – Computing edge equations: numerical precision – Interpolating parameters (i.e., color): just like another edge equation (why?) Optimizing the algorithm – Like line rasterization: DDA, early termination, etc. David Luebke 3/10/2016 Rasterizing General Polygons Parity test: – Starting outside polygon, count edges crossed. A – Odd = inside, even = outside D B H C G I E Big cost: testing every edge F against every pixel Solution: the active edge table algorithm – Sort edges by Y – Keep a list of edges that intersect current scanline, sorted by their X-intersection w/ scanline David Luebke 3/10/2016 Clipping Lines Cohen-Sutherland Algorithm – Clip 2-D line segments to rectangular viewport – Designed for rapid trivial accept & trivial reject ops David Luebke 4-bit outcodes divide screen into 9 regions Bitwise operations determine whether to accept, reject, or intersect with a viewport edge & recurse May require multiple iterations 3/10/2016 Clipping Polygons Clipping polygons fundamentally more difficult – Polygons can gain or lose edges – Concave polygons can even multiply Sutherland-Hodgman Algorithm – Simplify by divide-and-conquer: consider each clipping plane individually David Luebke Input: polygon as ordered list of vertices Output: polygon as ordered list of vertices Lends itself to pipelined hardware implementation 3/10/2016 Clipping Polygons Sutherland-Hodgman Algorithm – Know the details: Point-plane test Line-plane intersection Rules: inside outside inside outside inside outside p s p p output David Luebke s i output p inside outside p s s no output i output p output 3/10/2016 Clipping in 3-D Problem: clipping under perspective must happen before homogeneous divide – Solution 1: clip to hither plane in eye coordinates, then multiply by projection matrix, then do homogeneous divide Better: transform to canonical perspective coordinates to simplify clipping – Solution 2: clip after projection (must clip all 4 homogeneous coordinates) – Solution 3 (ugly but common): clip to hither & yon before projection, clip to 2-D viewport after projection and divide David Luebke 3/10/2016 Color Rods and cones Cones and color perception – Metamers – 3-D color: X, Y, and Z; CIE color space Gamma correction David Luebke 3/10/2016 Lighting Definitions: illumination, lighting, shading Illumination: – Direct versus indirect – Light properties: geometry, spectrum Common simplifications: ambient, directional, and point – Surface material: geometry, reflectance, microstructure Common simplification: Phong lighting David Luebke Diffuse (Lambertian) reflection: incoming light reflected equally in all directions, proportional to N • L Specular reflection: approximate falloff with (V • R)nshiny 3/10/2016 Lighting Putting it all together: the Phong lighting model I total ka I ambient #lights i 1 I i kd Nˆ Lˆ k s Vˆ Rˆ nshiny Note: evaluate per light, per color component Common simplification: constant V (viewer infinitely far away) David Luebke 3/10/2016 Shading Where to apply lighting calculations? – Once per face: flat shading – Once per vertex, interpolate resulting color: Gouraud shading – Once per pixel, interpolating normal vectors from vertices: Phong shading Flat shading David Luebke Phong Shading 3/10/2016