clipping and scan conversionmisha/fall20/13.pdfcohen-sutherland line clipping β€’ associate a 4-bit...

96
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

Upload: others

Post on 05-Mar-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Announcementsβ€’ Assignment 2 posted

Start yesterday!!!

Page 3: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

(π‘₯π‘₯,𝑦𝑦, 𝑧𝑧)

(π‘₯π‘₯β€²,𝑦𝑦′)

3D WorldCoordinates

3D ObjectCoordinates

Page 5: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

3D WorldCoordinates

Transform = 𝑀𝑀𝑀𝑀 = local to world transform

(π‘₯π‘₯,𝑦𝑦, 𝑧𝑧)

(π‘₯π‘₯β€²,𝑦𝑦′)

Page 6: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Transformations

ModelingTransformation

ViewingTransformation

2D Image Coordinates

ProjectionTransformation

Window-to-ViewportTransformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

3D WorldCoordinates

3D CameraCoordinates

(π‘₯π‘₯,𝑦𝑦, 𝑧𝑧)

(π‘₯π‘₯β€²,𝑦𝑦′)

Transform = 𝑀𝑀𝑀𝑀 = local to world transform

Page 7: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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

𝑀𝑀𝑦𝑦2Window Viewport

Screen Coordinates Image Coordinates

(𝑀𝑀π‘₯π‘₯,𝑀𝑀𝑦𝑦) (𝑣𝑣π‘₯π‘₯ , 𝑣𝑣𝑦𝑦)

(π‘₯π‘₯,𝑦𝑦, 𝑧𝑧)

(π‘₯π‘₯β€²,𝑦𝑦′)

𝑉𝑉 = viewport transform

𝑉𝑉 =1 0 𝑣𝑣π‘₯π‘₯1

0 1 𝑣𝑣π‘₯π‘₯20 0 1

𝑣𝑣π‘₯π‘₯2 βˆ’ 𝑣𝑣π‘₯π‘₯1

𝑀𝑀π‘₯π‘₯2 βˆ’ 𝑀𝑀π‘₯π‘₯10 0

0𝑣𝑣𝑦𝑦2 βˆ’ 𝑣𝑣𝑦𝑦1

𝑀𝑀𝑦𝑦2 βˆ’ 𝑀𝑀𝑦𝑦10

0 0 1

1 0 βˆ’π‘€π‘€π‘₯π‘₯1

0 1 βˆ’π‘€π‘€π‘¦π‘¦1

0 0 1

Transform = π‘‰π‘‰π‘ƒπ‘ƒπ‘‡π‘‡πΆπΆβ†’π‘Šπ‘Šβˆ’1 𝑀𝑀

Page 10: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Transformations𝑁𝑁

𝐿𝐿1

𝐿𝐿2

𝑉𝑉

Viewer

𝐼𝐼 = 𝐼𝐼𝐸𝐸 + �𝐿𝐿

𝐾𝐾𝐴𝐴 β‹… 𝐼𝐼𝐿𝐿𝐴𝐴 + 𝐾𝐾𝐷𝐷 β‹… 𝑁𝑁, 𝐿𝐿 + 𝐾𝐾𝑆𝑆 β‹… 𝑉𝑉,𝑅𝑅 𝑛𝑛 β‹… 𝐼𝐼𝐿𝐿

3D Model2D Screen

Lighting

ViewingTransformation

3D Primitives

ModelingTransformation

ProjectionTransformation

Clipping

Image

ViewportTransformation

ScanConversion

Page 12: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Clippingβ€’ Avoid drawing parts of primitives outside window

Points Line Segments Polygons

Screen Coordinates

Window

Page 15: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

β€’ Is point (π‘₯π‘₯,𝑦𝑦) inside the clip window?

Point Clipping

Window𝑀𝑀π‘₯π‘₯1 𝑀𝑀π‘₯π‘₯2

𝑀𝑀𝑦𝑦2

𝑀𝑀𝑦𝑦1

(π‘₯π‘₯,𝑦𝑦)

Page 16: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

β€’ 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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Clippingβ€’ Avoid drawing parts of primitives outside window

Points Line Segments Polygons

Screen Coordinates

Window

Page 18: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode π’ƒπ’ƒπŸŽπŸŽπ’ƒπ’ƒπŸπŸπ’ƒπ’ƒπŸπŸπ’ƒπ’ƒπŸ‘πŸ‘ to each vertex

𝑏𝑏0 = 1 if the vertex is left of the window 𝑏𝑏1 = 1 if the vertex is right of the window 𝑏𝑏2 = 1 if the vertex is below the window 𝑏𝑏3 = 1 if the vertex is above the window

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

0000 01001000

0001 01011001

0010 01101010

Page 20: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃1

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃6

𝑃𝑃5

𝑃𝑃2

𝑃𝑃4𝑃𝑃3

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 21: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃6

𝑃𝑃5

𝑃𝑃4𝑃𝑃3

𝑃𝑃1

𝑃𝑃2

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 22: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃6

𝑃𝑃5

𝑃𝑃4𝑃𝑃3

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 23: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃4𝑃𝑃3

𝑃𝑃6

𝑃𝑃5

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 24: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃4𝑃𝑃3

𝑃𝑃6

𝑃𝑃5

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 25: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃8

𝑃𝑃7

𝑃𝑃4𝑃𝑃3

𝑃𝑃6

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 26: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃4𝑃𝑃3

𝑃𝑃6

𝑃𝑃8

𝑃𝑃7

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 27: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃3

𝑃𝑃6

𝑃𝑃8

𝑃𝑃7

𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 28: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃3

𝑃𝑃6

𝑃𝑃8𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 29: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃3

𝑃𝑃6

𝑃𝑃8𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 30: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃10

𝑃𝑃9

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 31: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑃𝑃10

𝑃𝑃9

Page 32: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑃𝑃10

𝑃𝑃9

Page 33: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010𝑃𝑃9β€²

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑃𝑃10

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 34: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes is not 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

Page 35: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Cohen-Sutherland Line Clippingβ€’ Associate a 4-bit outcode 𝑏𝑏0𝑏𝑏1𝑏𝑏2𝑏𝑏3 to each vertexβ€’ If both outcodes are 0, line segment is insideβ€’ If AND of outcodes isnot 0, line segment is outsideβ€’ Otherwise clip and test

0000 01001000

0001 01011001

0010 01101010

𝑃𝑃8β€²

𝑃𝑃7β€²

𝑃𝑃5β€²

𝑃𝑃3

𝑃𝑃6

𝑃𝑃4

𝑏𝑏0 𝑏𝑏1

𝑏𝑏2

𝑏𝑏3

How many bits would you need in 3D?

Page 36: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Clippingβ€’ Avoid drawing parts of primitives outside window

Points Line Segments Polygons

Screen Coordinates

Window

Page 37: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Clippingβ€’ Find the part of a polygon inside the clip window

Before Clipping

Page 38: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Clip to each window boundary, one at a time

Page 39: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Clip to each window boundary, one at a time

Page 40: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Clip to each window boundary, one at a time

Page 41: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Clip to each window boundary, one at a time

Page 42: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Clip to each window boundary, one at a time

Page 43: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ How do we clip a convex polygon with respect to

a line?

OutsideInside

WindowBoundary

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 44: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 45: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 46: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 47: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 48: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 49: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 50: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 51: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃𝑃𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

𝑃𝑃5

𝑃𝑃4

𝑃𝑃3

Page 52: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃𝑃𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

Page 53: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary 𝑃𝑃𝑃𝑃𝑃𝑃𝑃

𝑃𝑃1𝑃𝑃2

When polygons are clipped, per-vertex properties (e.g. lighting) is interpolated to the new vertices.

Page 54: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Sutherland-Hodgeman Clippingβ€’ Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

OutsideInside

WindowBoundary

What happens if the polygon is not convex?

Page 55: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

2D Rendering Pipeline

ScanConversion

Clipping

2D Primitives

Image

Clip portions of geometric primitivesresiding outside the window

Fill pixels representing primitives in screen coordinates

3D Primitives

Page 57: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

𝑃𝑃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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Inside Triangle TestBoolean 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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Triangle Sweep-Line Algorithmβ€’ Take advantage of spatial coherence

β€’ Take advantage of edge linearity

𝑑𝑑π‘₯π‘₯𝑑𝑑𝑦𝑦

Page 67: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Triangle Sweep-Line Algorithmvoid ScanTriangle( Triangle T , Color rgba ){

for both edge pairs{

initialize xL, xR, y;compute dxL/dyL and dxR/dyR;until y reaches the first end-point

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

}} 𝑑𝑑π‘₯π‘₯𝐿𝐿

𝑑𝑑𝑦𝑦𝐿𝐿𝑑𝑑π‘₯π‘₯𝑅𝑅

𝑑𝑑𝑦𝑦𝑅𝑅

π‘₯π‘₯𝐿𝐿 π‘₯π‘₯𝑅𝑅

Page 68: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Triangle Sweep-Line Algorithmvoid ScanTriangle( Triangle T , Color rgba ){

for both edge pairs{

initialize xL, xR, y;compute dxL/dyL and dxR/dyR;until y reaches the first end-point

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

}}

π‘₯π‘₯𝐿𝐿 π‘₯π‘₯𝑅𝑅𝑑𝑑π‘₯π‘₯𝐿𝐿

𝑑𝑑𝑦𝑦𝐿𝐿

𝑑𝑑π‘₯π‘₯𝑅𝑅𝑑𝑑𝑦𝑦𝑅𝑅

Page 69: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Triangle Sweep-Line Algorithmvoid ScanTriangle( Triangle T , Color rgba ){

for both edge pairs{

initialize xL, xR, y;compute dxL/dyL and dxR/dyR;until y reaches the first end-point

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

}}

Bresenham’s algorithmworks the same way, but uses only integer operations!

Page 70: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Scan Conversionβ€’ Will this method work for convex polygons?

Page 71: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Scan Conversionβ€’ Will this method work for convex polygons?

Yes, since each scan line will only intersect the polygon at two points.

Page 72: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Scan Conversionβ€’ How about these polygons?

Page 73: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Scan Conversionβ€’ How about these polygons?

Page 74: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 75: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 76: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Inside Polygon Rule

β€’ What is a good rule for which pixels are inside?

Concave Self-Intersecting With Holes

Page 77: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 78: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 79: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 80: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Polygon Scan Conversionβ€’ Convert everything into triangles

Scan convert the triangles

Page 81: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 82: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Scan Conversionβ€’ What about pixels on edges?

If we set them either β€œon” or β€œoff” we get aliasing or β€œjaggies”

𝑃𝑃1

𝑃𝑃2

𝑃𝑃3

Page 83: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 84: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 85: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 86: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 87: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 88: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 89: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

AntialiasingNote 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 90: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

Scan Conversionβ€’ Example:

No Anti-Aliasing 4 x Anti-AliasingImages courtesy of NVIDIA

Page 91: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 92: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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 93: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

β€’ 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 94: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

β€’ 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 95: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

β€’ 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 96: Clipping and Scan Conversionmisha/Fall20/13.pdfCohen-Sutherland Line Clipping β€’ Associate a 4-bit outcode 𝒃𝒃 𝟎𝟎 𝒃𝒃 𝟏𝟏 𝒃𝒃 𝟐𝟐 𝒃𝒃 πŸ‘πŸ‘

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