94
Clipping and Scan Conversion Michael Kazhdan (601.357/456) HB Ch. 3.2, 3.11, 6.7, 6.8 FvDFH Ch. 3.2, 3.6, 3.12, 3.14

Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Clipping and Scan Conversion

Michael Kazhdan

(601.357/456)

HB Ch. 3.2, 3.11, 6.7, 6.8

FvDFH Ch. 3.2, 3.6, 3.12, 3.14

Page 2: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Announcements

• Assignment 2 posted!

Page 3: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Lighting

ViewingTransformation

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

3D Camera Coordinates

2D Image Coordinates

3D Model

2D Image

Page 4: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

3D World

Coordinates

3D Object

Coordinates

Page 5: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

3D World

Coordinates

Transform = 𝑀𝑀 = local to world transform

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

Page 6: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

3D World

Coordinates

3D Camera

Coordinates(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

Transform = 𝑀𝑀 = local to world transform

Page 7: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

Camera Right

Camera Up

x

y

zCamera Back

𝑇𝐶→𝑊 is the camera to world transform𝑅𝑥 𝑈𝑥 𝐵𝑥 𝐸𝑥𝑅𝑦 𝑈𝑦 𝐵𝑦 𝐸𝑦𝑅𝑧 𝑈𝑧 𝐵𝑧 𝐸𝑧0 0 0 1

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

Transform = 𝑇𝐶→𝑊−1 𝑀

Page 8: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

Transform = 𝑃𝑇𝐶→𝑊−1 𝑀

𝑃 = projection transform

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

𝑃𝑜 =

1 0 𝐿 cos𝜙 00 1 𝐿 sin𝜙 00 0 0 00 0 0 1

𝑃𝑝 =

1 0 0 00 1 0 00 0 1 00 0 1 0

Page 9: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

𝑣𝑥1 𝑣𝑥

2𝑣𝑦1

𝑣𝑦2

𝑤𝑥1 𝑤𝑥

2𝑤𝑦1

𝑤𝑦2

Window Viewport

Screen Coordinates Image Coordinates

(𝑤𝑥, 𝑤𝑦) (𝑣𝑥 , 𝑣𝑦)

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′)

𝑉 = viewport transform

𝑉 =1 0 𝑣𝑥

1

0 1 𝑣𝑥2

0 0 1

𝑣𝑥2 − 𝑣𝑥

1

𝑤𝑥2 − 𝑤𝑥

1 0 0

0𝑣𝑦2 − 𝑣𝑦

1

𝑤𝑦2 − 𝑤𝑦

1 0

0 0 1

1 0 −𝑤𝑥1

0 1 −𝑤𝑦1

0 0 1

Transform = 𝑉𝑃𝑇𝐶→𝑊−1 𝑀

Page 10: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

3D Rendering Pipeline (for direct illumination)

3D Model

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

(𝑥, 𝑦, 𝑧)

(𝑥′, 𝑦′) 2D Screen

Page 11: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Transformations

ModelingTransformation

ViewingTransformation

ProjectionTransformation

Lighting

Clipping

ScanConversion

3D Geometric Primitives

Image

𝑁

𝐿1

𝐿2

𝑉

Viewer

𝐼 = 𝐼𝐸 +

𝐿

𝐾𝐴 ⋅ 𝐼𝐿𝐴 + 𝐾𝐷 ⋅ 𝑁, 𝐿 + 𝐾𝑆 ⋅ 𝑉, 𝑅

𝑛 ⋅ 𝐼𝐿

3D Model2D Screen

Page 12: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Lighting

ViewingTransformation

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

3D Camera Coordinates

2D Image Coordinates

Page 13: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Clipping

• Avoid drawing parts of primitives outside window Window defines part of scene being viewed

Must draw geometric primitives only inside window

Screen Coordinates

Window

Page 14: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Clipping

• Avoid drawing parts of primitives outside window Points

Line Segments

Polygons

Screen Coordinates

Window

Page 15: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

• Is point (𝑥, 𝑦) inside the clip window?

Point Clipping

Window

𝑤𝑥1 𝑤𝑥

2

𝑤𝑦2

𝑤𝑦1

(𝑥, 𝑦)

Page 16: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

• Is point (𝑥, 𝑦) inside the clip window?

Point Clipping

Window

(𝑥, 𝑦)

inside = (x >= wx1) &&(x <= wx2) &&(y >= wy1) &&(y <= wy2);

𝑤𝑥1 𝑤𝑥

2

𝑤𝑦2

𝑤𝑦1

Page 17: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Clipping

• Avoid drawing parts of primitives outside window Points

Line Segments

Polygons

Screen Coordinates

Window

Page 18: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Line Segment Clipping

• Find the part of a line inside the clip window Do this as efficiently as possible by identifying the

easiest cases first

𝑃1

𝑃10

𝑃9

𝑃8

𝑃7

𝑃4𝑃3

𝑃6

𝑃5

𝑃2

Page 19: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃1

𝑃10

𝑃9

𝑃8

𝑃7

𝑃4𝑃3

𝑃6

𝑃5

𝑃2

Page 20: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃1

𝑃10

𝑃9

𝑃8

𝑃7

𝑃6

𝑃5

𝑃2

𝑃4𝑃3

Page 21: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃10

𝑃9

𝑃8

𝑃7

𝑃6

𝑃5

𝑃4𝑃3

𝑃1

𝑃2

Page 22: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃10

𝑃9

𝑃8

𝑃7

𝑃6

𝑃5

𝑃4𝑃3

Page 23: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃10

𝑃9

𝑃8

𝑃7

𝑃4𝑃3

𝑃6

𝑃5

Page 24: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃10

𝑃9

𝑃8

𝑃7

𝑃4𝑃3

𝑃6

𝑃5

Page 25: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010𝑃5′

𝑃10

𝑃9

𝑃8

𝑃7

𝑃4𝑃3

𝑃6

Page 26: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010𝑃5′

𝑃10

𝑃9

𝑃4𝑃3

𝑃6

𝑃8

𝑃7

Page 27: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010𝑃5′

𝑃10

𝑃9

𝑃3

𝑃6

𝑃8

𝑃7

𝑃4

Page 28: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃7′

𝑃5′

𝑃10

𝑃9

𝑃3

𝑃6

𝑃8𝑃4

Page 29: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃7′

𝑃5′

𝑃10

𝑃9

𝑃3

𝑃6

𝑃8𝑃4

Page 30: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃8′

𝑃7′

𝑃5′

𝑃10

𝑃9

𝑃3

𝑃6

𝑃4

Page 31: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃8′

𝑃7′

𝑃5′

𝑃3

𝑃6

𝑃4

𝑃10

𝑃9

Page 32: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃8′

𝑃7′

𝑃5′

𝑃3

𝑃6

𝑃4

𝑃10

𝑃9

Page 33: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010𝑃9′

𝑃8′

𝑃7′

𝑃5′

𝑃3

𝑃6

𝑃4

𝑃10

Page 34: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃8′

𝑃7′

𝑃5′

𝑃3

𝑃6

𝑃4

Page 35: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Cohen-Sutherland Line Clipping

• Associate an outcode to each vertex

• If both outcodes are 0, line segment is inside

• If AND of outcodes not 0, line segment is outside

• Otherwise clip and test

Bit 1 Bit 2

Bit 3

Bit 4

0000 01001000

0001 01011001

0010 01101010

𝑃8′

𝑃7′

𝑃5′

𝑃3

𝑃6

𝑃4

How many bits would you need in 3D?

Page 36: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Clipping

• Avoid drawing parts of primitives outside window Points

Line Segments

Polygons

Screen Coordinates

Window

Page 37: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Clipping

• Find the part of a polygon inside the clip window

Before Clipping

Page 38: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Clip to each window boundary one at a time

Page 39: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Clip to each window boundary one at a time

Page 40: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Clip to each window boundary one at a time

Page 41: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Clip to each window boundary one at a time

Page 42: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Clip to each window boundary one at a time

Page 43: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• How do we clip a polygon with respect to a line?

Outside

Inside

Window

Boundary

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 44: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 45: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 46: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 47: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 48: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 49: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 50: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 51: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′′𝑃′

𝑃1𝑃2

𝑃5

𝑃4

𝑃3

Page 52: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′′𝑃′

𝑃1𝑃2

Page 53: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary 𝑃′′𝑃′

𝑃1𝑃2

When polygons are clipped, per-vertex properties

(e.g. lighting) is interpolated to the new vertices.

Page 54: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Sutherland-Hodgeman Clipping

• Do inside test for each point in sequence,

Insert new points when cross window boundary,

Remove points outside window boundary

Outside

Inside

Window

Boundary

What happens if the polygon is not convex?

Page 55: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Lighting

ViewingTransformation

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

3D Camera Coordinates

2D Image Coordinates3D Model

2D Screen

Page 56: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

2D Rendering Pipeline

ScanConversion

Clipping

2D Primitives

Image

Clip portions of geometric primitives

residing outside the window

Fill pixels representing primitives

in screen coordinates

3D Primitives

Page 57: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Overview

• Scan conversion Figure out which pixels to fill

• Shading Determine a color for each filled pixel

• Depth test Determine when the color of a pixel should be

overwritten

Page 58: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Scan Conversion

• Render an image of a geometric primitive

by setting pixel colors

• Example: Filling the inside of a triangle

𝑃1

𝑃2

𝑃3

void SetPixel( int x , int y , Color rgba )

Page 59: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Triangle Scan Conversion

• Properties of a good algorithm Must be fast

No cracks between adjacent primitives

𝑃1

𝑃2

𝑃3

𝑃4

Page 60: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Triangle Scan Conversion

• Properties of a good algorithm Must be fast

No cracks between adjacent primitives

𝑃1

𝑃2

𝑃3

𝑃4

Page 61: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Simple Algorithm

𝑃1

𝑃2

𝑃3

void ScanTriangle( Triangle T , Color rgba ){

for each pixel center (x,y)if( PointInsideTriangle( (x,y) , T ) )

SetPixel( x , y , rgba );}

• Color all pixels inside triangle

Page 62: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

𝑃1

𝑃2

Line defines two halfspaces

• Test: use implicit equation for a line On line: 𝑎𝑥 + 𝑏𝑦 + 𝑐 = 0

To the right: 𝑎𝑥 + 𝑏𝑦 + 𝑐 < 0

To the left: 𝑎𝑥 + 𝑏𝑦 + 𝑐 > 0

𝐿

Page 63: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Inside Triangle Test

• A point is inside a triangle if it is in the

positive half-space of all three boundary lines Triangle vertices are ordered counter-clockwise

Point must be on the left side of every boundary line

𝑃𝐿1

𝐿2

𝐿3

Page 64: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Inside Triangle Test

Boolean PointInsideTriangle( Point P , Triangle T ){

for each boundary line L of T{

Scalar d = L.a*P.x + L.b*P.y + L.c;if( d<0.0 ) return FALSE;

}return TRUE;

}

𝐿1

𝐿2

𝐿3

Page 65: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Simple Algorithm

𝑃1

𝑃2

𝑃3

void ScanTriangle( Triangle T , Color rgba ){

for each pixel center (x,y)if( PointInsideTriangle( (x,y) , T ) )

SetPixel( x , y , rgba );}

• What is bad about this algorithm?

Page 66: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Triangle Sweep-Line Algorithm

• Take advantage of spatial coherence

• Take advantage of edge linearity

𝑑𝑥𝑑𝑦

Page 67: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Triangle Sweep-Line Algorithm

void ScanTriangle( Triangle T , Color rgba ){

for both edge pairs{

initialize xL, xR;compute dxL/dyL and dxR/dyR;for each scanline at y

for( int x=xL ; x<=xR ; x++ ) SetPixel( x , y , rgba );xL += dxL/dyL;xR += dxR/dyR;

}}

𝑥𝐿 𝑥𝑅

𝑑𝑥𝐿𝑑𝑦𝐿

𝑑𝑥𝑅𝑑𝑦𝑅Bresenham’s algorithm

works the same way, but uses only integer operations!

Page 68: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Will this method work for convex polygons?

Page 69: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Will this method work for convex polygons? Yes, since each scan line will only intersect the polygon

at two points.

Page 70: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• How about these polygons?

Page 71: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• How about these polygons?

Page 72: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Fill pixels inside a polygon Triangle

Quadrilateral

Convex

Star-shaped

Concave

Self-intersecting

Holes

What problems do we encounter with arbitrary polygons?

Page 73: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Need better test for points inside polygon Triangle method works only for convex polygons

Convex Polygon

𝐿1

𝐿2

𝐿3

𝐿4𝐿5

𝐿1

𝐿2

𝑳𝟑𝑨

𝐿4𝐿5

Concave Polygon

𝐿3𝐵

Page 74: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Inside Polygon Rule

• What is a good rule for which pixels are inside?

Concave Self-Intersecting With Holes

Page 75: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Inside Polygon Rule

Concave Self-Intersecting With Holes

• Odd-parity rule Any ray from inside 𝑃 to infinity must cross an odd

number of edges

Page 76: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Sweep-Line Algorithm

Use incremental algorithm to find spans

Determine “insideness” with odd parity rule

• Takes advantage of scan line coherence

𝑥𝐿 𝑥𝑅

Triangle Polygon

Page 77: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Sweep-Line Algorithmvoid ScanPolygon( Polygon P , Color rgba ){

sort edges by maxymake empty “active edge list”for each scanline ( top-to-bottom ){

insert/remove edges from “active edge list”update x coordinate of every active edgesort active edges by x coordinatefor each succesive pair of edge-points (left-to-right)

SetPixels( xi , xi+1 , y , rgba );}

}

Page 78: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Convert everything into triangles Scan convert the triangles

Page 79: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Polygon Scan Conversion

• Convert everything into triangles Scan convert the triangles

Note:

OpenGL will render polygons, but it assumes that:

• The polygon is planar

• The polygon is convex

Note:

Even if you only pass in triangles for rendering, OpenGL

may still have to render (convex planar) polygons after

the triangle is clipped.

Page 80: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Scan Conversion

• What about pixels on edges? If we set them either “on” or “off” we get aliasing or

“jaggies”

𝑃1

𝑃2

𝑃3

Page 81: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Scan Conversion

• What about pixels on edges? If we set them either “on” or “off” we get aliasing or

“jaggies”

𝑃1

𝑃2

𝑃3This is like using a “nearest”

interpolation filter!

Page 82: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Antialiasing Techniques

• Display at higher resolution Corresponds to increasing sampling rate

Not always possible (fixed size monitors, fixed refresh

rates, etc.)

• Modify pixel intensities Vary pixel intensities along boundaries for antialiasing

Page 83: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Scan Conversion

• What about pixels on edges? Setting them either “on” or “off” we get aliasing/“jaggies”

Antialias by varying pixel intensities along boundaries

𝑃1

𝑃2

𝑃3

Page 84: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Antialiasing

• Method 1: Area sampling Calculate percent of pixel covered by primitive

Multiply this percentage by desired intensity/color

𝑃1

𝑃2

𝑃3

𝑃1

𝑃2

𝑃3

100%2% 25% 60%

Page 85: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Antialiasing

• Method 1: Area sampling Calculate percent of pixel covered by primitive

Multiply this percentage by desired intensity/color

𝑃1

𝑃2

𝑃3

𝑃1

𝑃2

𝑃3

100%2% 25% 60%

This is like using a “bilinear”

interpolation filter!

Page 86: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Antialiasing

• Method 2: Supersampling (aka postfiltering) Sample as if screen were higher resolution

Average multiple samples to get final intensity

» This can be done by rendering the scene multiple

times with different (fractional) offsets

𝑃1

𝑃2

𝑃3𝑃3

𝑃1

𝑃2

Page 87: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Antialiasing

Note that this makes things harder because pixels

are no longer “owned” by a single triangle. Triangles contribute color rather than set color

Along edges the total contribution must sum to one.

Makes depth-testing more complicated.

𝑃4

𝑃2

𝑃3

𝑃1

Page 88: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Scan Conversion

• Example:

No Anti-Aliasing 4 x Anti-Aliasing

Images courtesy of NVIDIA

Page 89: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Lighting

ViewingTransformation

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

3D Camera Coordinates

2D Image Coordinates3D Model

2D Window

2D Screen

Page 90: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Overview

• Scan conversion Figure out which pixels to fill

• Shading Determine a color for each filled pixel

• Depth test Determine when the color of a pixel comes from the

front-most primitive

Page 91: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

• Recall:

In the “lighting” phase, we calculated

the color at each vertex.

Gouraud Shading

𝑉1 𝐿1

𝑁1

𝑁2

𝑁3𝐿3

𝐿2𝑉3

𝑉2

𝐼1

𝐼2

𝐼3

Lighting

ViewingTransformation

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

Page 92: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

• Recall:

In the “lighting” phase, we calculated

the color at each vertex. In the “scan conversion” phase, linearly

interpolate colors at vertices

Gouraud Shading

𝐼1

𝐼2

𝐼3

𝛼

𝛽

𝛾

𝐵 = (1 − 𝛽) ⋅ 𝐼2 + 𝛽 ⋅ 𝐼3

𝐼 = (1 − 𝛾) ⋅ 𝐴 + 𝛾 ⋅ 𝐵

𝐴 𝐵𝐼

Lighting

ViewingTransformation

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

𝐴 = (1 − 𝛼) ⋅ 𝐼1 + 𝛼 ⋅ 𝐼2

Page 93: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

• Recall:

In the “lighting” phase, we calculated

the color at each vertex. In the “scan conversion” phase, linearly

interpolate colors at vertices

Gouraud Shading

𝐼1

𝐼2

𝐼3

𝛼

𝛽

𝛾

𝐵 = (1 − 𝛽) ⋅ 𝐼2 + 𝛽 ⋅ 𝐼3

𝐼 = (1 − 𝛾) ⋅ 𝐴 + 𝛾 ⋅ 𝐵

𝐴 𝐵

Lighting

ViewingTransformation

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

Note: The values of 𝛼 and 𝛽 only need to be updated as

we move to the next scan-line. The value of 𝛾 needs

to be updated as we advance along the scan-line.

𝐴 = (1 − 𝛼) ⋅ 𝐼1 + 𝛼 ⋅ 𝐼2

𝐼

Page 94: Clipping and Scan Conversionmisha/Fall19/13.pdfCohen-Sutherland Line Clipping • Associate an outcode to each vertex • If both outcodes are 0, line segment is inside • If AND

Lighting

ViewingTransformation

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

3D Camera Coordinates

2D Image Coordinates3D Model

2D Screen