Graphics Graphics & Graphical Programming Lecture 23 - Viewing & Clipping
Graphics2 Viewing & Clipping - Outline Viewing in 2D Clipping in 2D Cohen-Sutherland Algorithm Cyrus & Beck Algorithm Clipping Polygons Sutherland-Hodgman Algorithm
Graphics3 Viewing in 2 Dimensions In 2D, a ‘world’ consists of an infinite plane, defined in ‘world’ coordinates, i.e metres, angstroms or other units appropriate to the model We normally pick an area of the 2D plane to view, referred to as the viewing ‘window’. On our display device, need to allocate an area for display, referred to as the ‘viewport’ in device specific coordinates. –We “Clip” objects outside of window. –We “Translate” coordinates to fit the viewport. –We “Scale” to the device coordinates.
Graphics4 Viewing in 2D - Viewport Window in world coordinates. 45 250 Viewport in Device coords 250 x 250 Pixels.
Graphics5 Clipping in 2D Need to clip primitives (eg lines) against the sides of the viewing window –e.g lines or polygons –We only see what is inside the window
Graphics6 Trivial Clipping Acceptance If all a line’s vertices lie inside box we “accept” it
Graphics7 Trivial Vertex Rejection All line vertices lie outside and on same side reject.
Graphics8 This is an efficient method of accepting or rejecting lines that do not intersect the window edges. Assign a binary 4 bit code to each vertex : –First bit : above top of window, y > ymax –Second bit : below bottom, y < ymin –Third bit : to right of right edge, x > xmax –Fourth bit : to left of left edge, x < xmin –4-bit code called: Outcode Cohen-Sutherland Clipping Algorithm
Graphics9 Cohen-Sutherland 2D Outcodes
Graphics10 Cohen-Sutherland Algorithm Both endpoint codes 0000, trivial acceptance, else: Do logical AND of Outcodes (reject if non-zero)
Graphics11 Cohen-Sutherland Algorithm Logical AND between codes for 2 endpoints, Reject line if non-zero – trivial rejection. 0001
Graphics12 Line Intersection. Now need to intersect line segments with edges of clip rectangle. Select any clip edge, trivial line-splitting, feed two new lines back into algorithm - known as re-entrant Alternatively express line in parametric form to handle vertical lines. ( ie x=x(t), y=y(t), parameter t ) –Substitute for x or y. –Solve for t. –Need to perform 4 intersection checks for each line.
Graphics13 Cyrus & Beck Algorithm Treat line in parametric form. Note line will cross all clip boundaries somewhere :
Graphics14 Cyrus & Beck Algorithm Treat line in parametric form. Note line will cross all clip boundaries somewhere : –Find all intersections, test afterwards to determine if they are on the clip rectangle. –Define a point on the clipping plane and its normal. –Notice the dot product between a vector from this point and the intersection is zero.
Graphics15 Cyrus & Beck algorithm Edge E j NjNj P0P0 P1P1 P EJ
Graphics16 Cyrus & Beck algorithm Sign of denominator is important. Must 0 ( ignore parallel lines ). Parametric form implies direction. Denominator < 0 point entering clip region. Denominator > 0 point leaving clip region.
Graphics17 Cyrus & Beck algorithm Denominator < 0 point entering clip region, classify as PE Denominator > 0 point leaving clip region, classify as PL Edge E j NjNj D
Graphics18 Cyrus & Beck Algorithm Sort PE’s and PL’s according to t. PE PL PE t t Draw from a PE PL pair. PL < PE no intersection
Graphics19 Clipping Polygons Clip polygons by clipping successively against 4 sides.
Graphics20 Clipping Polygons Clip polygons by clipping successively against all 4 sides Can implement as pipelined algorithm (ie special hardware can do the work) Recursively test each edge. –Form new edge with next vertex –Call with new edge
Graphics21 Sutherland-Hodgman Clipping Algorithm Output Vertex First Output Four cases of polygon clipping: InsideOutsideInsideOutsideInsideOutsideInsideOutside Case 3 No output. Case 1Case 2. Output Intersection Case 4 Second Output
Graphics22 Loop round vertices, test each against all 4 clipping planes in sequence Call algorithm again with new edge formed by next vertex -re-entrant No storage requirement between stages –Easy to implement in hardware Sutherland-Hodgman Algorithm
Graphics23 Java 2D Clipping Can use any Shape as clipping boundary Graphics2D has: –clip(Shape sh ); –Shape sh = getClip(); –setClip(Shape sh ); See the API :-) The clipping shape can be any of the Shape types supported as well as the more normal Rectangle. In other circumstances you may need to implement your own clipping In 3D we need to clip to a volume - not just a 2D shape
Graphics24 Viewing & Clipping - Summary Window and ViewPort Clipping cuts out what we do not “see” Also reduces unnecessary computation Can be done at various levels Java 2D system does a lot of the clipping for us if we use that rendering pipeline Acknowledgement: Thanks to Eric McKenzie, Edinburgh, from whose Graphics Course some of these slides were based.