Hidden Surface Removal Visibility l Assumption All polygons
Hidden Surface Removal
Visibility l Assumption: All polygons are opaque l 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 3 D Clipping Inside: Hidden Surface Removal Ø Backface culling Ø Ø Occlusion Ø Ø l Polygons facing away from the viewer 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 l Avoid drawing polygons facing away from the viewer Ø l Front-facing polygons occlude these polygons in a closed polyhedron Test if a polygon is front- or back-facing? back-facing Ideas? front-facing
Detecting Back-face Polygons l The polygon normal of a … Ø front-facing polygon points towards the viewer Ø back-facing polygon points away from the viewer If (n v) > 0 “back-face” If (n v) ≤ 0 “front-face” v = view vector l l back Eye-space test … EASY! Ø “back-face” if nz < 0 gl. Cull. Face(GL_BACK) front
Polygon Normals l Let polygon vertices v 0, v 1, v 2, . . . , vn - 1 be in counterclockwise order and co-planar Calculate normal with cross product: l n = (v 1 - v 0) X (vn - 1 - v 0) Normalize to unit vector with n/║n║ l n v 3 v 2 v 4 v 0 v 1
Normal Direction l l Vertices counterclockwise Front-facing Vertices clockwise Back-facing 2 1 0 0 2 1 Front facing Back facing
Painter’s Algorithm (1) l Assumption: Later projected polygons overwrite earlier projected polygons Graphics Pipeline 3 2 1 Oops! The red polygon Should be obscured by the blue polygon
Painter’s Algorithm (2) l Main Idea Ø l A painter creates a picture by drawing background scene elemens beforeground ones Requirements Ø Ø Draw polygons in back-tofront order Need to sort the polygons by depth order to get a correct image from Shirley
Painter’s Algorithm (3) l Sort by the depth of each polygon Graphics Pipeline 3 depth 2 1 3 2 1
Painter’s Algorithm (4) l l Compute zmin ranges for each polygon Project polygons with furthest zmin first (z) depth zmin
Painter’s Algorithm (5) l Problem: Can you get a total sorting? zmin Correct?
Painter’s Algorithm (6) l Cyclic Overlap Ø l How do we sort these three polygons? Sorting is nontrivial Ø Ø Split polygons in order to get a total ordering Not easy to do in general
Visibility l How do we ensure that closer polygons overwrite further ones in general?
Z-Buffer l Depth buffer (Z-Buffer) Ø Ø Ø A secondary image buffer that holds depth values Same pixel resolution as the color buffer Why is it called a Z-Buffer? Ø l 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 l Visibility testing is done during rasterization
Z-buffer: A Secondary Buffer DAM Entertainment Color buffer Depth buffer
Z-Buffer l How do we calculate the depth values on the polygon interior? P 3 P 4 ys P 2 Scanline order za zp P 1 zb Bilinear Interpolation
Z-buffer - Example
Parallel with the image plane
Not Parallel
Z-Buffer Algorithm l Algorithm easily handles this case
Z-buffering in Open. GL l Create depth buffer by setting GLUT_DEPTH flag in glut. Init. Display. Mode()or the appropriate flag in the PIXELFORMATDESCRIPTOR. l Enable per-pixel depth testing with gl. Enable(GL_DEPTH_TEST) l Clear depth buffer by setting GL_DEPTH_BUFFER_BIT in gl. Clear()
- Slides: 26