Hidden Surfaces Dr Scott Schaefer 1 Hidden Surfaces

  • Slides: 68
Download presentation
Hidden Surfaces Dr. Scott Schaefer 1

Hidden Surfaces Dr. Scott Schaefer 1

Hidden Surfaces 2/62

Hidden Surfaces 2/62

Hidden Surfaces 3/62

Hidden Surfaces 3/62

Hidden Surfaces 4/62

Hidden Surfaces 4/62

Backface Culling 5/62

Backface Culling 5/62

Backface Culling view direction 6/62

Backface Culling view direction 6/62

Backface Culling view direction 7/62

Backface Culling view direction 7/62

Backface Culling , draw polygon view direction 8/62

Backface Culling , draw polygon view direction 8/62

Backface Culling , cull polygon view direction 9/62

Backface Culling , cull polygon view direction 9/62

Backface Culling 10/62

Backface Culling 10/62

Backface Culling counter clock-wise orientation, draw polygon 11/62

Backface Culling counter clock-wise orientation, draw polygon 11/62

Backface Culling clock-wise orientation, cull polygon 12/62

Backface Culling clock-wise orientation, cull polygon 12/62

Backface Culling Advantages u Improves rendering speed by removing roughly half of polygons from

Backface Culling Advantages u Improves rendering speed by removing roughly half of polygons from scan conversion n Disadvantages u Assumes closed surface with consistently oriented polygons u NOT a true hidden surface algorithm!!! n 13/62

Backface Culling n Is this all we have to do? 14/62

Backface Culling n Is this all we have to do? 14/62

Backface Culling n Is this all we have to do? No! - Can still

Backface Culling n Is this all we have to do? No! - Can still have 2 (or more) front faces that map to the same screen pixel 15/62

Backface Culling n Is this all we have to do? No! - - Can

Backface Culling n Is this all we have to do? No! - - Can still have 2 (or more) front faces that map to the same screen pixel Which actually gets drawn? 16/62

Painter’s Algorithm Sort polygons according to distance from viewer n Draw from back to

Painter’s Algorithm Sort polygons according to distance from viewer n Draw from back to front n n How do we sort polygons? 17/62

Painter’s Example z = 0. 7 z = 0. 3 z = 0. 1

Painter’s Example z = 0. 7 z = 0. 3 z = 0. 1 Sort by depth: Green rect Red circle Blue tri z=0 18/62

Painter’s Algorithm 19/62

Painter’s Algorithm 19/62

Painter’s Algorithm n Sometimes there is NO ordering that produces correct results!!! 20/62

Painter’s Algorithm n Sometimes there is NO ordering that produces correct results!!! 20/62

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 21/62

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 21/62

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 2. If an object is

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 2. If an object is uninterrupted (its zmin and zmax are adjacent in the sorted list), it is fine 22/62

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 2. If an object is

Painter’s Algorithm 1. Sort all objects’ zmin and zmax 2. If an object is uninterrupted (its zmin and zmax are adjacent in the sorted list), it is fine 3. If 2 objects DO overlap 3. 1 Check if they overlap in x - If not, they are fine 3. 2 Check if they overlap in y - If not, they are fine - If yes, need to split one 23/62

Painter’s Algorithm n The splitting step is the tough one - Need to find

Painter’s Algorithm n The splitting step is the tough one - Need to find a plane to split one polygon by so that each new polygon is entirely in front of or entirely behind the other - Polygons may actually intersect, so then need to split each polygon by the other 24/62

Painter’s Algorithm n The splitting step is the tough one - Need to find

Painter’s Algorithm n The splitting step is the tough one - Need to find a plane to split one polygon by so that each new polygon is entirely in front of or entirely behind the other - Polygons may actually intersect, so then need to split each polygon by the other n After splitting, you can resort the list and should be fine 25/62

Painter’s Algorithm-Summary Advantages u Simple algorithm for ordering polygons n Disadvantages u Sorting criteria

Painter’s Algorithm-Summary Advantages u Simple algorithm for ordering polygons n Disadvantages u Sorting criteria difficult to produce u Redraws same pixel many times u Sorting can also be expensive n 26/62

Depth (“Z”) Buffer Simple modification to scan-conversion n Maintain a separate buffer storing the

Depth (“Z”) Buffer Simple modification to scan-conversion n Maintain a separate buffer storing the closest “z” value for each pixel n Only draw pixel if depth value is closer than stored “z” value u Update buffer with closest depth value n 27/62

Depth (“Z”) Buffer Advantages u Simple to implement u Allows for a streaming approach

Depth (“Z”) Buffer Advantages u Simple to implement u Allows for a streaming approach to polygon drawing n Disadvantages u Requires extra storage space u Still lots of overdraw n 28/62

Binary Space Partitioning Trees n BSP tree: organize all of space (hence partition) into

Binary Space Partitioning Trees n BSP tree: organize all of space (hence partition) into a binary tree - Preprocess: overlay a binary tree on objects in the scene - Runtime: correctly traversing this tree enumerates objects from back to front - Idea: divide space recursively into half-spaces by choosing splitting planes t Splitting planes can be arbitrarily oriented 29/62

BSP Trees: Objects 9 6 5 1 8 7 4 2 3 30/62

BSP Trees: Objects 9 6 5 1 8 7 4 2 3 30/62

BSP Trees: Objects 9 6 7 5 1 - 8 + 4 2 3

BSP Trees: Objects 9 6 7 5 1 - 8 + 4 2 3 31/62

BSP Trees: Objects Put front objects in the left branch + 9 6 5

BSP Trees: Objects Put front objects in the left branch + 9 6 5 1 - 8 7 - + 1 2 3 4 5 6 7 8 9 4 2 3 32/62

BSP Trees: Objects Put front objects in the left branch + 9 6 1

BSP Trees: Objects Put front objects in the left branch + 9 6 1 8 7 - + 5 4 2 - 1 2 + 3 4 5 6 7 8 9 3 33/62

BSP Trees: Objects Put front objects in the left branch + 9 6 1

BSP Trees: Objects Put front objects in the left branch + 9 6 1 8 7 - + 5 4 2 - 1 3 + 2 4 + - + 3 5 6 - + 7 9 8 34/62

BSP Trees: Objects Put front objects in the left branch + 9 6 1

BSP Trees: Objects Put front objects in the left branch + 9 6 1 8 7 - + 5 4 2 - 1 + 3 + 2 2 4 + - + 3 5 - - + 6 6 8 + 7 9 35/62

BSP Trees: Objects Put front objects in the left branch + 9 6 1

BSP Trees: Objects Put front objects in the left branch + 9 6 1 8 7 - + 5 - 4 2 1 + 3 + 2 2 4 + - + 3 5 - - + 6 6 8 + 7 9 When to stop the recursion? 36/62

Object Splitting No bunnies were harmed in my example n But what if a

Object Splitting No bunnies were harmed in my example n But what if a splitting plane passes through an object? n - Split the object; give half to each node: - Worst case: can create up to O(n 3) objects! Ouch 37/62

Building a BSP Tree n n n Choose a splitting polygon Sort all other

Building a BSP Tree n n n Choose a splitting polygon Sort all other polygons as u Front u Behind u Crossing u On Add “front” polygons to front child, “behind” to back child Split “crossing” polygons with infinite plane Add “on” polygons to root Recur 38/62

Building a BSP Tree 6 7 3 1 2 5 4 39/62

Building a BSP Tree 6 7 3 1 2 5 4 39/62

Building a BSP Tree 6 7 3 1 2 5 4 1 b 2,

Building a BSP Tree 6 7 3 1 2 5 4 1 b 2, 3, 4, 5, 6, 7 40/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b f 7 -2, 6, 5 -2 3 b 2, 4, 5 -1, 7 -1 41/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b f 7 -2, 6, 5 -2 3 f 2 b 7 -1 b 4, 5 -1 42/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b f 7 -2, 6, 5 -2 3 f 2 b 7 -1 b 4 b 5 -1 43/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b f 7 -2, 6, 5 -2 3 f 2 b 7 -1 b 4 b 5 -1 44/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b f 7 -2, 6, 5 -2 3 f 2 b 7 -1 b 4 b 5 -1 45/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 f b 3 f 6, 5 -2 2 b 7 -1 b 4 b 5 -1 46/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 47/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 48/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 49/62

Rendering with a BSP Tree n n n If eye is in front of

Rendering with a BSP Tree n n n If eye is in front of plane u Draw “back” polygons u Draw “on” polygons u Draw “front” polygons If eye is behind plane u Draw “front” polygons u Draw “on” polygons u Draw “back” polygons Else eye is on plane u Draw “front” polygons u Draw “back” polygons 50/62

BSP Trees: Objects Correctly traversing this tree enumerates objects from back to front +

BSP Trees: Objects Correctly traversing this tree enumerates objects from back to front + 9 6 1 8 7 - + 5 - 4 2 1 + 3 + 2 2 4 + - + 3 5 - - + 6 6 8 + 7 9 Traversal order? 51/62

BSP Trees: Objects Correctly traversing this tree enumerates objects from back to front +

BSP Trees: Objects Correctly traversing this tree enumerates objects from back to front + 9 6 1 8 7 - + 5 4 2 - 1 + 3 + 2 2 4 + - + 3 5 - - + 6 6 8 + 7 9 Traversal order: 8 ->9 ->7 ->6 ->5 ->3 ->4 ->2 ->1 52/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 Traversal order: 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 53/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 Traversal order: 6 ->(5 -2)->(7 -2)->3 ->(5 -1)->4 ->(7 -1)->2 ->1 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 54/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 Traversal order: 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 55/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 Traversal order: 1 ->2 ->(7 -1)->4 ->(5 -1)->3 ->(7 -2)->(5 -2)->6 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 56/62

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3

Building a BSP Tree 6 7 -2 5 -2 7 -1 5 -1 3 1 2 4 1 b 7 -2 Traversal order? 3 f b 6 b 5 -2 f 2 b 7 -1 b 4 b 5 -1 57/62

Rendering with a BSP Tree n n Advantages u No depth comparisons needed u

Rendering with a BSP Tree n n Advantages u No depth comparisons needed u Polygons split and ordered automatically Disadvantages u Computationally intense preprocess stage restricts algorithm to static scenes u Splitting increases polygon count u Redraws same pixel many times u Choosing splitting plane not an exact science 58/62

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind viewer 59/62

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind viewer View frustum 60/62

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind

Improved BSP Rendering n Take advantage of view direction to cull away polygons behind viewer 61/62

Open. GL and Hidden Surfaces gl. Enable(GL_DEPTH_TEST); gl. Enable(GL_CULL_FACE); gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

Open. GL and Hidden Surfaces gl. Enable(GL_DEPTH_TEST); gl. Enable(GL_CULL_FACE); gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); gl. Cull. Face ( GL_BACK ); 62/62

63/62

63/62

Scan Line Algorithm Assume for each line of screen, we have scan -lines for

Scan Line Algorithm Assume for each line of screen, we have scan -lines for all polygons intersecting that line n For each polygon, keep track of extents of scan line n Whenever the x-extents of two scan lines overlap, determine ordering of two polygons n 64/62

Scan Line Algorithm 65/62

Scan Line Algorithm 65/62

Scan Line Algorithm 66/62

Scan Line Algorithm 66/62

Scan Line Algorithm 67/62

Scan Line Algorithm 67/62

Scan Line Algorithm Advantages u Takes advantage of coherence resulting in fast algorithm u

Scan Line Algorithm Advantages u Takes advantage of coherence resulting in fast algorithm u Does not require as much storage as depth buffer n Disadvantages u More complex algorithm u Requires all polygons sent to GPU before drawing n 68/62