Presentation is loading. Please wait.

Presentation is loading. Please wait.

Clipping Clipping Sutherland-Hodgman Clipping

Similar presentations


Presentation on theme: "Clipping Clipping Sutherland-Hodgman Clipping"— Presentation transcript:

1 Clipping Clipping Sutherland-Hodgman Clipping
Cohen-Sutherland Clipping Liang-Barsky Clipping

2 The Story So Far for 3D Graphics
We now understand: how to model objects as a set of polygons and create a 3D world how to view the 3D world with a camera model, projecting the polygons to a 2D image plane What should come next: determine visible polygons in a scene (clipping and hidden surface removal) rasterise objects – turn 2D objects represented by edge coordinates to pixels (filling polygons) calculate lighting/texture values for pixels

3 Clipping Clipping removes parts of the geometry that are outside the view/clipping region Clipping edge/plane: an infinite line or plane and we want to output only the geometry on one side of it Clip region Result

4 Inside-Outside Testing
Suppose lines/planes have a normal vector pointing toward the outside of the clip region (or towards inside) Dot products give inside/outside information x is any point on the clip line/plane Outside Inside x f n i s

5 Clipping Points Against a View Volume
A point is inside the view volume if it is on the inside of all the clipping planes Now we see why clipping is done in canonical view space. For instance, to check against the left plane: x coordinate must be > -1 The normals to the clip planes can be considered to point inward, toward the visible region (user defined) In general, if a point p’(x’,y’,z’) is inside, then for a clipping plane nxx + nyy +nzz + d = 0, with (nx,ny,nz) pointing inward, nxx’ + nyy’ +nzz’ + d > 0

6 Remember the Plane Equation?
Given a point A on the plane, and the plane’s normal vector n, the plane equation can be obtained using the fact that every vector on the plane should be perpendicular to n so dot product is 0 P r nxx+nyy+nzz+d=0 (d = -n.a), n= (nx,ny,nz) So

7 Sutherland-Hodgman p2 p3 p1 p4 p0 p0 p1 p2 p3 p4
Consider the polygon as a list of vertices Clip the polygon against each edge of the clip region in turn Rewrite the polygon one vertex at a time – the rewritten polygon will be the clipped polygon p2 p3 p1 p4 p0 p0 p1 p2 p3 p4 Clip Top Vertices in Clip Right Clip Bottom Clip Left Clipped vertices out Clip Far Clip Near

8 Sutherland-Hodgman Inside Outside Inside Outside Inside Outside Inside
p i p s p s i p s Output p Output i No output Output i and p Case 1: Wholly inside visible region - save endpoint Case 2: Leave (from inside to outside) visible region - save the intersection Case 3: Wholly outside visible region - save nothing Case 4: Enter (from outside to inside) visible region - save intersection and endpoint

9 Cohen-Sutherland Clip Rectangle
We extend the edges of the clip rectangle to divide the plane of the clip rectangle into nine regions Each region is assigned a 4-bit code (outcode) determined by where the region lies with respect to the clip edges Each bit in the outcode is set to either 1 (true) or 0 (false), depending on the following conditions: Bit 1: above top edge Y > Ymax Bit 2: below bottom edge Y < Ymin Bit 3: right of right edge X > Xmax Bit 4: left of left edge X < Xmin 1001 1000 1010 0001 0000 0010 0101 0100 0110 Clip Rectangle

10 Cohen-Sutherland Use outcode to record end point in/out against each clipping line/plane An outcode has 4 bits in 2D, 6 bits in 3D, one bit per clipping line 1st and 2nd bits for Y (>Ymax, <Ymin) clipping lines, 3rd and 4th bits for X (>Xmax,<Xmin) clipping lines, 5th and 6th for Z (>Zmin,<Zmax) clipping lines Bit is 1 if point is outside the corresponding clipping line, 0 if inside

11 Cohen-Sutherland Say a=outcode (P1), b=outcode(P2)
If a=b=0 then both ends inside so line inside – trivial accept If (a | b) = 0, then one inside one outside – inconclusive - compute intersection point and check outcode for the intersection point If a & b != 0 (bitwise AND) then both ends on the same side of the window – trivial reject If a & b = 0 both ends are outside, but on the outside of different edges of the window - inconclusive - compute intersection point and check outcode for the intersection point

12 Cohen-Sutherland E a=b=0 B D A (a | b) = 0 C a & b != 0 H a & b = 0 J
F G H I J a=b=0 (a | b) = 0 a & b != 0 a & b = 0 a & b = 0 End points pairs are check for trivial acceptance or trivial rejection If not accepted or rejected, divided into two segments at a clip edge Repeat the process on resulting line segments until completely inside or rejected

13 Example P1 P1’ P2 Clip against left, right, bottom, top
boundaries in turn. P1: 1001 P2: 0100 First clip to left edge, giving P1’P2 P1’: 1000 P1’ P2 x=xmin

14 Example P1’ P1’’ P2’ P2 x=xmin P1’: 1000 P2 : 0100 No need to clip
against right edge Clip against bottom gives P1’P2’ Clip against top gives P1’’P2’ P1’ P1’’ P2’ P2’=0000, P1’=0000 P2 x=xmin

15 Calculating the Intersection
To calculate intersection of P1P2 with, say left edge: Left edge: x = xmin Line : y - y2 = m (x - x2) where m = (y2 - y1) / (x2 - x1) Thus intersection is (xmin, y*) where y* = y2 + m (xmin - x2) P2 P1

16 Liang-Barsky Clipping
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) The parametric equation of the line segment gives x values and y values for every point in terms of a parameter t that ranges from 0 to 1. The equations are We can see that when t = 0, the point computed is P(x1,y1); and when t = 1, the point computed is P(x2,y2)

17 Liang-Barsky Clipping
We want parameter values that are inside all the clipping planes Last parameter value to enter is the start of the visible segment First parameter value to leave is the end of the visible segment If we leave some clip plane before we enter another, we cannot see any part of the line Liang-Barsky is more efficient than Cohen-Sutherland - computing intersection vertices is most expensive part of clipping

18 Parametric Intersection
Find parametric intersections tright ttop tbottom tleft tb<tl< tt<tr tright tleft ttop tb<tt< tl<tr tbottom

19 Liang-Barsky Clipping
Set tmin=0 and tmax=1 If t< tmin or t > tmax 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 t is entering value set tmin =t; if t is leaving value set tmax =t If tmin < tmax then draw a line from (x1 + Dx tmin, y1 + Dy tmin) to (x1 + Dx tmax, y1 + Dy tmax) If the line crosses over the window, you will see the two points are intersections between the line and clipping edges

20 Entering or Leaving We can classify if entering or leaving value by using inner product Let P(x1,y1), Q(x2,y2) be the line and n be the normal vector (outward) If the parameter t is entering If the parameter t is leaving 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. We can determine n from the equation ax + by + c = o, where n = (a,b). Therefore, n = (1,2). Q - P = (-4-1,2-3) = (-5,-1). n. (Q - P) = (1,2) * (-5,-1) = -5-2 = -7 which is less than 0. So, this line is entering the edge

21 T Values for Intersection with Clip Edges
T value intersection with left edge: x = L With right edge: x = R With top edge: y = T With bottom edge: y = B e.g.

22 Line and Clipping Edge Intersection
From the parametric equation and the t values, we can calculate the intersection between a line and an edge as follows Intersection with left edge Intersection with right edge Intersection with top edge Intersection with bottom edge

23 Example Consider if t value is entering or exiting by using inner product. (Q-P) = (15+5,9-3) = (20,6) At left edge (Q-P).nL = (20,6)(-10,0) = -200 < 0 entering so we set tmin = 1/4 At right edge (Q-P)nR = (20,6)(10,0) = 200 > 0 exiting so we set tmax = 3/4 Because tmin < tmax then we draw a line from (-5+(20)*(1/4), 3+(6)*(1/4)) to (-5+(20)*(3/4), 3+(6)*(3/4))

24 Example Consider if tvalue is entering
or leaving by using inner product. (Q-P) = (2+8,14-2) = (10,12) At top edge (Q-P).nT = (10,12).(0,10) = 120 > 0 exiting so we set tmax = 8/12 At left edge (Q-P).nL = (10,12).(-10,0) = -100 < 0 entering so we set tmin = 8/10 Because tmin > tmax then we don't draw a line.


Download ppt "Clipping Clipping Sutherland-Hodgman Clipping"

Similar presentations


Ads by Google