Clipping The Story So Far for 3D Graphics o Clipping o We now understand: o how to model objects as a set of polygons and create o Sutherland-Hodgman Clipping a 3D world o Cohen-Sutherland Clipping o how to view the 3D world with a camera model, o Liang-Barsky Clipping projecting the polygons to a 2D image plane o What should come next: o determine visible polygons in a scene (clipping and hidden surface removal) o rasterise objects – turn 2D objects represented by edge coordinates to pixels (filling polygons) o calculate lighting/texture values for pixels Clipping Inside-Outside Testing o Clipping removes parts of the geometry that o Suppose lines/planes have a • − > n (s x) 0 normal vector pointing toward are outside the view/clipping region • − = the outside of the clip region n (i x) 0 (or towards inside) • − < n (f x) 0 o Clipping edge/plane: an infinite line or plane and we want to output only the geometry on o Dot products give one side of it inside/outside information Outside Inside x o x is any point on the clip f line/plane n Clip region i Result s 1
Clipping Points Against a View Volume Remember the Plane Equation? Given a point A on the plane, o A point is inside the view volume if it is on the inside of and the plane’s normal all the clipping planes vector n , the plane equation P can be obtained using the o Now we see why clipping is done in canonical view fact that every vector on the space. For instance, to check against the left plane: x coordinate must be > -1 plane should be r perpendicular to n so dot product is 0 o The normals to the clip planes can be considered to point inward, toward the visible region (user defined) � → � � � � � � � ( ) • = • − = − = n AP n r a n r n a 0 o In general, if a point p’(x’,y’,z’) is inside, then for a clipping plane n x x + n y y +n z z + d = 0, with (n x ,n y ,n z ) n x x + n y y + n z z + d =0 So pointing inward, n x x’ + n y y’ +n z z’ + d > 0 ( d = -n.a ), n = ( n x , n y , n z ) Sutherland-Hodgman Sutherland-Hodgman Outside Outside Outside Outside Inside Inside o Consider the polygon as a list of Inside Inside vertices p 2 p s p 3 i s p o Clip the polygon against each edge of p 1 the clip region in turn p p 4 p 0 s i p s o Rewrite the polygon one vertex at a time – the rewritten polygon will be the p 0 p 1 p 2 p 3 p 4 Output p Output i No output Output i and p clipped polygon Case 1: Wholly inside visible region - save endpoint Clip Clip Clip Vertices in Top Right Bottom Case 2: Leave (from inside to outside) visible region - save the intersection Case 3: Wholly outside visible region - save nothing Clip Clip Clip Clipped vertices out Far Near Left Case 4: Enter (from outside to inside) visible region - save intersection and endpoint 2
Cohen-Sutherland Cohen-Sutherland o We extend the edges of the clip rectangle to divide the o Use outcode to record end point in/out against each plane of the clip rectangle into nine regions clipping line/plane o Each region is assigned a 4-bit code (outcode) determined by where the region lies with respect to the o An outcode has 4 bits in 2D, 6 bits in 3D, one bit per clip edges clipping line o Each bit in the outcode is set to either 1 (true) or 0 (false), depending on the following conditions: o 1st and 2nd bits for Y (>Ymax, <Ymin) clipping lines, 3rd and 4th bits for X (>Xmax,<Xmin) clipping lines, 5 th and o Bit 1: above top edge Y > Ymax 6 th for Z (>Zmin,<Zmax) clipping lines 1001 1000 1010 o Bit 2: below bottom edge Y < Ymin o Bit 3: right of right edge X > Xmax 0001 0000 0010 o Bit is 1 if point is outside the corresponding clipping line, o Bit 4: left of left edge X < Xmin 0 if inside 0101 0100 0110 Clip Rectangle Cohen-Sutherland Cohen-Sutherland o Say a=outcode (P1), b=outcode(P2) E a=b=0 B o If a=b=0 then both ends inside so line inside – trivial accept D A (a | b) = 0 C H a & b != 0 o If (a | b) = 0 , then one inside one outside – inconclusive - compute intersection point and check outcode for the a & b = 0 intersection point J G F a & b = 0 o If a & b != 0 (bitwise AND) then both ends on the same side of I the window – trivial reject o If a & b = 0 both ends are outside, but on the outside of o End points pairs are check for trivial acceptance or trivial rejection different edges of the window - inconclusive - compute o If not accepted or rejected, divided into two segments at a clip edge intersection point and check outcode for the intersection point o Repeat the process on resulting line segments until ocompletely inside or rejected 3
Example Example P 1 Clip against P 1 ’: 1000 left, right, P 1 ’ P 2 : 0100 P 1 ’ bottom, top boundaries in turn. P 1 ’’ No need to clip against right edge P 1 : 1001 P 2 : 0100 Clip against bottom gives P 1 ’P 2 ’ First clip to left edge, giving P 2 ’ Clip against top P 1 ’P 2 gives P 1 ’’P 2 ’ P 1 ’: 1000 P 2 ’=0000, P 1 ’=0000 P 2 P 2 x=xmin x=x min Calculating the Intersection Liang-Barsky Clipping o To calculate intersection of P1P2 with, say left edge: o This uses the parametric equations for a line and solves four inequalities to find the range of the parameter for which the line is visible (within the viewport) o Left edge: x = x min o The parametric equation of the line segment gives x o Line : y - y2 = m (x - x2) values and y values for every point in terms of a o where m = (y2 - y1) / (x2 - x1) parameter t that ranges from 0 to 1. The equations are ( ) P 2 = + − = + ∆ x x x x t x xt o Thus intersection is (x min , y*) 1 2 1 1 ( ) o where y* = y2 + m (x min - x2) = + − = + ∆ y y y y t y yt 1 2 1 1 o We can see that when t = 0, the point computed is P(x 1 ,y 1 ); and when t = 1, the point computed is P(x 2 ,y 2 ) P 1 4
Liang-Barsky Clipping Parametric Intersection o We want parameter values that are inside all the clipping planes o Find parametric intersections t right o Last parameter value to enter is the start of the visible segment t top o First parameter value to leave is the end of the visible segment t bottom t left o If we leave some clip plane before we enter another, we cannot see any part of the line t b <t l < t t <t r t right o Liang-Barsky is more efficient than Cohen-Sutherland - t left computing intersection vertices is most expensive part of clipping t top t b <t t < t l <t r t bottom Liang-Barsky Clipping Entering or Leaving o Set t min =0 and t max =1 We can classify if entering or leaving value by using inner product Let P(x 1 ,y 1 ), Q(x 2 ,y 2 ) be the line and n be the normal vector (outward) o If t< t min or t > t max then ignore it and go to the next edge. Otherwise classify the t value as entering or leaving value (using inner product to classify) If the parameter t is entering If the parameter t is leaving o If t is entering value set t min =t; if t is leaving value set t max =t Let P = (1,3), Q = (-4,2) while the edge equation is x+2y-4 = 0. Determine if the vector from P to Q is entering or leaving the edge. o If t min < t max then draw a line from We can determine n from the equation ax + by + c = o, where n = (a,b). (x 1 + ∆ x t min , y 1 + ∆ y t min ) to (x 1 + ∆ x t max , y 1 + ∆ y t max ) Therefore, n = (1,2). Q - P = (-4-1,2-3) = (-5,-1). o If the line crosses over the window, you will see the two points n. (Q - P) = (1,2) * (-5,-1) = -5-2 = -7 which is less than 0. are intersections between the line and clipping edges So, this line is entering the edge 5
T Values for Intersection with Clip Edges Line and Clipping Edge Intersection o From the parametric equation and the t values, we can calculate the intersection o T value intersection with left edge: x = L between a line and an edge as follows o With right edge: x = R o Intersection with left edge o With top edge: y = T o Intersection with right edge o With bottom edge: y = B o Intersection with top edge o Intersection with bottom edge ( ) = + − L = x x x x t L e.g. ( ) = + − = + ∆ 1 2 1 x x x x t x xt 1 2 1 1 ( ) = + − = + ∆ y y y y t y yt 1 2 1 1 Example Example Consider if t value is entering or Consider if tvalue is entering exiting by using inner product. or leaving by using inner product. (Q-P) = (15+5,9-3) = (20,6) (Q-P) = (2+8,14-2) = (10,12) At left edge (Q-P).n L = (20,6)(-10,0) = -200 < 0 entering so we set t min = 1/4 At top edge (Q-P).n T = (10,12).(0,10) = 120 > 0 exiting At right edge (Q-P)n R = (20,6)(10,0) so we set t max = 8/12 = 200 > 0 exiting so we set t max = 3/4 At left edge (Q-P).n L = Because t min < t max then we draw a (10,12).(-10,0) = -100 < 0 entering line from (-5+(20)*(1/4), 3+(6)*(1/4)) to (- so we set t min = 8/10 5+(20)*(3/4), 3+(6)*(3/4)) Because t min > t max then we don't draw a line. ( ) x = x + x − x t = x + ∆ xt 1 2 1 1 ( ) y = y + y − y t = y + ∆ yt 1 2 1 1 6
Recommend
More recommend