Computer Graphics BingYu Chen National Taiwan University HiddenSurface
Computer Graphics Bing-Yu Chen National Taiwan University
Hidden-Surface Removal Back-Face Culling The Depth-Sort Algorithm Binary Space-Partitioning Trees The z-Buffer Algorithm Visible-Surface Ray Tracing (Ray Casting) o Space Subdivision Approaches o o o 1
Hidden-Surface Removal =Visible-Surface Determination o Determining what to render at each pixel. o A point is visible if there exists a direct line-of-sight to it, unobstructed by another objects (visible surface determination). o Moreover, some objects may be invisible because there are behind the camera, outside of the field-ofview, too far away (clipping) or back faced (backface culling). 2
Hidden Surfaces: why care? o Occlusion: Closer (opaque) objects along same viewing ray obscure more distant ones. o Reasons for removal n Efficiency: As with clipping, avoid wasting work on invisible objects. n Correctness: The image will look wrong if we don’t model occlusion properly. 3
Back-Face Culling = Front Facing x B A C D E F H z G 4
Back-Face Culling = Front Facing o use cross-product to get the normal of the face (not the actual normal) o use inner-product to check the facing v 1 N v 2 V v 3 5
Clipping (View Frustum Culling) view frustum occlusion eye back face view frustum 6
List-Priority Algorithms o The Painter’s Algorithm o The Depth-Sort Algorithm o Binary Space-Partitioning Trees 7
The Painter’s Algorithm o Draw primitives from back to front need for depth comparisons. 8
The Painter’s Algorithm o for the planes with constant z o not for real 3 D, just for 2½D o sort all polygons according to the smallest (farthest) z coordinate of each o scan convert each polygon in ascending order of smallest z coordinate (i. e. , back to front) 9
The Depth-Sort Algorithm o sort all polygons according to the smallest (farthest) z coordinate of each o resolve any ambiguities that sorting may cause when the polygons’ z extents overlap, splitting polygons if necessary o scan convert each polygon in ascending order of smallest z coordinate (i. e. , back to front) 10
Overlap Cases y y x R Q P P Q P z Q x x 11
Binary Space-Partitioning Trees o An improved painter’s algorithm o Key observation: T 3 T 1 T 2 T 5 T 4 13
Binary Space-Partitioning Trees - T 1 + T 2 T 3 T 1 T 2 T 3 14
Binary Space-Partitioning Trees - T 1 + T 2 + T 3 T 2 T 3 15
Splitting triangles a a A A c c B b 16
BSP Tree Construction BSPtree make. BSP(L: list of polygons) { if (L is empty) { return the empty tree; } Choose a polygon P from L to serve as root; Split all polygons in L according to P return new Tree. Node ( P, make. BSP(polygons on negative side of P), make. BSP(polygons on positive side of P)) } o Splitting polygons is expensive! It helps to choose P wisely at each step. n Example: choose five candidates, keep the one that splits the fewest polygons. 17
BSP Tree Display void show. BSP(v: Viewer, T: BSPtree) { if (T is empty) return; P = root of T; if (viewer is in front of P) { show. BSP(back subtree of T); draw P; show. BSP(front subtree of T); } else { show. BSP(front subtree of T); draw P; show. BSP(back subtree of T); } } 2 D BSP demo 18
Binary Space-Partitioning Trees o Same BSP tree can be used for any eye position, constructed only once if the scene if static. o It does not matter whether the tree is balanced. However, splitting triangles is expensive and try to avoid it by picking up different partition planes. 20
BSP Tree 6 5 9 7 10 8 1 11 2 4 3 21
BSP Tree 6 1 5 inside ones 9 7 10 outside ones 8 1 11 2 4 3 22
BSP Tree 6 1 5 2 3 4 9 7 10 8 1 11 2 5 6 7 8 9 10 11 4 3 23
BSP Tree 6 1 5 5 9 b 9 7 10 1 11 b 9 a 11 a 2 11 8 6 7 9 a 10 11 a 8 9 b 11 b 4 3 24
BSP Tree 6 5 1 9 b 9 7 10 1 11 b 9 a 11 a 2 5 2 3 8 4 11 9 b 7 9 a 4 8 6 11 b 10 3 11 a 25
BSP Tree 6 5 1 9 b 9 7 10 1 11 b 9 a 11 a 2 3 8 4 11 point 8 6 9 b 7 9 a 4 3 5 2 11 b 10 11 a 26
BSP Tree Traversal 6 5 1 9 b 9 7 10 1 11 b 9 a 11 a 2 3 8 4 11 point 8 6 9 b 7 9 a 4 3 5 2 11 b 10 11 a 27
BSP Tree Traversal 6 5 1 9 b 9 7 10 1 11 b 9 a 11 a 2 3 8 4 11 point 8 6 9 b 7 9 a 4 3 5 2 11 b 10 11 a 28
The z-Buffer Algorithm o Resolve depths at the pixel level o Idea: add Z to frame buffer, when a pixel is drawn, check whether it is closer than what’s already in the frame buffer 29
The z-Buffer Algorithm + = 30
The z-Buffer Algorithm void z. Buffer() { int pz; for (each polygon) { for (each pixel in polygon’s projection) { pz=polygon’s z-value at (x, y); if (pz>=Read. Z(x, y)) { Write. Z(x, y, pz); Write. Pixel(x, y, color); } } 31
The z-Buffer Algorithm y z 1 y 1 za ys y 2 y 3 zp zb Scan line z 2 z 3 32
z-Buffer: Example color buffer depth buffer 33
The z-Buffer Algorithm o Benefits n Easy to implement n Works for any geometric primitive n Parallel operation in hardware o independent of order of polygon drawn o Limitations n n Memory required for depth buffer Quantization and aliasing artifacts Overfill Transparency does not work well 34
Ray Tracing = Ray Casting select center of projection and window on viewplane; for (each scan line in image) { for (each pixel in scan line) { determine ray from center of projection through pixel; for (each object in scene) { if (object is intersected and is closest considered thus far) record intersection and object name; } set pixel’s color to that at closest object intersection; } } 38
Ray Casting Center of projection Window 39
Ray Casting (Appel, 1968) 40
Ray Casting (Appel, 1968) 41
Ray Casting (Appel, 1968) 42
Ray Casting (Appel, 1968) 43
Ray Casting (Appel, 1968) direct illumination 44
Spatial Partitioning 45
Spatial Partitioning R C A B 46
Spatial Partitioning 1 A B 3 2 47
Space Subdivision Approaches Uniform grid K-d tree 48
Space Subdivision Approaches Quadtree (2 D) Octree (3 D) BSP tree 49
Uniform Grid 50
Uniform Grid Preprocess scene 1. Find bounding box 51
Uniform Grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 52
Uniform Grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place object in cell if its bounding box overlaps the cell 53
Uniform Grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place object in cell if its bounding box overlaps the cell 4. Check that object overlaps cell (expensive!) 54
Uniform Grid Traversal Preprocess scene Traverse grid 3 D line = 3 D-DDA 55
From Uniform Grid to Quadtree 56
Quadtree (Octrees) subdivide the space adaptively 57
Quadtree Data Structure Quadrant Numbering 58
Quadtree Data Structure Quadrant Numbering 59
Quadtree Data Structure Quadrant Numbering 60
Quadtree Data Structure Quadrant Numbering 61
From Quadtree to Octree y x z 62
K-d Tree A A 63 Leaf nodes correspond to unique regions in space
K-d Tree A B A 64
K-d Tree A B B A 65
K-d Tree A C B B A 66
K-d Tree A C B A 67
K-d Tree A C D B C B A 68
K-d Tree A C D B C B D A 69
K-d Tree A D B B C C D A Leaf nodes correspond to unique regions in space 70
K-d Tree Traversal A D B B C C D A Leaf nodes correspond to unique regions in space 71
- Slides: 67