Visibility Assumption: All polygons are opaque Hidden Surface Removal � What polygons are visible with respect to your view frustum? � Outside: View Frustum Clipping � Remove polygons outside of the view volume � For example, Liang-Barsky 3D Clipping � Inside: Hidden Surface Removal � Backface culling � CSE 581 Polygons facing away from the viewer � Occlusion � Polygons farther away are obscured by closer polygons � Full or partially occluded portions � Why should we remove these polygons? � Avoid unnecessary expensive operations on these polygons later � No Lines Removed Hidden Lines Removed
Hidden Surfaces Removed Occlusion: Full, Partial, None Full Partial None • The rectangle is closer than the triangle • Should appear in front of the triangle Backface Culling Detecting Back-face Polygons � The polygon normal of a … � Avoid drawing polygons facing away from the viewer front-facing polygon points towards the viewer � Front-facing polygons occlude these polygons in a closed � back-facing polygon points away from the viewer � polyhedron � Test if a polygon is front- or back-facing? If ( n � v) > 0 ⇒ “back-face” If ( n � v) ≤ 0 ⇒ “front-face” back back-facing v = view vector Ideas? � Eye-space test … EASY! “back-face” if n z < 0 � front-facing front glCullFace (GL_BACK) �
Polygon Normals Normal Direction � Let polygon vertices v 0 , v 1 , v 2 ,..., v n - 1 be in � Vertices counterclockwise ⇒ Front-facing counterclockwise order and co-planar � Vertices clockwise ⇒ Back-facing � Calculate normal with cross product: n = ( v 1 - v 0 ) X ( v n - 1 - v 0 ) 2 1 � Normalize to unit vector with n / ║ n ║ 0 0 n v 3 1 2 v 2 v 4 Front facing Back facing v 1 v 0 Painter’s Algorithm (1) Painter’s Algorithm (2) � Assumption: Later projected polygons overwrite earlier � Main Idea projected polygons A painter creates a picture � by drawing background scene elemens before Graphics Pipeline foreground ones 3 2 1 3 2 1 � Requirements Draw polygons in back-to- � Oops! The red polygon Should be obscured by front order the blue polygon Need to sort the polygons � by depth order to get a correct image from Shirley
Painter’s Algorithm (3) Painter’s Algorithm (4) � Sort by the depth of each polygon � Compute z min ranges for each polygon � Project polygons with furthest z min first Graphics Pipeline 3 2 1 3 2 1 depth (z) depth z min z min z min z min Painter’s Algorithm (5) Painter’s Algorithm (6) � Problem: Can you get a total sorting? � Cyclic Overlap � How do we sort these three polygons? z min z min z min z min � Sorting is nontrivial � Split polygons in order to get a total ordering � Not easy to do in general Correct?
Visibility Z-Buffer � Depth buffer (Z-Buffer) � How do we ensure that closer polygons � A secondary image buffer that holds depth values overwrite further ones in general? � Same pixel resolution as the color buffer � Why is it called a Z-Buffer ? � After eye space, depth is simply the z -coordinate � Sorting is done at the pixel level � Rule : Only draw a polygon at a pixel if it is closer than a polygon that has already been drawn to this pixel Z-Buffer Algorithm Z-buffer: A Secondary Buffer � Visibility testing is done during rasterization DAM Entertainment Color buffer Depth buffer
Z-buffer - Example ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Z-Buffer ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ How do we calculate the depth values on the polygon interior? � ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ Z-buffer ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ P 3 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ − ( ) y y = + − ( ) ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ z z z z 1 s − a 1 4 1 ( ) y y ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 4 − ( ) y y = + − ( ) z z z z 1 s − b 1 2 1 ( ) y y 1 2 − ( ) x x P 4 P 2 = + − ( ) a p Scanline order z z z z − p a b a ( ) x x Screen a b y s z a z p z b Bilinear Interpolation P 1 [0,7,5] [6,7,5] [0,6,7] 5 5 5 5 5 5 5 7 5 5 5 5 5 5 6 7 5 5 5 5 5 5 6 7 Parallel with 5 5 5 5 Not Parallel the image plane 4 5 6 7 5 5 5 3 4 5 6 7 5 5 2 3 4 5 6 7 5 [0,1,5] [0,1,2] [5,1,7] 5 5 5 5 5 5 5 ∞ 5 5 5 5 5 5 5 ∞ 5 5 5 5 5 5 ∞ ∞ 5 5 5 5 5 5 ∞ ∞ 5 5 5 5 5 ∞ ∞ ∞ 5 5 5 5 5 ∞ ∞ ∞ 5 5 5 5 ∞ ∞ ∞ ∞ 5 5 5 5 ∞ ∞ ∞ ∞ 5 5 5 ∞ ∞ ∞ ∞ ∞ 4 5 5 7 ∞ ∞ ∞ ∞ 5 5 ∞ ∞ ∞ ∞ ∞ ∞ 3 4 5 6 7 ∞ ∞ ∞ 5 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 2 3 4 5 6 7 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
Z-Buffer Algorithm Z-buffering in OpenGL � Algorithm easily handles this case � Create depth buffer by setting GLUT_DEPTH flag in glutInitDisplayMode()or the appropriate flag in the PIXELFORMATDESCRIPTOR. � Enable per-pixel depth testing with glEnable(GL_DEPTH_TEST) � Clear depth buffer by setting GL_DEPTH_BUFFER_BIT in glClear()
Recommend
More recommend