Upload
barrie-greer
View
227
Download
5
Tags:
Embed Size (px)
Citation preview
1
Dr. Scott Schaefer
Hidden Surfaces
2/62
Hidden Surfaces
3/62
Hidden Surfaces
4/62
Hidden Surfaces
5/62
Backface Culling
6/62
Backface Culling
view direction
v
7/62
Backface Culling
view direction
n
v
8/62
Backface Culling
view direction
n
v
0vn , draw polygon
9/62
Backface Culling
view direction
n
v
0vn , cull polygon
10/62
Backface Culling
11/62
Backface Culling
1
2
3
counter clock-wise orientation, draw polygon
12/62
Backface Culling
1
2
3
clock-wise orientation, cull polygon
13/62
Backface Culling
Advantages Improves rendering speed by removing
roughly half of polygons from scan conversion
DisadvantagesAssumes closed surface with consistently
oriented polygonsNOT a true hidden surface algorithm!!!
14/62
Backface Culling
Is this all we have to do?
15/62
Backface Culling
Is this all we have to do? No!- Can still have 2 (or more) front faces that map to
the same screen pixel
16/62
Backface Culling
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?
17/62
Painter’s Algorithm
Sort polygons according to distance from viewer
Draw from back to front
How do we sort polygons?
18/62
Painter’s Example
z = 0.7z = 0.3
z = 0.1
Sort by depth:Green rectRed circle
Blue triz = 0
19/62
Painter’s Algorithm
20/62
Sometimes there is NO ordering that produces correct results!!!
Painter’s Algorithm
21/62
Painter’s Algorithm
1. Sort all objects’ zmin and zmax
22/62
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
23/62
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
24/62
Painter’s Algorithm
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
25/62
Painter’s Algorithm
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
After splitting, you can resort the list and should be fine
26/62
Painter’s Algorithm-Summary
AdvantagesSimple algorithm for ordering polygons
DisadvantagesSorting criteria difficult to produceRedraws same pixel many timesSorting can also be expensive
27/62
Depth (“Z”) Buffer
Simple modification to scan-conversion Maintain a separate buffer storing the closest
“z” value for each pixel Only draw pixel if depth value is closer than
stored “z” valueUpdate buffer with closest depth value
28/62
Depth (“Z”) Buffer
AdvantagesSimple to implementAllows for a streaming approach to
polygon drawing Disadvantages
Requires extra storage spaceStill lots of overdraw
29/62
Binary Space Partitioning Trees
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
Splitting planes can be arbitrarily oriented
30/62
BSP Trees: Objects
1 2 3
45
6 78
9
31/62
BSP Trees: Objects
1 2 3
45
6 78
9
+
-
32/62
BSP Trees: Objects
1 2 3
45
6 78
9
985 61 42 3
Put front objects in the left branch
7
+
--+
33/62
BSP Trees: Objects
1 2 3
45
6 78
9
985 61 42 3
Put front objects in the left branch
7
-+
-+ -+
34/62
BSP Trees: Objects
1 2 3
45
6 78
9
895 6
1
32 4
Put front objects in the left branch
7
-+
-+ -+
-+ -+ -+
35/62
BSP Trees: Objects
1 2 3
45
6 78
9
865 6
1
32
2 4
Put front objects in the left branch
7 9
-+
-+ -+
+ + -+- -
+ - + -
36/62
BSP Trees: Objects
1 2 3
45
6 78
9
865 6
1
32
2 4
Put front objects in the left branch
7 9
-+
-+ -+
+ + -+- -
+ - + -
When to stop the recursion?
37/62
Object Splitting
No bunnies were harmed in my example But what if a splitting plane passes through
an object?- Split the object; give half to each node:
- Worst case: can create up to O(n3) objects!
Ouch
38/62
Building a BSP Tree
Choose a splitting polygon Sort all other polygons as
Front Behind Crossing On
Add “front” polygons to front child, “behind” to back child
Split “crossing” polygons with infinite plane Add “on” polygons to root Recur
39/62
Building a BSP Tree
1 2
3
4
5
6
7
40/62
Building a BSP Tree
1 2
3
4
5
6
7
2,3,4,5,6,7
1
b
41/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
3
7-2,6, 5-2
1
b
bf
2,4,5-1,7-1
42/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2 4, 5-1
3
7-17-2,6, 5-2
1
b
b
b
f
f
43/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
5-1
3
7-1
47-2,6, 5-2
1
b
b
b
b
f
f
44/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
5-1
3
7-1
47-2,6, 5-2
1
b
b
b
b
f
f
45/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
5-1
3
7-1
47-2,6, 5-2
1
b
b
b
b
f
f
46/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
7-2
5-1
3
7-1
46, 5-2
1
b b
b
b
b
f
f
47/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
5-2
2
7-2
5-1
3
7-1
46
1
b b
bb
b
b
f
f
48/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
b
49/62
2
7-2
Building a BSP Tree
1 2
3
4
6
5-1
3
7-1
46
1
7-2
7-1
5-2
5-1
b b
b
b
b
f
f
5-2
b
50/62
Rendering with a BSP Tree
If eye is in front of plane Draw “back” polygons Draw “on” polygons Draw “front” polygons
If eye is behind plane Draw “front” polygons Draw “on” polygons Draw “back” polygons
Else eye is on plane Draw “front” polygons Draw “back” polygons
51/62
BSP Trees: Objects
1 2 3
45
6 78
9
865 6
1
32
2 4
Correctly traversing this tree enumerates objects from back to front
7 9
-+
-+ -+
+ + -+- -
+ - + -
Traversal order?
52/62
BSP Trees: Objects
1 2 3
45
6 78
9
865 6
1
32
2 4
Correctly traversing this tree enumerates objects from back to front
7 9
-+
-+ -+
+ + -+- -
+ - + -
Traversal order:8->9->7->6->5->3->4->2->1
53/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
Traversal order: 2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
b
54/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
Traversal order:6->(5-2)->(7-2)->3->(5-1)->4->(7-1)->2->1
2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
b
55/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
Traversal order: 2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
b
56/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
bTraversal order:1->2->(7-1)->4->(5-1)->3->(7-2)->(5-2)->6
57/62
Building a BSP Tree
1 2
3
4
6
7-2
7-1
5-2
5-1
Traversal order? 2
7-2
5-1
3
7-1
46
1
b b
b
b
b
f
f
5-2
b
58/62
Rendering with a BSP Tree
Advantages No depth comparisons needed Polygons split and ordered automatically
Disadvantages Computationally intense preprocess stage
restricts algorithm to static scenes Splitting increases polygon count Redraws same pixel many times Choosing splitting plane not an exact science
59/62
Improved BSP Rendering
Take advantage of view direction to cull away polygons behind viewer
60/62
Improved BSP Rendering
Take advantage of view direction to cull away polygons behind viewer
View frustum
61/62
Improved BSP Rendering
Take advantage of view direction to cull away polygons behind viewer
62/62
OpenGL and Hidden Surfaces
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glCullFace ( GL_BACK );
63/62
64/62
Scan Line Algorithm
Assume for each line of screen, we have scan-lines for all polygons intersecting that line
For each polygon, keep track of extents of scan line
Whenever the x-extents of two scan lines overlap, determine ordering of two polygons
65/62
Scan Line Algorithm
66/62
Scan Line Algorithm
67/62
Scan Line Algorithm
68/62
Scan Line Algorithm
AdvantagesTakes advantage of coherence resulting in
fast algorithmDoes not require as much storage as depth
buffer Disadvantages
More complex algorithmRequires all polygons sent to GPU before
drawing