Download Presentation
## Clipping

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Clipping**Pradondet Nilagupta 204481 Foundation of Computer Graphics**Learning outcomes for this lecture**• You will • understand • What is meant by ‘clipping’ • 2D clipping concepts • 3D clipping concepts • Different kinds of clipping • The Cohen-Sutherland 2D region-coding clipping technique • Be able to • calculate Cohen-Sutherland 2D region-coding 204481 Foundation of Computer Graphics**Clipping**• Clipping means • Identifyingportions of a scene that are inside (or outside) a specified region • Examples • Multiple viewports on a device • Deciding how much of a games world the player can see 204481 Foundation of Computer Graphics**Clipping**• Clipping means • Identifyingportions of a scene that are inside (or outside) a specified region • Examples • Multiple viewports on a device • Deciding how much of a games world the player can see Player can’t see this far yet 204481 Foundation of Computer Graphics**Requirements for clipping**• Is (x, y) inside or outside a given region • For 2D graphics the region defining what is to be clipped is called • The clip window Clip window 204481 Foundation of Computer Graphics**Interior and exterior clipping**• interior clipping • what is to be saved is inside the clip window • exterior clipping • what is to be saved is outside clip window Interior clipping- keep point P2 P2(x2, y2) 204481 Foundation of Computer Graphics**Interior and exterior clipping**• We shall assume interior clipping for now • But you must be aware of exterior clipping too Exterior clipping- keep point P1 Clip window P1(x1, y1) 204481 Foundation of Computer Graphics**Overview of types of clipping**• All-or-none clipping • If any part of object outside clip windowwhole object is rejected • Point clipping • Only keep points inside clip window • Line clipping • Only keep segment of line inside clip window • Polygon clipping • Only keep sub-polygons inside clip window 204481 Foundation of Computer Graphics**Before and after POINT clipping**• Before • After 204481 Foundation of Computer Graphics**Before and after LINE clipping**• Before • After 204481 Foundation of Computer Graphics**Before and after POLYGON clipping**• Before • After 204481 Foundation of Computer Graphics**Cohen-Sutherland 2D clipping**• 4 regions are defined – outside the clip window • TOP • BOTTOM • LEFT • RIGHT 204481 Foundation of Computer Graphics**The 4-bit codes**• A 4-bit code is assigned to each point • This code is sometimes called a Cohen-Sutherland region code • LEFT bit 1 = binary 0001 • RIGHT bit 2 = binary 0010 • BOTTOM bit 3 = binary 0100 • TOP bit 4 = binary 1000 • So a point coded 0001 is LEFT of the clip window, etc. 204481 Foundation of Computer Graphics**Cohen-Sutherland region codes**• The point (65, 50) is above and to the right of the clip window, • Therefore gets the code: 1010 204481 Foundation of Computer Graphics**Cohen-Sutherland region codes**• 1000 indicates the TOP region • 0010 indicates the RIGHT region • the 4 bit Cohen-Sutherland codeis formed by a logical OR of all region codes • 1000 TOP • OR 0010 RIGHT • ------------------ • = 1010 204481 Foundation of Computer Graphics**How the codes are useful**• Why bother encoding points with 4-bit Cohen-Sutherland region codes? • Can make some quick decisions • If code is zero (0000) point is inside window • If code is non-zero point is outside window • For the two endpoints of a line • If codes for both endpoints are zero,whole line is inside window • If (logical) AND of codes for endpoints is non-zero,the endpoints must have a region in common …So the whole line must be outside clip window 204481 Foundation of Computer Graphics**Some lines and their endpoint codes**TOP BOTTOM RIGHT LEFT • Line1 codes are 1001 and 0000 • Line2 codes are 0000 and 0000 << inside • Line3 codes are 1000 and 0100 • Line4 codes are 1010 and 0110 << outside 204481 Foundation of Computer Graphics**Clipping from region codes**• To clip a line based on region codes: • Choose an endpoint that is OUTSIDE clip window (I.e. non-zero code) • Check first bit (TOP) – if set, clip intersection from point to TOP of clip window • Check second bit (BOTTOM) and so on 204481 Foundation of Computer Graphics**Line Clipping Algorithms**• Goal: avoid drawing primitives that are outside the viewing window. • The most common case: clipping line segments against a rectangular window 204481 Foundation of Computer Graphics**Line Clipping Algorithms**• Three cases: • Segment is entirely inside the window - Accept • Segment is entirely outside the window - Reject • Segment intersects the boundary - Clip 204481 Foundation of Computer Graphics**ymax**ymin xmin xmax Point Classification • How can we tell if a point is inside a rectangular window? 204481 Foundation of Computer Graphics**Line Segment Clipping**• If both endpoints are inside the window, the entire segment is inside: trivial accept • If one endpoint is inside, and the other is outside, line segment must be split. • What happens when both endpoints are outside? 204481 Foundation of Computer Graphics**1001**1000 1010 ymax 0001 0000 0010 ymin 0101 0100 0110 xmin xmax Cohen-Sutherland Algorithm • Assign a 4-digit binary outcode to each of the 9 regions defined by the window: 204481 Foundation of Computer Graphics**bit**1 2 3 4 condition y > ymax y < ymin x > xmax x < xmin 1001 1000 1010 ymax 0001 0000 0010 ymin 0101 0100 0110 xmin xmax Cohen-Sutherland Algorithm 204481 Foundation of Computer Graphics**Cohen-Sutherland Algorithm**• Compute the outcodes C1 and C2 corresponding to both segment endpoints. • If ((C1 | C2) == 0): Trivial Accept • If ((C1 & C2) != 0): Trivial Reject • Otherwise, split segment into two parts. Reject one part, and repeat the procedure on the remaining part. • What edge should be intersected ? 204481 Foundation of Computer Graphics**D**C ymax B A ymin xmin xmax Example Outcode(A) = 0000 Outcode(D) = 1001 No trivial accept/reject Clip (A,D) with y = ymax, splitting it into (A,B) and (B,D) Reject (B,D) Proceed with (A,B) 204481 Foundation of Computer Graphics**ymax**ymin xmin xmax Example E D C Outcode(A) = 0100 Outcode(E) = 1010 B A No trivial accept/reject Clip (A,E) with y = ymax, splitting it into (A,D) and (D,E) Reject (D,E) Proceed with (A,D) 204481 Foundation of Computer Graphics**ymax**ymin xmin xmax Example D C Outcode(A) = 0100 Outcode(D) = 0010 B A No trivial accept/reject Clip (A,D) with y = ymin, splitting it into (A,B) and (B,D) Reject (A,B) Proceed with (B,D) 204481 Foundation of Computer Graphics**ymax**ymin xmin xmax Example D C Outcode(B) = 0000 Outcode(D) = 0010 B No trivial accept/reject Clip (B,D) with x = xmax, splitting it into (B,C) and (C,D) Reject (C,D) Proceed with (B,C) 204481 Foundation of Computer Graphics**Clipping a line**• Choose point P2 (code is 1000) • Clip from point to TOP of clip window 204481 Foundation of Computer Graphics**Clipping a line**• Choose point P1 (code is 0110) • Bit 1 (TOP) not set • Bit 2 (BOTTOM) set – so clip bottom 204481 Foundation of Computer Graphics**Clipping a line**• Bit 3 (RIGHT) set – so clip right • Bit 4 (LEFT) not set – so clipping finished 204481 Foundation of Computer Graphics**How to clip a line**• Need to know • End points of line(x1, y1) – (x2, y2) • X or Y value definingclip window edge • (similar triangles and line gradient) • m = (y2 – y1) / (x2 – x1); // (gradient) • newx = xwmin; • newy = y1 + m*(xwmin – x1) xmin (x2, y2) (x1, y1) (newx, newy) 204481 Foundation of Computer Graphics**Clipping Lines**• A line is completely visible if both of its end points are in the window. • Brute Force Method - Solve simultaneous equations for intersections of lines with window edges. (xl, yt) (xr, yt) A point is visible if xl < x < xr and yb < y < yt (x, y) (xl, yb) (xr, yb) 204481 Foundation of Computer Graphics**Cohen-Sutherland Clipping**• Region Checks: Trivially reject or accept lines and points. • Fast for large windows (everything is inside) and for small windows (everything is outside). • Each vertex is assigned a four-bit outcode. 204481 Foundation of Computer Graphics**1001**1000 1010 0001 0000 0010 0101 0100 0110 Cohen-Sutherland Clipping (cont.) Bit 1: Above Bit 2: Below Bit 3: Right Bit 4: Left • Bit 1: 1 if y > yt, else 0 • Bit 2: 1 if y < yb, else 0 • Bit 3: 1 if x > xr, else 0 • Bit 4: 1 if x < xl, else 0 204481 Foundation of Computer Graphics**1001**1000 1010 0001 0000 0010 0101 0100 0110 Cohen-Sutherland Clipping (cont.) • A line can be trivially accepted if both endpoints have an outcode of 0000. • A line can be trivially rejected if any corresponding bits in the two outcodes are both equal to 1. (This means that both endpoints are to the right, to the left, above, or below the window.) • if (outcode 1 & outcode 2) != 0000, trivially reject! Bit 1: Above Bit 2: Below Bit 3: Right Bit 4: Left 204481 Foundation of Computer Graphics**Clipping Lines Not Accepted or Rejected**• In the case where a line can be neither trivially accepted nor rejected, the algorithm uses a “divide and conquer” method. Line AD: 1) Test outcodes of A and D --> can’t accept or reject. 2) Calculate intersection point B, which is on the dividing line between the window and the “above” region. Form new line segment AB and discard BD because above the window. 3) Test outcodes of A and B. Reject. Line EH: ?? D C B A H E F G 204481 Foundation of Computer Graphics**Polygon Clipping**• Polygons can be clipped against each edge of the window one edge at a time. Window/edge intersections, if any, are easy to find since the X or Y coordinates are already known. • Vertices which are kept after clipping against one window edge are saved for clipping against the remaining edges. Note that the number of vertices usually changes and will often increase. 204481 Foundation of Computer Graphics**P4**P4 I2 I2 P3 I1 I1 P1 P2 P1 Polygon Clipping Algorithm • The window boundary determines a visible and invisible region. • The edge from vertex i to vertex i+1 can be one of four types: • Exit visible region - save the intersection • Wholly outside visible region- save nothing • Enter visible region - save intersection and endpoint • Wholly inside visible region - save endpoint 204481 Foundation of Computer Graphics**Polygon clipping issues**• The final output, if any, is always considered a single polygon. • The spurious edge may not be a problem since it always occurs on a window boundary, but it can be eliminated if necessary. 204481 Foundation of Computer Graphics**Pipelined Polygon Clipping**• Because polygon clipping does not depend on any other polygons, it is possible to arrange the clipping stages in a pipeline. the input polygon is clipped against one edge and any points that are kept are passed on as input to the next stage of the pipeline. • This way four polygons can be at different stages of the clipping process simultaneously. This is often implemented in hardware. Clip Top Clip Right Clip Bottom Clip Left 204481 Foundation of Computer Graphics**Clipping in the openGL pipeline**• A figure illustrating the openGL graphics pipeline: clipping is done here 204481 Foundation of Computer Graphics**Text clipping**• strategies for dealing with text objects • Consider following example: • Assume interiorclipping 204481 Foundation of Computer Graphics**Student Exercise**• Draw three possible results of interior clipping of the string “ABC” • Try to name what typeof clipping you have done in each case 204481 Foundation of Computer Graphics**Student Exercise**• All-or-none string clipping • No text is retained after clippingsince part of text is outside clip window • Whole String“ABC” is clipped as a single object 204481 Foundation of Computer Graphics**Student Exercise**• All-or-none character clipping • Only “B” and “C” are retained after clippingsince part of “A” is outside clip window • Each character is clipped as a separate object 204481 Foundation of Computer Graphics**Student Exercise**• Character component clipping • All of “B” and “C” are retained, and those parts of “A” inside clip window are also retained • Each component of each character is clipped as a separate object 204481 Foundation of Computer Graphics**Introduction to 3D clipping**• In 3D a clip volume needs to be defined • Example for “perspective” 3D • We define • Point to project to(viewer’s “eye”) 204481 Foundation of Computer Graphics**Introduction to 3D clipping**• In 3D a clip volume needs to be defined • Example for “perspective” 3D • We define • Window to define directionand amount of view 204481 Foundation of Computer Graphics