Presentation is loading. Please wait.

Presentation is loading. Please wait.

159.235Graphics1 159.235 Graphics & Graphical Programming Lecture 23 - Viewing & Clipping.

Similar presentations


Presentation on theme: "159.235Graphics1 159.235 Graphics & Graphical Programming Lecture 23 - Viewing & Clipping."— Presentation transcript:

1 159.235Graphics1 159.235 Graphics & Graphical Programming Lecture 23 - Viewing & Clipping

2 159.235Graphics2 Viewing & Clipping - Outline Viewing in 2D Clipping in 2D Cohen-Sutherland Algorithm Cyrus & Beck Algorithm Clipping Polygons Sutherland-Hodgman Algorithm

3 159.235Graphics3 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.

4 159.235Graphics4 Viewing in 2D - Viewport Window in world coordinates. 45  250  Viewport in Device coords 250 x 250 Pixels.

5 159.235Graphics5 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

6 159.235Graphics6 Trivial Clipping Acceptance If all a line’s vertices lie inside box we “accept” it

7 159.235Graphics7 Trivial Vertex Rejection All line vertices lie outside and on same side  reject.

8 159.235Graphics8 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

9 159.235Graphics9 Cohen-Sutherland 2D Outcodes 0000 0100 0001 1001 10001010 0010 01100101

10 159.235Graphics10 Cohen-Sutherland Algorithm 0000 0100 0001 1001 10001010 0010 01100101 Both endpoint codes 0000, trivial acceptance, else: Do logical AND of Outcodes (reject if non-zero)

11 159.235Graphics11 Cohen-Sutherland Algorithm 0000 0100 0001 1001 1000 1010 0010 01100101 1000 0000 Logical AND between codes for 2 endpoints, Reject line if non-zero – trivial rejection. 0001

12 159.235Graphics12 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.

13 159.235Graphics13 Cyrus & Beck Algorithm Treat line in parametric form. Note line will cross all clip boundaries somewhere :

14 159.235Graphics14 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.

15 159.235Graphics15 Cyrus & Beck algorithm Edge E j NjNj P0P0 P1P1 P EJ

16 159.235Graphics16 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.

17 159.235Graphics17 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

18 159.235Graphics18 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

19 159.235Graphics19 Clipping Polygons Clip polygons by clipping successively against 4 sides.

20 159.235Graphics20 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

21 159.235Graphics21 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

22 159.235Graphics22 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

23 159.235Graphics23 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

24 159.235Graphics24 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.


Download ppt "159.235Graphics1 159.235 Graphics & Graphical Programming Lecture 23 - Viewing & Clipping."

Similar presentations


Ads by Google