SlideShare a Scribd company logo
1 of 19
• Discuss clipping
• Points
• Lines
• Polygons
Why Clip?
• Rasterization is very expensive
• Approximately linear with number of
fragments created
• Math and logic per pixel
• If we only rasterize what is actually
viewable, we can save a lot
• A few operations now can save many later
Clipping Primitives
• Different primitives can be handled in
different ways
• Points
• Lines
• Polygons
Point Clipping
• This one is easy
• How to determine if a point (x, y, z) is in
the viewing volume (xnear, ynear, znear),
(xfar, yfar, zfar)?
• Who wants to tell me how?
• if ((x > xfar II x < xnear) ||
(y > yfar II y < ynear) ||
(z > zfar II z < znear))
cull the point
else
keep it
Line Clipping
• What happens when a line passes out
of the viewing volume/plane?
• Part is visible, part is not
• Need to find the entry/exit points, and
shorten the line
• The shortened line is what gets passed to
rasterization
Line Clipping
Example
• Let’s do 2D first
• Clip a line against 1
edge of the viewport
• What do we know?
• Similar triangles
• A / B = C / D
• B = (x2 - x1)
• A = (y2 - y1)
• C = (y1 - ymax)
➡ D = BC / A
➡ (x’, y’) = (x1 - D,
(x2, y2)
(x1, y1)
(x’, y’) ???
A
B
C
D
Line Clipping
• The other cases are handled similarly
• The algorithm extends easily to 3D
• The problem?
• Too expensive! (these numbers are for 2D)
• 3 floating point subtracts
• 2 floating point multiplies
• 1 floating point divide
• 4 times! (once for each edge)
• We need to do better
Cohen-Sutherland
Line Clipping
• Split plane into 9
regions
• Assign each a 4-bit tag
• (above, below, right, left)
• Assign each endpoint
a tag
1001 1000 1010
0001 0000 0010
0101 0100 0100
Viewport
Cohen-Sutherland
Line Clipping
• Algorithm:
1. if (tag1 == tag2 == 0000)
accept the line
2. if ((tag1 & tag2) != 0)
reject the line
3. Clip the line against an
edge (where both bits
are nonzero)
4. Assign the new vertex a
4-bit value
5. Return to 1
1001 1000 1010
0001 0000 0010
0101 0100 0110
Viewport
N.B.: & is the bitwise
AND operator
Cohen-Sutherland
Example
• What are the vertex codes for these
lines?
Polygon
Inside/Outside
• Polygons have a distinct inside and
outside
• How do you tell, just from a list of
vertices/edges?
• Even/odd
• Winding number
Polygon
Inside/Outside:
Even / Odd
• Count edge crossings
• If the number is even, that area is outside
• If odd, it is inside
0
2
2
1
0 1
1
1
2
2
2
3
3
4
Polygon
Inside/Outside:
Winding Number
• Each line segment is assigned a
direction by walking around the edges
in some pre-defined order
• OpenGL walks counter-clockwise
• Count right->left edge crossings and
left->right edge crossings
• If equal, the point is outside
Polygon Clipping
• Polygons are just composed of lines.
Why do we need to treat them
differently?
• Need to keep track of what is inside
Lines Polygons
NOTE:
Polygon Clipping
• Many tricky bits
• Maintaining inside/outside
• Introduces variable number of vertices
• Need to handle screen corners correctly
Sutherland-
Hodgeman Polygon
Clipping
• Simplify via separation
• Clip the entire polygon with one edge
• Clip the output polygon against the next
edge
• Repeat for all edges
• Extends easily to 3D (6 edges instead
of 4)
• Can create intermediate vertices that
get thrown out later
Sutherland-
Hodgeman Polygon
Clipping
• Example 1:
Out -> In
Save new clip vertex
and ending vertex
In -> In
Save ending vertex
In -> Out
Save new clip vertex
Out -> Out
Save nothing
Sutherland-
Hodgeman
Polygon Clipping
• Example 2:
Start
Clip
Left
Clip
Right
Clip
Bottom
Clip
Top
NOTE:
Done with Clipping
• Point Clipping
• Easy, just do inequalities
• Line Clipping
• Cohen-Sutherland
• Polygon Clipping
• Sutherland-Hodgeman

More Related Content

What's hot (20)

Clipping ( Cohen-Sutherland Algorithm )
Clipping ( Cohen-Sutherland Algorithm )Clipping ( Cohen-Sutherland Algorithm )
Clipping ( Cohen-Sutherland Algorithm )
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
 
Curve clipping
Curve clippingCurve clipping
Curve clipping
 
Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping AlgorithmCohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping Algorithm
 
Lecture filling algorithms
Lecture  filling algorithmsLecture  filling algorithms
Lecture filling algorithms
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
 
line clipping
line clipping line clipping
line clipping
 
Clipping in Computer Graphics
Clipping in Computer GraphicsClipping in Computer Graphics
Clipping in Computer Graphics
 
Clipping
ClippingClipping
Clipping
 
Polygon clipping
Polygon clippingPolygon clipping
Polygon clipping
 
The sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithmThe sutherland hodgeman polygon clipping algorithm
The sutherland hodgeman polygon clipping algorithm
 
Clipping
ClippingClipping
Clipping
 
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clipping
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Sutherland hodgman polygon clipping algorithm
Sutherland hodgman polygon clipping algorithmSutherland hodgman polygon clipping algorithm
Sutherland hodgman polygon clipping algorithm
 
Implementation
ImplementationImplementation
Implementation
 
Cohen sutherland algorithm
Cohen sutherland algorithmCohen sutherland algorithm
Cohen sutherland algorithm
 
Bhavesh window clipping slidshare
Bhavesh window clipping slidshareBhavesh window clipping slidshare
Bhavesh window clipping slidshare
 
lecture8 clipping
lecture8 clippinglecture8 clipping
lecture8 clipping
 
Boundary fill algm
Boundary fill algmBoundary fill algm
Boundary fill algm
 

Similar to Clipping (20)

99995327.ppt
99995327.ppt99995327.ppt
99995327.ppt
 
Clipping & Rasterization
Clipping & RasterizationClipping & Rasterization
Clipping & Rasterization
 
Clipping.ppt
Clipping.pptClipping.ppt
Clipping.ppt
 
Decppt
DecpptDecppt
Decppt
 
7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf7-Clipping-16 (1).pdf
7-Clipping-16 (1).pdf
 
Polygon Fill
Polygon FillPolygon Fill
Polygon Fill
 
UNIVERSAL PROPERTY.pptx
UNIVERSAL PROPERTY.pptxUNIVERSAL PROPERTY.pptx
UNIVERSAL PROPERTY.pptx
 
ch4.pptx
ch4.pptxch4.pptx
ch4.pptx
 
Clipping
ClippingClipping
Clipping
 
Clipping 22
Clipping 22Clipping 22
Clipping 22
 
02-gates-w.pptx
02-gates-w.pptx02-gates-w.pptx
02-gates-w.pptx
 
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
Convex Partitioning of a Polygon into Smaller Number of Pieces with Lowest Me...
 
Digital Electronics Unit_1.pptx
Digital Electronics Unit_1.pptxDigital Electronics Unit_1.pptx
Digital Electronics Unit_1.pptx
 
Dpsd lecture-notes
Dpsd lecture-notesDpsd lecture-notes
Dpsd lecture-notes
 
Representation image
Representation imageRepresentation image
Representation image
 
10CSL67 CG LAB PROGRAM 5
10CSL67 CG LAB PROGRAM 510CSL67 CG LAB PROGRAM 5
10CSL67 CG LAB PROGRAM 5
 
october9.ppt
october9.pptoctober9.ppt
october9.ppt
 
Switching theory unit 2
Switching theory unit 2Switching theory unit 2
Switching theory unit 2
 
Counit2
Counit2Counit2
Counit2
 
Lec-4_Rendering-1.pdf
Lec-4_Rendering-1.pdfLec-4_Rendering-1.pdf
Lec-4_Rendering-1.pdf
 

More from Ruchika Sinha

Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptRuchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptRuchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.pptRuchika Sinha
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.pptRuchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptRuchika Sinha
 
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboardRuchika Sinha
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithmRuchika Sinha
 
Optimization problems
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
 
Backtrack search-algorithm
Backtrack search-algorithmBacktrack search-algorithm
Backtrack search-algorithmRuchika Sinha
 
Software for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentationSoftware for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentationRuchika Sinha
 

More from Ruchika Sinha (20)

Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
 
Lec22 intel
Lec22 intelLec22 intel
Lec22 intel
 
Pc assembly
Pc assemblyPc assembly
Pc assembly
 
Casing
CasingCasing
Casing
 
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboard
 
Shortest path
Shortest pathShortest path
Shortest path
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
Python material
Python materialPython material
Python material
 
Python3
Python3Python3
Python3
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
 
Software Teting
Software TetingSoftware Teting
Software Teting
 
Backtrack search-algorithm
Backtrack search-algorithmBacktrack search-algorithm
Backtrack search-algorithm
 
XML
XMLXML
XML
 
Software for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentationSoftware for encrypting and decrypting text file powerpointpresentation
Software for encrypting and decrypting text file powerpointpresentation
 

Recently uploaded

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 

Recently uploaded (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 

Clipping

  • 1. • Discuss clipping • Points • Lines • Polygons
  • 2. Why Clip? • Rasterization is very expensive • Approximately linear with number of fragments created • Math and logic per pixel • If we only rasterize what is actually viewable, we can save a lot • A few operations now can save many later
  • 3. Clipping Primitives • Different primitives can be handled in different ways • Points • Lines • Polygons
  • 4. Point Clipping • This one is easy • How to determine if a point (x, y, z) is in the viewing volume (xnear, ynear, znear), (xfar, yfar, zfar)? • Who wants to tell me how? • if ((x > xfar II x < xnear) || (y > yfar II y < ynear) || (z > zfar II z < znear)) cull the point else keep it
  • 5. Line Clipping • What happens when a line passes out of the viewing volume/plane? • Part is visible, part is not • Need to find the entry/exit points, and shorten the line • The shortened line is what gets passed to rasterization
  • 6. Line Clipping Example • Let’s do 2D first • Clip a line against 1 edge of the viewport • What do we know? • Similar triangles • A / B = C / D • B = (x2 - x1) • A = (y2 - y1) • C = (y1 - ymax) ➡ D = BC / A ➡ (x’, y’) = (x1 - D, (x2, y2) (x1, y1) (x’, y’) ??? A B C D
  • 7. Line Clipping • The other cases are handled similarly • The algorithm extends easily to 3D • The problem? • Too expensive! (these numbers are for 2D) • 3 floating point subtracts • 2 floating point multiplies • 1 floating point divide • 4 times! (once for each edge) • We need to do better
  • 8. Cohen-Sutherland Line Clipping • Split plane into 9 regions • Assign each a 4-bit tag • (above, below, right, left) • Assign each endpoint a tag 1001 1000 1010 0001 0000 0010 0101 0100 0100 Viewport
  • 9. Cohen-Sutherland Line Clipping • Algorithm: 1. if (tag1 == tag2 == 0000) accept the line 2. if ((tag1 & tag2) != 0) reject the line 3. Clip the line against an edge (where both bits are nonzero) 4. Assign the new vertex a 4-bit value 5. Return to 1 1001 1000 1010 0001 0000 0010 0101 0100 0110 Viewport N.B.: & is the bitwise AND operator
  • 10. Cohen-Sutherland Example • What are the vertex codes for these lines?
  • 11. Polygon Inside/Outside • Polygons have a distinct inside and outside • How do you tell, just from a list of vertices/edges? • Even/odd • Winding number
  • 12. Polygon Inside/Outside: Even / Odd • Count edge crossings • If the number is even, that area is outside • If odd, it is inside 0 2 2 1 0 1 1 1 2 2 2 3 3 4
  • 13. Polygon Inside/Outside: Winding Number • Each line segment is assigned a direction by walking around the edges in some pre-defined order • OpenGL walks counter-clockwise • Count right->left edge crossings and left->right edge crossings • If equal, the point is outside
  • 14. Polygon Clipping • Polygons are just composed of lines. Why do we need to treat them differently? • Need to keep track of what is inside Lines Polygons NOTE:
  • 15. Polygon Clipping • Many tricky bits • Maintaining inside/outside • Introduces variable number of vertices • Need to handle screen corners correctly
  • 16. Sutherland- Hodgeman Polygon Clipping • Simplify via separation • Clip the entire polygon with one edge • Clip the output polygon against the next edge • Repeat for all edges • Extends easily to 3D (6 edges instead of 4) • Can create intermediate vertices that get thrown out later
  • 17. Sutherland- Hodgeman Polygon Clipping • Example 1: Out -> In Save new clip vertex and ending vertex In -> In Save ending vertex In -> Out Save new clip vertex Out -> Out Save nothing
  • 18. Sutherland- Hodgeman Polygon Clipping • Example 2: Start Clip Left Clip Right Clip Bottom Clip Top NOTE:
  • 19. Done with Clipping • Point Clipping • Easy, just do inequalities • Line Clipping • Cohen-Sutherland • Polygon Clipping • Sutherland-Hodgeman