Computer Graphics CMU 15-462/15-662, Spring 2018 What You Know How to Do (At This Point in the Course)
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 8: The Rasterization Pipeline (and its implementation on GPUs) Computer Graphics CMU 15-462/15-662, Spring 2018 What you know how to do (at this point in the course) y (w, h) z y x z x (0, 0) Position objects and the Determine the position of Project objects onto camera in the world objects relative to the camera the screen Sample triangle coverage Compute triangle attribute Sample texture maps values at covered sample points CMU 15-462/662, Spring 2018 What else do you need to know to render a picture like this? Surface representation How to represent complex surfaces? Occlusion Determining which surface is visible to the camera at each sample point Lighting/materials Describing lights in scene and how materials reflect light. CMU 15-462/662, Spring 2018 Course roadmap Introduction Drawing a triangle (by sampling) Drawing Things Transforms and coordinate spaces Key concepts: Perspective projection and texture sampling Sampling (and anti-aliasing) Coordinate Spaces and Transforms Today: putting it all together: end-to-end rasterization pipeline Geometry Materials and Lighting CMU 15-462/662, Spring 2018 Occlusion CMU 15-462/662, Spring 2018 Occlusion: which triangle is visible at each covered sample point? Opaque Triangles 50% transparent triangles CMU 15-462/662, Spring 2018 Review from lastLecture class 5 Math Assume we have a triangle defined(x, by y) the screen-space 2D position and distance (“depth”) from the camera of each vertex. T p0x p0y ,d0 T ⇥p1x p1y⇤ ,d1 Lecture 5 Math T ⇥p2x p2y⇤ ,d2 ⇥ ⇤ How do we compute the depth of the triangle at covered sample point ( x, y ) ? Interpolate it just like any other attribute that varies linearly overp the0x surfacep0y ,d 0 of the triangle. ⇥p1x p1y⇤ ,d1 ⇥p2x p2y⇤ ,d2 CMU 15-462/662, Spring 2018 ⇥ ⇤ Occlusion using the depth-buffer (Z-buffer) For each coverage sample point, depth-buffer stores depth of closest triangle at this sample point that has been processed by the renderer so far. Closest triangle at sample point (x,y) is triangle with minimum depth at (x,y) Initial state of depth buffer before rendering any triangles (all samples store farthest distance) Grayscale value of sample point used to indicate distance Black = small distance White = large distance CMU 15-462/662, Spring 2018 Depth buffer example CMU 15-462/662, Spring 2018 Example: rendering three opaque triangles CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) Processing yellow triangle: Grayscale value of sample point depth = 0.5 used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) After processing yellow triangle: Grayscale value of sample point used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) Processing blue triangle: Grayscale value of sample point depth = 0.75 used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) After processing blue triangle: Grayscale value of sample point used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) Processing red triangle: Grayscale value of sample point depth = 0.25 used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth-buffer (Z-buffer) After processing red triangle: Grayscale value of sample point used to indicate distance White = large distance Black = small distance Red = sample passed depth test Color buffer contents Depth buffer contents CMU 15-462/662, Spring 2018 Occlusion using the depth buffer bool pass_depth_test(d1, d2) { return d1 < d2; } depth_test(tri_d, tri_color, x, y) { if (pass_depth_test(tri_d, zbuffer[x][y]) { // triangle is closest object seen so far at this // sample point. Update depth and color buffers. zbuffer[x][y] = tri_d; // update zbuffer color[x][y] = tri_color; // update color buffer } } CMU 15-462/662, Spring 2018 Does depth-buffer algorithm handle interpenetrating surfaces? Of course! Occlusion test is based on depth of triangles at a given sample point. The relative depth of triangles may be different at different sample points. Green triangle in front of yellow triangle Yellow triangle in front of green triangle CMU 15-462/662, Spring 2018 Does depth-buffer algorithm handle interpenetrating surfaces? Of course! Occlusion test is based on depth of triangles at a given sample point. The relative depth of triangles may be different at different sample points. CMU 15-462/662, Spring 2018 Does depth buffer work with super sampling? Of course! Occlusion test is per sample, not per pixel! This example: green triangle occludes yellow triangle CMU 15-462/662, Spring 2018 Color buffer contents CMU 15-462/662, Spring 2018 Color buffer contents (4 samples per pixel) CMU 15-462/662, Spring 2018 Final resampled result Note anti-aliasing of edge due to filtering of green and yellow samples. CMU 15-462/662, Spring 2018 Summary: occlusion using a depth buffer ▪ Store one depth value per coverage sample (not per pixel!) ▪ Constant space per sample - Implication: constant space for depth buffer ▪ Constant time occlusion test per covered sample - Read-modify write of depth buffer if “pass” depth test - Just a read if “fail” ▪ Not specific to triangles: only requires that surface depth can be evaluated at a screen sample point But what about semi-transparent surfaces? CMU 15-462/662, Spring 2018 Compositing CMU 15-462/662, Spring 2018 Representing opacity as alpha Alpha describes the opacity of an object - Fully opaque surface: α = 1 - 50% transparent surface: α = 0.5 - Fully transparent surface: α = 0 Red triangle with decreasing opacity α = 1 α = 0.75 α = 0.5 α = 0.25 α =0 CMU 15-462/662, Spring 2018 Alpha: additional channel of image (rgba) α of foreground object CMU 15-462/662, Spring 2018 Over operator: Composite image B with opacity αB over image A with opacity αA A A over B != B over A B B “Over” is not commutative A B over A A over B Koala over NYC CMU 15-462/662, Spring 2018 Fringing Poor treatment of color/alpha can yield dark “fringing”: foreground color foreground alpha background color fringing no fringing CMU 15-462/662, Spring 2018 No fringing CMU 15-462/662, Spring 2018 Fringing (…why does this happen?) CMU 15-462/662, Spring 2018 Lecture 5 Math Lecture 5 Math d (x, y)=Ax + By + C wd (x, y)=Ax + By + C w T p0x p0y ,d0 T p p T ,d ⇥p01x p10yy⇤ ,d10 T ⇥p p ⇤T ,d ⇥p12x p21yy⇤ ,d21 LectureT 5 Math ⇥⇥p2x p2y⇤⇤ ,d2 C =⇥↵ B +(1⇤ ↵ )↵ A B − B A C = ↵BB +(1d ↵B)↵AA Over operator:(−x, y)= non-premultipliedAx + By + C alpha ↵ = ↵ +(1 ↵ )↵ C B w − B A Composite image B with opacity αB over image A with opacity αA ↵ = ↵ +(1 ↵ )↵ C B − B A A first attempt: T A = A A A T r g p0xb p0y ,d0 T A = ⇥A A A ⇤ r g⇥ b ⇤T A p1x T p1y ,d1 B B = ⇥Br Bg Bb ⇤ T T B = ⇥B B⇥p2Bx⇤ p2y⇤ ,d2 B over A r g b Appearance of semi- transparent A Composited⇥ color: ⇥ ⇤ ⇤ A C = ↵ B +(1 ↵ )↵ A B B − B A Appearance of What B lets through A over B semi-transparent B ↵C = ↵B +(1 ↵B)↵A A over B != B over A − “Over” is not commutative CMU 15-462/662, Spring 2018 Lecture 5 Math Lecture 5 Math d (x, y)=Ax + By + C wd (x, y)=LectureAx + By + 5C Math w T p0x p0y ,d0 T p0xd p0y T ,d0 ⇥p1x (px,1y y⇤)=,dAx1 + By + C w T Lecture⇥p1x p1y 5⇤T Math,d1 ⇥pLecture2x p2y⇤ ,d 5 Math2 T ⇥p p ⇤ ,dT ⇥ 2x p2y0⇤x p0y2 ,d0 Cd =⇥d↵ B +(1⇤ ↵ )↵T A (x, yB)=⇥Axp1x+ BypB1y+⇤AC,d1 w (x, y)=1xAx− +1yBy + C1 COver=w↵ B +(1operator:↵ )↵ A premultiplied alpha B ⇥ B ⇤TA ⇥p2x− p2y⇤ ,d2 Composite↵C = ↵B +(1 imageT ↵ TBB with)↵A opacity αB over image A with opacity αA p0xp0px⇥0yp0−y,d,d⇤0 0 Not premultiplied: ↵C Lecture= ↵B +(1 5↵ MathB)↵A ⇥ C⇥ = ↵⇤TB−⇤+(1T ↵ )↵ A Non-premultipliedp1Cxp=1px 1↵yBpB1y,d+(1 alpha:,d1T 1↵B)↵AA A = Ar Ag Ab − ↵ = ↵T +(1T ↵ )↵ ⇥p2x⇥p↵Cp2=yp⇤↵B,d⇤+(1,d2T ↵B)↵A dA = ⇥A2rx Ag2y Ab⇤ −2 (x, y)=Ax + By + C T A w⇥ ⇥ A =⇤ A⇤r ATg Ab B Not premultiplied: B = ⇥Br Bg Bb ⇤ ⇥ T ⇤T C =CB↵==BB⇥↵BBB+(1B=B+(1B↵rBB⇤)B↵↵BgA)A↵BAbA two multiplies, one add r −g T −b B over A p0x p0y ,d0 (referring to vector ops on colors) Premultiplied: ↵C = ↵B +(1⇥ ↵B)↵A ⇤ ⇥ − ⇤ C0 = BT+(1 ↵B)A Premultiplied⇥p1x 0 p1 alpha:y⇤ ,dT 1 B A↵=C =Ar↵BA+(1g Ab ↵−B)↵A − T A = ↵ A ↵T A ↵ A ↵ A0 ⇥=p ↵AApr ⇤↵A,dAgT ↵AAb ↵A B = ⇥2Bxr B2yg Bb⇤ 2 ⇥ ⇤ T B0 = ⇥↵BBr ↵BBg ↵BBb ↵B⇤ Premultiplied: 0 ⇥ ⇥ B r ⇤ B ⇤g B b B C0 = ⇥B’+(1 ↵B)A’ ⇤ one multiply, one add C = ↵BB +(1− ↵B)↵AA − T A0 = ↵ Ar ↵ Ag ↵ A ↵ CompositeA A alpha: A b A T B0 = ⇥↵ Br ↵ Bg ↵ B ↵ ⇤ Notice premultiplied alpha composites alpha just like how it composites rgb.