Clipping

23,888 views

Published on

Published in: Technology, Art & Photos
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
23,888
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
731
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Clipping

  1. 1. Clipping 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 1
  2. 2. Plan Review of rendering pipeline Clipping The Graphics Pipeline Modeling Transformations Illumination (Shading) Viewing Transformation (Perspective / Orthographic) Clipping Projection (to Screen Space) Scan Conversion (Rasterization) Visibility / Display 2
  3. 3. Modeling Transformations Modeling Transformations Illumination (Shading) Viewing Transformation (Perspective / Orthographic) Object space World space Clipping x' a b c d x Projection y' e f g h y (to Screen Space) = Scan Conversion z' i j k l z (Rasterization) 1 0 0 0 1 1 Visibility / Display Illumination (Shading) (Lighting) Vertices lit (shaded) according to Modeling material properties, surface Transformations properties (normal) and light Local lighting model Illumination (Diffuse, Ambient, Phong, etc.) (Shading) Φ Viewing Transformation (Perspective / Orthographic) ( L(ωr ) = k a + k d (n ⋅ l) + k s (v ⋅ r ) q ) 4π d s 2 Clipping Projection (to Screen Space) Scan Conversion (Rasterization) Visibility / Display 3
  4. 4. Viewing Transformation Modeling Viewing position is transformed to Transformations origin & direction is oriented along some axis (usually z) Illumination (Shading) Eye space Viewing Transformation (Perspective / Orthographic) Clipping Projection World space (to Screen Space) x' a b c d x Scan Conversion e f g h y Yet another y' (Rasterization) 4x4 matrix z' = i j k l z Visibility / Display 1 0 0 0 1 1 Clipping Modeling Transformations Illumination (Shading) Viewing Transformation (Perspective / Orthographic) Clipping Projection (to Screen Space) Scan Conversion (Rasterization) Visibility / Display 4
  5. 5. Games: pipeline Flight simulation: pipeline (painter for long time) 5
  6. 6. Questions? Plan Review of rendering pipeline Clipping 6
  7. 7. Clipping Clipping means Identifying portions 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 Clipping Clipping means Identifying portions 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 7
  8. 8. Clipping we’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport in general, this assumption will not hold: Clipping Eliminate portions of objects outside the viewing frustum View Frustum boundaries of the image plane projected in 3D far a near & far clipping plane top User may define left additional clipping planes near right bottom 8
  9. 9. Why clip? Avoid degeneracies Don’t draw stuff behind the eye Avoid division by 0 and overflow Efficiency Don’t waste time on objects outside the image boundary Other graphics applications (often non-convex) Hidden-surface removal, Shadows, Picking, Binning, CSG (Boolean) operations (2D & 3D) Why Clip? bad idea to rasterize outside of framebuffer bounds also, don’t waste time scan converting pixels outside window could be billions of pixels for very close objects! 9
  10. 10. When to clip? Before perspective transform in 3D space Use the equation of 6 planes Natural, not too degenerate In homogeneous coordinates after perspective transform (Clip space) Before perspective divide (4D space, weird w values) Canonical,independent of camera The simplest to implement in fact In the transformed 3D screen space after perspective division Problem: objects in the plane of the camera Clipping strategies Don’t clip (and hope for the best) Clip on-the-fly during rasterization Analytical clipping: alter input geometry 10
  11. 11. 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 Clipping analytically calculating the portions of primitives within the viewport 11
  12. 12. 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) Interior and exterior clipping Exterior clipping Clip window - keep point P1 P1 (x1, y1) We shall assume interior clipping for now But you must be aware of exterior clipping too 12
  13. 13. Overview of types of clipping All-or-none clipping If any part of object outside clip window whole 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 Before and after POINT clipping Before P2 P3 P1 P4 After P3 P1 13
  14. 14. Point Classification How can we tell if a point is inside a rectangular window? ymax xmin ≤ x ≤ xmax ymin ≤ y ≤ ymax ymin xmin xmax Line Clipping Algorithms Goal: avoid drawing primitives that are outside the viewing window. The most common case: clipping line segments against a rectangular window 14
  15. 15. Before and after LINE clipping Before P2 After P1 P2’ P1’ Before and after POLYGON clipping Before After 15
  16. 16. Clipping Lines naïve approach to clipping lines: for each line segment for each edge of viewport find intersection point pick “nearest” point if anything is left, draw it what do we mean by “nearest”? how can we optimize this? B D C A Trivial Accepts big optimization: trivial accept/rejects Q: how can we quickly determine whether a line segment is entirely inside the viewport? A: test both endpoints. 16
  17. 17. Trivial Rejects Q: how can we know a line is outside viewport? A: if both endpoints on wrong side of same edge, can trivially reject line Clipping Lines To Viewport combining trivial accepts/rejects trivially accept lines with both endpoints inside all edges of the viewport trivially reject lines with both endpoints outside the same edge of the viewport otherwise, reduce to trivial cases by splitting into two segments 17
  18. 18. Line Clipping Algorithms Three cases: Segment is entirely inside the window - Accept Segment is entirely outside the window - Reject Segment intersects the boundary - Clip 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? 18
  19. 19. Clipping Lines (xl, yt) (xr, yt) A point is visible if xl < x < xr (x, y) and yb < y < yt (xl, yb) (xr, yb) 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. 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. 19
  20. 20. Cohen-Sutherland 2D clipping 4 regions are defined – outside the clip window TOP BOTTOM LEFT P2 RIGHT P1 Cohen-Sutherland Algorithm Assign a 4-digit binary outcode to each of the 9 regions defined by the window: 1001 1000 1010 ymax 0001 0000 0010 ymin 0101 0100 0110 xmin xmax 20
  21. 21. Cohen-Sutherland Algorithm 1001 1000 1010 bit condition ymax 1 y > ymax 0001 0000 0010 2 y < ymin ymin 3 x > xmax 0101 0100 0110 4 x < xmin xmin xmax 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. 21
  22. 22. Cohen-Sutherland region codes The point (65, 50) is above and to the right of the clip window, Therefore gets the code: 1010 (65, 50) (10, 40) (60, 40) (10, 10) (60, 10) Cohen-Sutherland region codes 1000 indicates the TOP region 0010 indicates the RIGHT region the 4 bit Cohen-Sutherland code is formed by a logical OR of all region codes 1000 TOP OR 0010 RIGHT ------------------ = 1010 22
  23. 23. 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 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 L in e 1 L in e 4 L in e 2 L in e 3 23
  24. 24. 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 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 ? 24
  25. 25. Example D C ymax B Outcode(A) = 0000 A ymin Outcode(D) = 1001 No trivial accept/reject xmin xmax Clip (A,D) with y = ymax, splitting it into (A,B) and (B,D) Reject (B,D) Proceed with (A,B) Example E ymax D C Outcode(A) = 0100 B ymin Outcode(E) = 1010 No trivial accept/reject A xmin xmax Clip (A,E) with y = ymax, splitting it into (A,D) and (D,E) Reject (D,E) Proceed with (A,D) 25
  26. 26. Example ymax D C Outcode(A) = 0100 Outcode(A) B ymin Outcode(D) = 0010 Outcode(D) No trivial accept/reject A xmin xmax Clip (A,D) with y = ymin, splitting it into (A,B) and (B,D) Reject (A,B) Proceed with (B,D) Example ymax D C Outcode(B) = 0000 Outcode(B) B ymin Outcode(D) = 0010 Outcode(D) No trivial accept/reject xmin xmax Clip (B,D) with x = xmax, splitting it into (B,C) and (C,D) Reject (C,D) Proceed with (B,C) 26
  27. 27. Clipping a line Choose point P2 (code is 1000) P2 P1 Clip from point to TOP of clip window P1 Clipping a line Choose point P1 (code is 0110) Bit 1 (TOP) not set P1 Bit 2 (BOTTOM) set – so clip bottom 27
  28. 28. Clipping a line Bit 3 (RIGHT) set – so clip right Bit 4 (LEFT) not set – so clipping finished How to clip a line Need to know xmin End points of line (x1, y1) – (x2, y2) (x2, y2) X or Y value defining (x1, y1) clip window edge (newx, newy) (similar triangles and line gradient) m = (y2 – y1) / (x2 – x1); // (gradient) newx = xwmin; newy = y1 + m*(xwmin – x1) 28
  29. 29. Cohen-Sutherland Review Use opcodes to quickly eliminate/include lines Best algorithm when trivial accepts/rejects are common Must compute viewing window clipping of remaining lines Non-trivial clipping cost Redundant clipping of some lines More efficient algorithms exist Solving Simultaneous Equations Equation of a line Slope-intercept (explicit equation): y = mx + b Implicit Equation: Ax + By + C = 0 Parametric Equation: Line defined by two points, P0 and P1 P(t) = P0 + (P1 - P0) t, where P is a vector [x, y]T x(t) = x0 + (x1 - x0) t y(t) = y0 + (y1 - y0) t 29
  30. 30. Parametric Line Equation Describes a finite line Works with vertical lines (like the viewport edge) 0 <=t <= 1 Defines line between P0 and P1 t<0 Defines line before P0 t>1 Defines line after P1 Parametric Lines and Clipping Define each line in parametric form: P0(t)…Pn-1(t) Define each edge of view window in parametric form: PL(t), PR(t), PT(t), PB(t) Perform Cohen-Sutherland intersection tests using appropriate view window edge and line 30
  31. 31. Line / Edge Clipping Equations Faster line clippers use parametric equations Line 0: x0 = x00 + (x01 - x00) t0 y0 = y00 + (y01 - y00) t0 View Window Edge L: xL = xL0 + (xL1 - xL0) tL yL = yL0 + (yL1 - yL0) tL x00 + (x01 - x00) t0 = xL0 + (xL1 - xL0) tL y00 + (y01 - y00) t0 = yL0 + (yL1 - yL0) tL Solve for t0 and/or tL Cyrus-Beck Algorithm We wish to optimize line/line intersection Start with parametric equation of line: P(t) = P0 + (P1 - P0) t And a point and normal for each edge PL, NL 31
  32. 32. Cyrus-Beck Algorithm Find t such that P1 PL NL [P(t) - PL] = 0 P(t) Inside P0 NL Substitute line equation for P(t): NL [P0 + (P1 - P0) t - PL] = 0 Solve for t t = NL [PL – P0] / -NL [P1 - P0] Cyrus-Beck Algorithm Compute t for line intersection with all four edges Discard all (t < 0) and (t > 1) Classify each remaining intersection as Potentially Entering (PE) Potentially Leaving (PL) NL [P1 - P0] > 0 implies PL NL [P1 - P0] < 0 implies PE Note that we computed this term when computing t so we can keep it around 32
  33. 33. Cyrus-Beck Algorithm Compute PE with largest t Compute PL with smallest t Clip to these two points PL P1 PL PE PE P0 Cyrus-Beck Algorithm Because of horizontal and vertical clip lines: Many computations reduce Normals: (-1, 0), (1, 0), (0, -1), (0, 1) Pick constant points on edges solution for t: -(x0 - xleft) / (x1 - x0) (x0 - xright) / -(x1 - x0) -(y0 - ybottom) / (y1 - y0) (y0 - ytop) / -(y1 - y0) 33
  34. 34. Comparison Cohen-Sutherland Repeated clipping is expensive Best used when trivial acceptance and rejection is possible for most lines Cyrus-Beck Computation of t-intersections is cheap Computation of (x,y) clip points is only done once Algorithm doesn’t consider trivial accepts/rejects Best when many lines must be clipped Liang-Barsky: Optimized Cyrus-Beck Nicholl et al.: Fastest, but doesn’t do 3D 34

×