1 Clipping Points Against a View Volume Remember the Plane - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 Clipping Points Against a View Volume Remember the Plane - - PDF document

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


slide-1
SLIDE 1

1

Clipping

  • Clipping
  • Sutherland-Hodgman Clipping
  • Cohen-Sutherland Clipping
  • Liang-Barsky Clipping

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

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

  • ne side of it

Clip region Result

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 n s f i x

< −

  • =

  • >

  • x)

(f n x) (i n x) (s n

slide-2
SLIDE 2

2

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

Remember the Plane Equation?

nxx+nyy+nzz+d=0 (d = -n.a), n= (nx,ny,nz)

So P

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

r

( )

= − = −

  • =

a n r n a r n AP n

  • Sutherland-Hodgman
  • 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

p0 p4 p3 p2 p1 p0 p4 p3 p2 p1

Clip Top Vertices in Clip Right Clip Bottom Clip Left Clipped vertices out Clip Far Clip Near

Sutherland-Hodgman

Inside Outside s p Output p s p Output i s p No output s p Output i and p i i

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

Inside Outside Inside Outside Inside Outside

slide-3
SLIDE 3

3

Cohen-Sutherland

  • 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

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

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

Cohen-Sutherland

A B C D E F G H I J

  • 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

a & b = 0 a & b != 0 a & b = 0 a=b=0 (a | b) = 0

slide-4
SLIDE 4

4

Example

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

P1’

Example

P2 x=xmin P1’

P1’: 1000 P2 : 0100 No need to clip against right edge Clip against bottom gives P1’P2’ Clip against top gives P1’’P2’

P2’ P1’’

P2’=0000, P1’=0000

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)

P1 P2

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)

( ) ( )

yt y t y y y y xt x t x x x x ∆ + = − + = ∆ + = − + =

1 1 2 1 1 1 2 1

slide-5
SLIDE 5

5

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

Parametric Intersection

  • Find parametric intersections

tbottom tleft ttop tright tb<tl< tt<tr tbottom tleft ttop tright tb<tt< tl<tr

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 + ∆x tmin, y1 + ∆y tmin) to (x1 + ∆x tmax, y1 + ∆y tmax)

  • If the line crosses over the window, you will see the two points

are intersections between the line and clipping edges

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

slide-6
SLIDE 6

6

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.

( )

L t x x x x

L =

− + =

1 2 1

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

( ) ( )

yt y t y y y y xt x t x x x x ∆ + = − + = ∆ + = − + =

1 1 2 1 1 1 2 1

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))

Example

( ) ( )

yt y t y y y y xt x t x x x x ∆ + = − + = ∆ + = − + =

1 1 2 1 1 1 2 1

Example

Consider if tvalue is entering

  • r 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.