clipping

34
1 Clipping 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 Learning outcomes for this lecture

Upload: johanna20

Post on 06-May-2015

19.401 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Clipping

1

Clipping

You willunderstand

What is meant by ‘clipping’2D clipping concepts3D clipping concepts

Different kinds of clippingThe Cohen-Sutherland 2D region-coding clipping technique

Be able tocalculate Cohen-Sutherland 2D region-coding

Learning outcomes for this lecture

Page 2: Clipping

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

Page 3: Clipping

3

Modeling Transformations

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Object space World space

x'y'z'1

=

xyz1

aei0

bfj0

cgk0

dhl1

Illumination (Shading) (Lighting)Vertices lit (shaded) according to material properties, surface properties (normal) and lightLocal lighting model (Diffuse, Ambient, Phong, etc.)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

2 4 )()()( )(

dkkkL sq

sdar πω Φ

⋅+⋅+= rvln

Page 4: Clipping

4

Viewing TransformationViewing position is transformed to origin & direction is oriented along some axis (usually z)

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Eye space

World space

Yet another 4x4 matrix

x'y'z'1

=

xyz1

aei0

bfj0

cgk0

dhl1

Clipping

Modeling Transformations

Illumination(Shading)

Viewing Transformation(Perspective / Orthographic)

Clipping

Projection (to Screen Space)

Scan Conversion(Rasterization)

Visibility / Display

Page 5: Clipping

5

Games: pipeline

Flight simulation: pipeline (painter for long time)

Page 6: Clipping

6

Questions?

Plan

Review of rendering pipeline

Clipping

Page 7: Clipping

7

Clipping

Clipping meansIdentifying portions of a scene that are inside (or outside) a specified region

ExamplesMultiple viewports on a device

Deciding how much of a games world the player can see

Clipping

Clipping meansIdentifying portions of a scene that are inside (or outside) a specified region

ExamplesMultiple viewports on a device

Deciding how much of a games world the player can see

Player can’t see this far yet

Page 8: Clipping

8

Clipping

we’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport

in general, this assumption will not hold:

ClippingEliminate portions of objects outside the viewing frustumView Frustum

boundaries of the image plane projected in 3Da near & far clipping plane

User may define additional clipping planes

bottom

top

right

left

near

far

Page 9: Clipping

9

Why clip?

Avoid degeneraciesDon’t draw stuff behind the eyeAvoid division by 0 and overflow

EfficiencyDon’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!

Page 10: Clipping

10

When to clip?

Before perspective transform in 3D space

Use the equation of 6 planesNatural, not too degenerate

In homogeneous coordinates after perspective transform (Clip space)

Before perspective divide (4D space, weird w values)Canonical,independent of cameraThe 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 rasterizationAnalytical clipping: alter input geometry

Page 11: Clipping

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

Page 12: Clipping

12

Interior and exterior clipping

interior clippingwhat 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

We shall assume interior clipping for now

But you must be aware of exterior clipping too

Exterior clipping- keep point P1

Clip window

P1(x1, y1)

Page 13: Clipping

13

Overview of types of clipping

All-or-none clippingIf any part of object outside clip windowwhole object is rejected

Point clippingOnly keep points inside clip window

Line clippingOnly keep segment of line inside clip window

Polygon clippingOnly keep sub-polygons inside clip window

Before and after POINT clipping

Before

After

P 1 P 3

P 2

P 4

P 1 P 3

Page 14: Clipping

14

Point Classification

How can we tell if a point is inside a rectangular window?

xmin xmax

ymax

ymin

maxmin

maxmin

yyyxxx

≤≤≤≤

maxmin

maxmin

yyyxxx

≤≤≤≤

Line Clipping Algorithms

Goal: avoid drawing primitives that are outside the viewing window.The most common case: clipping line segments against a rectangular window

Page 15: Clipping

15

Before and after LINE clipping

Before

After

P 1 ’

P 2 ’

P1

P2

Before and after POLYGON clipping

Before

After

Page 16: Clipping

16

Clipping Lines

naïve approach to clipping lines:for each line segment

for each edge of viewportfind intersection pointpick “nearest” point

if anything is left, draw it

what do we mean by “nearest”?how can we optimize this?

A

B

CD

Trivial Accepts

big optimization: trivial accept/rejectsQ: how can we quickly determine whether a line segment is entirely inside the viewport?

A: test both endpoints.

Page 17: Clipping

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/rejectstrivially accept lines with both endpoints inside all edges of the viewporttrivially reject lines with both endpoints outside the same edge of the viewportotherwise, reduce to trivial cases by splitting into two segments

Page 18: Clipping

18

Line Clipping Algorithms

Three cases:Segment is entirely inside the window -AcceptSegment is entirely outside the window -RejectSegment intersects the boundary - Clip

Line Segment Clipping

If both endpoints are inside the window, the entire segment is inside: trivial acceptIf one endpoint is inside, and the other is outside, line segment must be split.What happens when both endpoints are outside?

Page 19: Clipping

19

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.

A point is visible if

xl < x < xr

andyb < y < yt

(xr, yt)(xl, yt)

(xl, yb) (xr, yb)

(x, y)

Clipping Lines

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.

Cohen-Sutherland Clipping

Page 20: Clipping

20

Cohen-Sutherland 2D clipping

4 regions are defined – outside the clip window

TOPBOTTOMLEFTRIGHT

P1

P2

Cohen-Sutherland Algorithm

Assign a 4-digit binary outcode to each of the 9 regions defined by the window:

xmin xmax

ymax

ymin

0000 00100001

0110

10101000

0100

1001

0101

Page 21: Clipping

21

Cohen-Sutherland Algorithm

xmin xmax

ymax

ymin

0000 00100001

0110

10101000

0100

1001

0101

bitbit

11

22

33

44

conditioncondition

y > y > yymaxmax

y < y < yyminmin

x > x > xxmaxmax

x < x < xxminmin

The 4-bit codes

A 4-bit code is assigned to each pointThis code is sometimes called a Cohen-Sutherland region code

LEFT bit 1 = binary 0001RIGHT bit 2 = binary 0010BOTTOM bit 3 = binary 0100TOP bit 4 = binary 1000

So a point coded 0001 is LEFT of the clip window, etc.

Page 22: Clipping

22

Cohen-Sutherland region codes

The point (65, 50) is above and to the right of the clip window,Therefore gets the code: 1010

(10, 10) (60, 10)

(60, 40)(10, 40)

(65, 50)

Cohen-Sutherland region codes

1000 indicates the TOP region0010 indicates the RIGHT regionthe 4 bit Cohen-Sutherland codeis formed by a logical OR of all region codes

1000 TOPOR 0010 RIGHT------------------= 1010

Page 23: Clipping

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 windowIf code is non-zero point is outside window

For the two endpoints of a lineIf codes for both endpoints are zero,whole line is inside windowIf (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

Line1 codes are 1001 and 0000Line2 codes are 0000 and 0000 << insideLine3 codes are 1000 and 0100Line4 codes are 1010 and 0110 << outside

L in e 1

L in e 2

L in e 3

L in e 4

TOP BOTTOM RIGHT LEFT

Page 24: Clipping

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 windowCheck second bit (BOTTOM) and so on

Compute the outcodes C1 and C2 corresponding to both segment endpoints.If ((C1 | C2) == 0): Trivial AcceptIf ((C1 & C2) != 0): Trivial RejectOtherwise, split segment into two parts. Reject one part, and repeat the procedure on the remaining part.What edge should be intersected ?

Cohen-Sutherland Algorithm

Page 25: Clipping

25

Example

xmin xmax

ymax

ymin

AB

CD

Outcode(A) = 0000

Outcode(D) = 1001

Clip (A,D) with y = ymax, splitting it into (A,B) and (B,D)

Reject (B,D)

Proceed with (A,B)

No trivial accept/reject

Example

xmin xmax

ymax

ymin

A

B

C D

Outcode(A) = 0100

Outcode(E) = 1010

Clip (A,E) with y = ymax, splitting it into (A,D) and (D,E)

Reject (D,E)

Proceed with (A,D)

No trivial accept/reject

E

Page 26: Clipping

26

Example

xmin xmax

ymax

ymin

A

B

C D

Outcode(AOutcode(A) = 0100) = 0100

Outcode(DOutcode(D) = 0010) = 0010

Clip (A,D) with Clip (A,D) with y = y = yyminmin, splitting it into (A,B) and (B,D), splitting it into (A,B) and (B,D)

Reject (A,B)Reject (A,B)

Proceed with (B,D)Proceed with (B,D)

No trivial accept/rejectNo trivial accept/reject

Example

xmin xmax

ymax

yminB

C D

Outcode(BOutcode(B) = 0000) = 0000

Outcode(DOutcode(D) = 0010) = 0010

Clip (B,D) with Clip (B,D) with x = x = xxmaxmax, splitting it into (B,C) and (C,D), splitting it into (B,C) and (C,D)

Reject (C,D)Reject (C,D)

Proceed with (B,C)Proceed with (B,C)

No trivial accept/rejectNo trivial accept/reject

Page 27: Clipping

27

Clipping a line

Choose point P2 (code is 1000)

Clip from point to TOP of clip windowP 1

P 2

P 1

Clipping a line

Choose point P1 (code is 0110)

Bit 1 (TOP) not setBit 2 (BOTTOM) set – so clip bottom

P 1

Page 28: Clipping

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 knowEnd points of line(x1, y1) – (x2, y2)X or Y value definingclip window edge

(similar triangles and line gradient)m = (y2 – y1) / (x2 – x1); // (gradient)newx = xwmin;newy = y1 + m*(xwmin – x1)

xmin

(x1, y1)(x2, y2)

(newx, newy)

Page 29: Clipping

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 costRedundant clipping of some lines

More efficient algorithms exist

Solving Simultaneous Equations

Equation of a lineSlope-intercept (explicit equation): y = mx + bImplicit Equation: Ax + By + C = 0Parametric 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) ty(t) = y0 + (y1 - y0) t

Page 30: Clipping

30

Parametric Line Equation

Describes a finite lineWorks with vertical lines (like the viewportedge)

0 <=t <= 1Defines line between P0 and P1

t < 0Defines line before P0

t > 1Defines 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

Page 31: Clipping

31

Line / Edge Clipping Equations

Faster line clippers use parametric equationsLine 0:

x0 = x00 + (x0

1 - x00) t0

y0 = y00 + (y0

1 - y00) t0

View Window Edge L:xL = xL

0 + (xL1 - xL

0) tL

yL = yL0 + (yL

1 - yL0) tL

x00 + (x0

1 - x00) t0 = xL

0 + (xL1 - xL

0) tL

y00 + (y0

1 - y00) t0 = yL

0 + (yL1 - yL

0) 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 edgePL, NL

Page 32: Clipping

32

Cyrus-Beck Algorithm

Find t such thatNL [P(t) - PL] = 0

Substitute line equation for P(t): NL [P0 + (P1 - P0) t - PL] = 0

Solve for tt = NL [PL – P0] / -NL [P1 - P0]

PL

NL

P(t)Inside

P0

P1

Cyrus-Beck Algorithm

Compute t for line intersection with all four edgesDiscard all (t < 0) and (t > 1)Classify each remaining intersection as

Potentially Entering (PE)Potentially Leaving (PL)

NL [P1 - P0] > 0 implies PLNL [P1 - P0] < 0 implies PE

Note that we computed this term when computing t so we can keep it around

Page 33: Clipping

33

Compute PE with largest tCompute PL with smallest tClip to these two points

Cyrus-Beck Algorithm

PE

PL P1

PL

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 edgessolution for t:

-(x0 - xleft) / (x1 - x0)(x0 - xright) / -(x1 - x0)-(y0 - ybottom) / (y1 - y0)(y0 - ytop) / -(y1 - y0)

Page 34: Clipping

34

Comparison

Cohen-SutherlandRepeated clipping is expensiveBest used when trivial acceptance and rejection is possible for most lines

Cyrus-BeckComputation of t-intersections is cheapComputation of (x,y) clip points is only done onceAlgorithm doesn’t consider trivial accepts/rejectsBest when many lines must be clipped

Liang-Barsky: Optimized Cyrus-BeckNicholl et al.: Fastest, but doesn’t do 3D