cs5620 hidden surface intro to computer graphics removalcomputer science dept., technion hidden...

7
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

Upload: others

Post on 25-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 2: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 3: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 4: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 5: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 6: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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

Page 7: CS5620 Hidden Surface Intro to Computer Graphics RemovalComputer Science Dept., Technion Hidden Surface Removal Page 6 Z Fighting 31 When Z-buffer has low precision and/or is not chosen

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