400 likes | 799 Views
Clipping. Culling and Clipping. What can’t we see? anything occluded by another object (HSR) anything outside view volume Today: clipping. Clipping Against a Rectangular Region Multiple Cases. B. F. E. C. G. A. D. H. Clip Rectangle. Division of Space. Clip Region. 1. 0. 1. 0.
E N D
Culling and Clipping • What can’t we see? • anything occluded by another object (HSR) • anything outside view volume • Today: clipping
Clipping Against a Rectangular RegionMultiple Cases B F E C G A D H Clip Rectangle
Division of Space Clip Region
1 0 1 0 Cohen-Sutherland Clipping:“Outcodes”
Cohen-Sutherland Clipping:Region Outcodes 1001 1000 1010 0001 0000 0010 0100 0110 0101
Cohen-Sutherland Clipping:Trivial Acceptance: O(P0) = O(P1) = 0 1001 1000 1010 P1 0001 0000 0010 P0 0100 0110 0101
Cohen-Sutherland Clipping:Trivial Rejection: O(P0) & O(P1) 0 P0 1001 1000 1010 P1 P0 0001 0000 0010 P0 P1 P1 0100 0110 0101
Cohen-Sutherland Clipping:O(P0) =0 , O(P1) 0 1001 1000 1010 P0 0001 0000 0010 P1 P1 P0 P0 0100 0110 0101 P1
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
Cohen-Sutherland Clipping:Example 1 A 1001 1000 1010 B C 0001 0000 0010 0100 0110 0101
Cohen-Sutherland Clipping:Example 1 1001 1000 1010 B C 0001 0000 0010 0100 0110 0101
Cohen-Sutherland Clipping:Example 2 1001 1000 1010 E D 0001 0000 0010 C B A 0100 0110 0101
Cohen-Sutherland Clipping:Example 2 1001 1000 1010 E D 0001 0000 0010 C B 0100 0110 0101
Cohen-Sutherland Clipping:Example 2 1001 1000 1010 D 0001 0000 0010 C B 0100 0110 0101
Cohen-Sutherland Clipping:Example 2 1001 1000 1010 0001 0000 0010 C B 0100 0110 0101
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
Cohen Sutherland in 3D • Use 6-bit outcodes • When needed, clip line segment against planes
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
Liang-Barsky Clipping • Consider the parametric form of a line segment • We can distinguish between the cases by looking at the ordering of the values of a where the line determined by the line segment crosses the lines that determine the window p(a) = (1-a)p1+ ap2 1 a 0 p2 p1
Liang-Barsky Parametric Clipping Edge Ei Outside Inside
Potentially Entering (PE) and Potentially Leaving (PL) Points Edge Ei Edge Ei Outside Inside Inside Outside Potentially Entering (PE) Point Potentially Leaving (PL) Point
Liang-Barsky Clipping:Potentially Leaving vs. Potentially Entering PE PL PL PL PE PL PE PE = “Inside”
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.
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
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.
Polygon Clipping:The Convexity Problem Single object becomes multiple objects.
Top Bottom Right Left Sutherland-Hodgeman Polygon Clipping