Download Examples of Clipping - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity! Lecture 15: Clipping Docsity.com Clipping โข Last week we discussed some simple examples of clipping in Camera โ We said that OpenGL takes care of a lot of that for free โข But clipping is an important part of computer graphics: Docsity.com Clip Rectangle Cohen-Sutherland Line Clipping in 2D โข Divide plane into 9 regions โข Compute the sign bit of 4 comparisons between a vertex and a clip edge โ (๐ฆ๐๐๐ฅ โ ๐ฆ, ๐ฆ โ ๐ฆ๐๐๐, ๐ฅ๐๐๐ฅ โ ๐ฅ; ๐ฅ โ ๐ฅ๐๐๐), cast the results to 0 or 1 โ Point lines inside the region if all four bits are 0 ๏ฝ 4 bit outcode records results of four bounds tests: ๏ฝ 1st bit: outside halfplane of top edge (above top edge) ๏ฝ 2nd bit: outside halfplane of bottom edge ๏ฝ 3rd bit: outside halfplane of right edge ๏ฝ 4th bit: outside halfplane of left edge ๏ฝ Compute outcodes for both vertices of the input edge, denoted ๐๐ถ0 and ๐๐ถ1 ๏ฝ If ๐๐ถ0 = 0 and ๐๐ถ1 = 0, (i.e., outcode: 0000), then the input edge is trivially accepted ๏ฝ Lines lying entirely in a particular halfplane can e trivially rejected. That is ๐๐ถ0 ๐ด๐๐ท ๐๐ถ1 โ 0 (i.e., they share an โoutside bitโ) Docsity.com Cohen-Sutherland Algorithm โข If we can neither trivially accept or reject, then we do divide-and-conquer โข Subdivide line into two segments and test again Clip rectangle D C B A E F G H I ๏ฝ Use a clip edge to cut line ๏ฝ Use outcodes to choose which edge is crossed ๏ฝ The bits that are different between outcodes will tell us which edge to examine ๏ฝ Pick an order for checking edges: top โ bottom โ right โ left ๏ฝ Compute the intersection point ๏ฝ Clip edge will be axis-aligned, so we can fix either the x or the y ๏ฝ Can substitute into the line equation ๏ฝ Iterate for the newly created line segment, might need multiple passes (e.g., E-I at H) Docsity.com Cohen-Sutherland Algorithm โข Assumes the form: โ ๐ฆ = ๐ฆ0 + ๐ ๐๐๐๐ โ (๐ฅ โ ๐ฅ0) โ ๐ฅ = ๐ฅ0 + (1/๐ ๐๐๐๐) โ (๐ฆ โ ๐ฆ0) Algorithm (float x0, y0, x1, y1) ComputeOutCode(x0, y0, outcode0); ComputeOutCode(x1, y1, outcode1); repeat check for trivial reject or trivial accept pick the point that is outside the clip rectangle if TOP then x = x0 + (x1 โ x0) * (ymax โ y0)/(y1 โ y0); y = ymax; else if BOTTOM then x = x0 + (x1 โ x0) * (ymin โ y0)/(y1 โ y0); y = ymin; else if RIGHT then y = y0 + (y1 โ y0) * (xmax โ x0)/(x1 โ x0); x = xmax; else if LEFT then y = y0 + (y1 โ y0) * (xmin โ x0)/(x1 โ x0); x = xmin; if (x0, y0 is the outer point) then x0 = x; y0 = y; ComputeOutCode(x0, y0, outcode0) else x1 = x; y1 = y; ComputeOutCode(x1, y1, outcode1) until done DrawRectangle(xmin, ymin, xmax, ymax) DrawLine (x0, y0, x1, y1) Docsity.com Cyrus-Beck / Liang-Barsky Parametric Line Clipping โข Use the parametric line formulation: โ ๐(๐ก) = ๐0 + ๐ก(๐1 โ ๐0) โข Determine if the line intersects with a clip line (both extended to infinity) โข Decide if the intersection actually occurs on the polygon โข This is a very similar strategy for intersection tests in ray-tracing Docsity.com Parametric Line Clipping โข Uses the relationship between the Normal of the clipping edge and points on the line to be clipped โข Given the normal ๐๐ and any point ๐๐ธ๐ on edge ๐ธ๐ Docsity.com Parametric Line Clipping, Using Dot Product โข Find t โ Note that if t is less than 0 or greater than 1, then the intersection occurs outside of the line segment Docsity.com Parametric Line Clipping, Against A Polygon โข When applied to line-clipping, there will be multiple t values (a t value for each clip edge test) โ Eliminate t values outside of [0, 1] โ Determine if the intersection is considered potentially entering (PE) or potentially leaving (PL) โข Problem, how can you tell?? PE PL Docsity.com Parametric Line Clipping, Against A Polygon โข Turns out: โ if (Ni โข D) < 0 => Potentially Entering (PE), if (Ni โข D) > 0 => Potentially Leaving (PL) โข Remember, D = (๐1 โ ๐0) โข Pick the inner most pair of PE and PL โ Traverse from P0 to P1: โข If we encounter a PL before a PE, there is no intersection (see Line 2) โข Otherwise, pick PE with the max t, and PL with the min t โข Note that t must be between [0, 1] Docsity.com Pseudocode Pre-calculate Ni and select PEi for each edge; for each line segment to be clipped if P1 = P0 then line is degenerate so clip as a point; else begin tE = 0; tL = 1; for each candidate intersection with a clip edge if Ni โข D ๏น 0 then {Ignore edges parallel to line} begin calculate t; {of line and clip edge intersection} use sign of Ni โข D to categorize as PE or PL; if PE then tE = max(tE,t); if PL then tL = min(tL,t); end if tE > tL then return nil else return P(tE) and P(tL) as true clip intersections end Docsity.com