cs5620 hidden surface intro to computer graphics removalcomputer science dept., technion hidden...
TRANSCRIPT
1
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 1
Hidden Surface Removal
Reminder - Pipeline
2
3D Model Transformations
Polygon at [(2,9), (5,7), (8,9)]
Polygon at [โฆ ]
Reminder - Pipeline
Camera coordinates
Model-view Perspective
division Projection
Clip coordinates
Object coordinates
๐ฅ๐ฆ๐ง1
Normalized device coordinates
๐ฅ๐
๐ฆ๐
๐ง๐
Viewport
Screen coordinates
๐ฅ๐
๐ฆ๐
3
Hidden Surface Removal
โข Input: Polygons in normalized device coordinates
โข Output: 2D image of projected polygons containing only visible portions
๐ฅ๐
๐ฆ๐
4
Hidden Surface Removal Algorithms
โข Back face culling
โข Painterโs algorithm (depth sort)
โข Z-Buffer
โข Scan-line Z-Buffer
5
Back Face Culling Object Space
In a closed polyhedron back faces are not visible
6
torus demo
2
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 2
7
The Normal Vector
๐ฃ3
๐ 1,2,3 = ๐ 2,3,1 = โ๐(2,1,3)
๐ฃ1 ๐ฃ2
๐ = (๐ฃ3 โ ๐ฃ1) ร (๐ฃ2 โ ๐ฃ1)
๐ = (๐ฃ3 โ ๐ฃ1) ร (๐ฃ2 โ ๐ฃ1)
Back Face Culling Determining back faces
โข In a closed polyhedron back faces are not visible
โข Assume normals point out
8
face visible iff: โ๐
2โค ๐ โค
๐
2
๐๐๐ ๐ โฅ 0 iff
๐ฃ โ ๐ = |๐ฃ||๐|๐๐๐ ๐ โฅ 0 iff
Back Face culling When will it work?
9
Closed, convex Open Closed, non convex
Back Face Culling
โข Closed convex objects โ All back faces invisible โ All front faces visible โ ๐๐ข๐ฌ๐ข๐๐ข๐ฅ๐ข๐ญ๐ฒ ๐ฉ๐ซ๐จ๐๐ฅ๐๐ฆ ๐ฌ๐จ๐ฅ๐ฏ๐๐
โข Open objects โ Back faces possibly visible
โข Closed non-convex objects โ Invisible back faces โ Front faces can be visible, invisible or
partially visible 10
Painterโs Algorithm Object space
โข Which color to draw?
โข Draw everything โ which order to draw?
11
Painterโs Algorithm
โข Sort polygons by depth values
โข Paint back to front
โข When will this fail?
12
intersecting polygons cyclic occlusion
3
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 3
Painterโs Algorithm
โข Works in special cases
โ E.g. polygons with constant z
โ Where do we have polygons with constant z?
13
Painterโs Algorithm How do we fix it?
โข Depth sort per polygon doesnโt work
โข Depth sort per pixel
โ Image space algorithm
14
Z-Buffer Algorithm Image Space
15
Resolve visibility at the pixel level
Store color + current z per pixel
Put new color only if new z < current z
Z-Buffer Algorithm The Z-Buffer
16
Z-Buffer Algorithm The Algorithm
For every pixel (x,y)
putZ(x,y,MaxZ)
For each polygon P
Q = project(P)
for each pixel (x,y) in Q
z = depth(Q,x,y)
if z < getZ(x,y)
putColor(x,y,col(P))
putZ(x,y,z)
end
end
end
4 3
1
2
10 1
1
Projected scene
Z buffer
Image
1
1
A
B
C
D Z-Buffer Algorithm The Algorithm
For every pixel (x,y)
putZ(x,y,MaxZ)
For each polygon P
Q = project(P)
for each pixel (x,y) in Q
z = depth(Q,x,y)
if z < getZ(x,y)
putColor(x,y,col(P))
putZ(x,y,z)
end
end
end
4 3
1
2
10 1
1
Projected scene
Z buffer
Image
1
4
4
2
4
2
4
A
B
C
D
4
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 4
Z-Buffer Algorithm The Algorithm
For every pixel (x,y)
putZ(x,y,MaxZ)
For each polygon P
Q = project(P)
for each pixel (x,y) in Q
z = depth(Q,x,y)
if z < getZ(x,y)
putColor(x,y,col(P))
putZ(x,y,z)
end
end
end
4 3
1
2
10
1
Projected scene
Z buffer
Image
3
4
2
4
2
3
3 3
1
3
1
3
A
B
C
D Computing depth(Q,x,y)
โข Have z coordinate at 3 vertices
โข How do we compute z at pre-image of projected point?
20
๐ง1
๐ง2
๐ง3
Computing depth(Q,x,y)
21
๐ฅ
๐ฆ
๐ง
โข We know
โ 3D coordinates at vertices
โ 2D coordinates at projected vertices
โ 2D coordinates at ๐ = (๐ฅ, ๐ฆ)
โข We need
โ 3D coordinates at ๐ฃ
๐
๐ฃ
Computing depth(Q,x,y)
22
โข Linear transformations preserve straight lines
1. Compute ๐๐๐ and ๐๐๐
2. Express ๐ with ๐๐ and ๐๐๐
3. Compute ๐ in the same way from ๐๐ and ๐๐๐
๐ฅ
๐ฆ
๐ง
๐
๐
๐ง1 ๐ง2
๐ง3
๐๐๐
๐1
๐2
๐3
๐๐๐
Linear Interpolation On a line
โข Input: 2 points, 2 values
โข Output: value at any point ๐ on the line ๐ฟ(๐ก) between them
23
๐ = ๐ฟ ๐ก , ๐(๐)
๐1, ๐1
๐0, ๐0
๐ฟ ๐ก = ๐ก๐1 + 1 โ ๐ก ๐0
๐ฟ 0 = ๐0 ๐ฟ 1 = ๐1
๐ฟ 0.5 =๐0 + ๐1
2
๐1, ๐1, ๐0, ๐0, ๐
๐ =๐โ๐0
๐1โ๐0 =
๐ฆโ๐ฆ0
๐ฆ1โ๐ฆ0 =
๐ฅโ๐ฅ0
๐ฅ1โ๐ฅ0
๐
๐ ๐ = ๐ ๐ก = ๐ก๐1 + 1 โ ๐ก ๐0
๐ก โ [0,1]
Computing depth(Q,x,y)
24
โข Apply linear interpolation on a line twice:
๐ฅ
๐ฆ
๐ง
๐
๐
๐ง1 ๐ง2
๐ง3
๐๐๐
๐1
๐2
๐3
๐12
๐๐๐ = ๐๐๐๐ง2 + 1 โ ๐๐๐ ๐ง1
๐ = ๐๐๐๐ + 1 โ ๐ ๐ง3
๐๐๐ ๐
๐๐๐ =๐12 โ ๐1
๐2 โ ๐1
๐ =๐ โ ๐3
๐12 โ ๐3
5
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 5
25
Z-Buffer โ depth(Q,x,y)
1z
2z3z
scanline Y=y
( , )x y
12 12 1 12 2(1 )z t z t z 13 13 1 13 3(1 )z t z t z
123 12 123 13depth( , , ) (1 )Q x y t z t z
3 13212 13 123
2 1 3 1 13 12
, ,y y x xy y
t t ty y y y x x
123 12 123 13
123 12 1 12 2 123 13 1 13 3
123 12 123 13 1 123 12 2 123 13 3
1 1 2 2 3 3 1 2 3
depth( , , ) (1 )
( (1 ) ) (1 )( (1 ) )
( (1 ) ) (1 ) (1 )(1 )
; 1
Q x y t z t z
t t z t z t t z t z
t t t t z t t z t t z
z z z
Rasterize using Bresenham algorithm
Linear Interpolation On a triangle
โข Input: 3 points, 3 values
โข Output: value at any point ๐ in the triangle the points span
26
๐ = ๐ผ1๐1 + ๐ผ2๐2 + ๐ผ3๐3
๐ผ1 + ๐ผ2 + ๐ผ3 = 1 ๐ผ๐ โฅ 0
๐1, ๐1, ๐2, ๐2, ๐3, ๐3, ๐
๐๐ฎ๐ญ ๐ถ๐ =?
๐ ๐ = ๐ ๐ผ1, ๐ผ2, ๐ผ3 = ๐ผ1๐1 + ๐ผ2๐2 + ๐ผ3๐3
๐ ๐1, ๐1
๐2, ๐2
๐3, ๐3
(-1, 1,1)
p=(x,y,z)
Barycentric Coordinates
27
p1=(x1,y1,z1)
p2=(x2,y2,z2)
p3=(x3,y3,z3)
A2 A1
A3
Barycentric coordinates of p = (1, 2, 3) B.C. are unique. B.C. of all interior points are 0. Triangle centroid = (1/3,1/3,1/3).
(0,0,1)
(1,0,0)
(0,1,0)
๐ถ๐ =๐ด๐
๐ด1 + ๐ด2 + ๐ด3
28
Z-Buffer - Project(P)
zp monotone with respect to z โ use as depth to determine order
z
zp
z1 z2
1 0 0 0
0 1 0 0
( )0 0
10 0 1/ 0 /
xx
yy
d d d zz
d d d
d z d
2
/
/
p
p
p
x
z dxy
yz d
zd z
z d
29
Z-Buffer Algorithm
โข Image space algorithm
โข Data structure: Array of depth values
โข Implemented in hardware due to simplicity
โข Depth resolution of 32 bits is common
โข Scene may be updated on the fly, adding new polygons
30
6
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 6
Z Fighting
31
When Z-buffer has low precision and/or
is not chosen correctly
z
zp
32
Transparency Z-Buffer
How can we emulate transparent objects?
33
Transparency Z-Buffer
โข Extension to the basic Z-buffer algorithm
โข Save all pixel values
โข At the end โ have list of polygons & depths (order) for each pixel
โข Simulate transparency by weighting the different list elements, in order
โข Do we really need to store all pixel values?
34
The Graphics Pipeline
โข Hardware implementation of screen Z-buffer:
โ Polygons sent through pipeline one at a time
โ Display updated to reflect each new polygon
Geometry Processing
(viewing transformations)
Rasterizer (scan-conversion
Z-buffer)
35
A={ }
a
d c
b
A={a,d}
A={c,d,b}
A={b}
A={ }
Scan-Line Z-Buffer Algorithm
36
โข In software implementations - amount of memory required for screen Z-buffer may be prohibitive
โข Scan-line Z-buffer algorithm:
โ Render the image one line at a time
โ Take into account only polygons affecting this line
โข Combination of polygon scan-conversion & Z-buffer algorithms
โข Only Z-buffer the size of scan-line is required.
โข Entire scene must be available in advance
โข Image cannot be updated incrementally
Scan-Line Z-Buffer Algorithm
7
CS5620
Intro to Computer Graphics
Copyright
C. Gotsman, G. Elber, M. Ben-Chen
Computer Science Dept., Technion
Hidden Surface Removal
Page 7
37
Scan-Line Z-Buffer Algorithm ScanLineZBuffer(Scene)
Scene2D := Project(Scene);
Sort Scene2D into buckets of polygons P in increasing YMin(P) order;
A := EmptySet;
for y := YMin(Scene2D) to YMax(Scene2D) do
for each pixel (x, y) in scanline Y=y do PutZ(x, MaxZ);
A := A + {P in Scene : YMin(P)<=y};
A := A - {P in A : YMax(P)<y};
for each polygon P in A
for each pixel (x, y) in Pโs span(s) on the scanline
z := Depth(P, x, y);
if (z<GetZ(x)) then
PutColor(x, y, Col(P));
PutZ(x, z);
end;
end;
end;
end;
A={ }
b
A={a,d}
A={c,d,b}
A={b}
A={ }
a
d c
Line and Polygon Clipping Algorithms
38
โข Cohen-Sutherland
โข Sutherland-Hodgman
โข Liang-Barksy
โข Cyrus-Beck