1 Clipping 3 Culling and Clipping • What can’t we see? - anything occluded by another object (HSR) - anything outside view volume • Today: clipping 4 5 6 7 8 9 10 11 Clipping Against a Rectangular Region Multiple Cases B F E C A Clip Rectangle G D H 12 Division of Space ymax Clip Region ymin x min x max 13 Cohen-Sutherland Clipping: “Outcodes” y ymax 1 b0 x x max 0 1 Example : 0 b1 b2 b3 y ymin x xmin 1 b0 0 y ymax y ymax 1 b1 0 y ymin y ymin 14 Cohen-Sutherland Clipping: Region Outcodes 1001 1000 1010 0001 0000 0010 0101 0100 0110 ymax ymin x min x max 15 Cohen-Sutherland Clipping: Trivial Acceptance: O(P0) = O(P1) = 0 1001 1000 ymax 1010 P1 0001 0000 0010 0100 0110 P0 ymin 0101 x min x max 16 Cohen-Sutherland Clipping: Trivial Rejection: O(P0) & O(P1) 0 P0 1001 1000 1010 P1 ymax P0 0001 ymin 0000 0010 P0 0101 x min P1 P1 0100 0110 x max 17 Cohen-Sutherland Clipping: O(P0) =0 , O(P1) 0 1001 1000 1010 ymax P0 0001 0000 P1 0010 P0 P0 ymin 0100 0101 0110 P1 x min x max P1 18 Cohen-Sutherland Clipping: The Algorithm • Compute the outcodes for the two vertices • Test for trivial acceptance or rejection • Select a vertex for which outcode is not zero - There will always be one • Select the first nonzero bit in the outcode to define the boundary against which the line segment will be clipped • Compute the intersection and replace the vertex with the intersection point • Compute the outcode for the new point and iterate 19 Cohen-Sutherland Clipping: Example 1 A 1001 1000 1010 B ymax C 0001 0000 0010 0101 0100 0110 ymin x min x max 20 Cohen-Sutherland Clipping: Example 1 1001 1000 1010 B ymax C 0001 0000 0010 0101 0100 0110 ymin x min x max 21 Chopping at each boundary 22 Cohen-Sutherland Clipping: Example 2 1001 1000 1010 E ymax D 0001 0000 ymin C 0010 B A 0100 0101 x min 0110 x max 23 Cohen-Sutherland Clipping: Example 2 1001 1000 1010 E ymax D 0001 0000 ymin C 0010 B 0100 0101 x min 0110 x max 24 Cohen-Sutherland Clipping: Example 2 1001 1000 1010 ymax D 0001 0000 ymin C 0010 B 0100 0101 x min 0110 x max 25 Cohen-Sutherland Clipping: Example 2 1001 1000 0001 0000 1010 ymax ymin C 0010 B 0100 0101 x min 0110 x max 26 Cohen-Sutherland Clipping: Advantages/Extension • Easily extended to 3 dimensions by adding two bits to the outcode for the z axis. • Calculations then reduce to intersection of line with plane • Algorithm most efficient when most segments can either be trivially accepted or trivially rejected 27 Cohen Sutherland in 3D • Use 6-bit outcodes • When needed, clip line segment against planes 28 Other Clipping Algorithm • Cyrus–Beck, Liang–Barsky - generally, these try to be more clever about intersections - represent lines parametrically, solve for intersection t values P(t) = P0 + t(P1-P0) - can clip in fewer steps than Cohen–Sutherland - try to increase number of trivial rejection cases 29 Parametric Representation of Lines p1 p(1) p 0 p(0) p ( ) p( ) (1 )p 0 p1 30 Liang-Barsky Clipping • Consider the parametric form of a line segment p() = (1-)p1+ p2 1 0 p2 p1 • We can distinguish between the cases by looking at the ordering of the values of where the line determined by the line segment crosses the lines 31 Liang-Barsky Parametric Clipping p( ) p 0 (p1 p 0 ) Edge Ei Outside Inside p Ei p1 N i p( ) p Ei 0 N i p( ) p Ei 0 N i p( ) p Ei 0 p0 Ni 32 Potentially Entering (PE) and Potentially Leaving (PL) Points Edge Ei Outside Edge Ei Inside Inside Outside p1 p1 p0 Ni Potentially Entering (PE) Point Ni p1 p 0 0 p0 Ni Potentially Leaving (PL) Point Ni p1 p0 0 33 Liang-Barsky Clipping: Computing the Intersection N i p( ) p Ei 0 p( ) p 0 (p1 p 0 ) N i p 0 p Ei N i p1 p 0 0 Let D (p1 p 0 ) and solve for : = N i p 0 p Ei Ni D 34 Liang-Barsky Clipping: Potentially Leaving vs. Potentially Entering p1 p1 PE PL ymax p1 PL PL p0 PE PL p0 PE PE ymin p0 x min = “Inside” x max 35 Liang-Barsky Clipping: Algorithm Strategy • Find the largest PE greater than zero. • Find the smallest PL less than one. • Reject the segment if PE > PL. 36 Liang-Barsky Clipping: Pseudocode of Algorithm for (each line segment to be clipped) alpha_E=0; alpha_L=1; for (each candidate intersection with a clip edge) { if (Ni•D!=0) { /*edges not parallel to line*/ calculate alpha; use sign of Ni•D to class alpha as PE or PL; if (PE) alpha_E = max(alpha_E,alpha); if (PL) alpha_L = min(alpha_L,alpha); } } if (alpha_E > alpha_L) return NULL; else return P(alpha_E) and P(alpha_L) as clip intersections 37 Polygon Clipping: Convex Polygons 5 4 5’ 4’ 1 3 1 3 2 2 Line segment clipping is done in order. New vertices are generated at clip point. External vertices are eliminated. 38 Polygon Clipping: The Convexity Problem Single object becomes multiple objects. 39 Sutherland-Hodgeman Polygon Clipping Top Bottom Right Left