Top Banner
Clipping Slides courtesy: David Luebke
51

Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Apr 22, 2019

Download

Documents

truongquynh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Slides courtesy: David Luebke

Page 2: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping

• We’ve been assuming that all primitives (lines,

triangles, polygons) lie entirely within the

viewport

• In general, this assumption will not hold:

Page 3: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping

• Definition: Analytically calculating the

portions of primitives within the viewport

Page 4: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Why Clip?

• Bad idea to rasterize outside of framebuffer

bounds

• Also, don’t waste time scan converting pixels

outside window

Page 5: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping

• The naïve approach to clipping lines:

for each line segment

for each edge of viewport

find intersection points

pick “nearest” point

if anything is left, draw it

• What do we mean by “nearest”?

• How can we optimize this?

Page 6: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Trivial Accepts

• Big optimization: trivial accept/rejects

• How can we quickly determine whether a line

segment is entirely inside the viewport?

• A: test both endpoints. xmin xmax

ymax

ymin

Page 7: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Trivial Rejects

• How can we know a line is outside viewport?

• A: if both endpoints on wrong side of same

edge, can trivially reject line

xmin xmax

ymax

ymin

Page 8: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Cohen-Sutherland Line Clipping

• Divide viewplane into regions defined by viewport

edges

• Assign each region a 4-bit outcode:

0000 0010 0001

1001

0101 0100

1000 1010

0110

xmin xmax

ymax

ymin

Page 9: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Cohen-Sutherland Line Clipping

• To what do we assign outcodes?

• How do we set the bits in the outcode?

• How do you suppose we use them?

0000 0010 0001

1001

0101 0100

1000 1010

0110

xmin xmax

ymax

ymin

Page 10: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Cohen-Sutherland Line Clipping

• Set bits with simple tests

x > xmax y < ymin etc.

• Assign an outcode to each vertex of line

– If both outcodes = 0, trivial accept

– bitwise AND vertex outcodes together

– If result 0, trivial reject

Page 11: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Cohen-Sutherland Line Clipping

• If line cannot be trivially accepted or rejected,

subdivide so that one or both segments can be

discarded

• Pick an edge that the line crosses (how?)

• Intersect line with edge (how?)

• Discard portion on wrong side of edge and assign

outcode to new vertex

• Apply trivial accept/reject tests; repeat if necessary

– Q: How did the programmer get stuck in the

shower?

– A: Lather, Rinse, Repeat

Page 12: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Outcode tests and line-edge intersects are quite fast

But some lines require multiple iterations:

– Clip top

– Clip left

– Clip bottom

– Clip right

• Fundamentally more efficient algorithms:

– Cyrus-Beck uses parametric lines

– Liang-Barsky optimizes this for upright volumes

Cohen-Sutherland Line Clipping

Page 13: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping in 2D and 3D

• We know how to clip a single line segment

• How about a polygon in 2D?

• How about in 3-D?

Page 14: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Polygons

• Clipping polygons is more complex than

clipping the individual lines

– Input: polygon

– Output: polygon, or nothing

• When can we trivially accept/reject a polygon

as opposed to the line segments that make up

the polygon?

Page 15: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• What happens to a triangle during clipping?

• Possible outcomes:

triangletriangle

Why Is Clipping Hard?

trianglequad triangle5-gon

• How many sides can a clipped triangle

have?

Page 16: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• A really tough case:

Why Is Clipping Hard?

Page 17: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• A really tough case:

Why Is Clipping Hard?

concave polygonmultiple polygons

Page 18: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Sutherland-Hodgman Clipping

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

Page 19: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 20: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 21: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 22: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 23: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 24: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 25: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 26: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully clipped

Sutherland-Hodgman Clipping

Page 27: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

• Basic idea:

– Consider each edge of the viewport individually

– Clip the polygon against the edge equation

– After doing all planes, the polygon is fully

clipped

• Will this work for non-rectangular clip

regions?

• What would 3-D

clipping involve?

Sutherland-Hodgman Clipping

Page 28: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Sutherland-Hodgman Clipping

• Input/output for algorithm: o Input: list of polygon vertices in order

o Output: list of clipped poygon vertices consisting of old

vertices (maybe) and new vertices (maybe)

• Note: this is exactly what we expect from

the clipping operation against each edge

Page 29: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Sutherland-Hodgman Clipping

• Sutherland-Hodgman basic routine: 1. Go around polygon one vertex at a time

2. Current vertex has position p

3. Previous vertex had position s, and it has been

added to the output if appropriate

Page 30: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Sutherland-Hodgman Clipping • Edge from s to p takes one of four cases:

(Green line can be a line or a plane)

inside outside

s

p

p output

inside outside

s

p

no output

inside outside

s p

i output

inside outside

s p

i output

p output

Page 31: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Sutherland-Hodgman Clipping

• Four cases: o s inside plane and p inside plane

• Add p to output

• Note: s has already been added

o s inside plane and p outside plane

• Find intersection point i

• Add i to output

o s outside plane and p outside plane

• Add nothing

o s outside plane and p inside plane

• Find intersection point i

• Add i to output, followed by p

Page 32: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Point-to-Plane test

• A very general test to determine if a point p is

“inside” a plane P, defined by q and n: (p - q) • n < 0: p inside P

(p - q) • n = 0: p on P

(p - q) • n > 0: p outside P

P

n p

q

P

n

p

q

P

n p

q

Page 33: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Point-to-Plane Test

• Dot product is relatively expensive o 3 multiplies

o 5 additions

o 1 comparison (to 0, in this case)

• Think about how you might optimize or

special-case this

Page 34: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Finding Line-Plane Intersections

• Use parametric definition of edge: E(t) = s + t(p - s)

o If t = 0 then E(t) = s

o If t = 1 then E(t) = p

oOtherwise, E(t) is part way from s to p

Page 35: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Finding Line-Plane Intersections

• Edge intersects plane P where E(t) is

on P o q is a point on P

o n is normal to P

(E(t) - q) • n = 0

t = [(q - s) • n] / [(p - s) • n]

• The intersection point i = E(t) for this

value of t

Page 36: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Line-Plane Intersections

• Note that the length of n doesn’t

affect result:

t = [(q - s) • n] / [(p - s) • n]

• Again, lots of opportunity for

optimization

Page 37: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

3-D Clipping

• Before actually drawing on the screen,

we have to clip (Why?)

• Can we transform to screen coordinates

first, then clip in 2-D? o Correctness: shouldn’t draw objects behind viewer

(what will an object with negative z coordinates do in

our perspective matrix?)

Page 38: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Recap: Perspective Projection • Recall the matrix:

• Or, in 3-D coordinates:

10100

0100

0010

0001

z

y

x

ddz

z

y

x

d

dz

y

dz

x,,

Page 39: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Under Perspective • Problem: after multiplying by a perspective

matrix and performing the homogeneous divide, a point at (-8, -2, -10) looks the same as a point at (8, 2, 10).

• Solution A: clip before multiplying the point by the projection matrix

oClip in camera coordinates

• Solution B: clip after the projection matrix but before the homogeneous divide

oClip in homogeneous screen coordinates

Page 40: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Under Perspective

• We will talk first about solution A:

Clip against

view volume

Apply projection

matrix and

homogeneous

divide

Transform into

viewport for

2-D display

3-D world

coordinate

primitives

Clipped

world

coordinates

2-D device

coordinates

Canonical

screen

coordinates

Page 41: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Recap: Perspective Projection • The typical view volume is a frustum or truncated pyramid

x or y

z

Page 42: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Perspective Projection

• The viewing frustum consists of six

planes

• The Sutherland-Hodgman algorithm

(clipping polygons to a region one plane

at a time) generalizes to 3-D

oClip polygons against six planes of view

frustum

oSo what’s the problem?

Page 43: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Perspective Projection • The viewing frustum consists of six planes

• The Sutherland-Cohen algorithm (clipping polygons

to a region one plane at a time) generalizes to 3-D o Clip polygons against six planes of view frustum

o So what’s the problem?

• The problem: clipping a line segment to an

arbitrary plane is relatively expensive

oDot products and such

Page 44: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Perspective Projection

• In fact, for simplicity we prefer to use the

canonical view frustum:

1

-1

x or y

z -1

Front plane

Back plane

Why is this going to be

simpler?

Page 45: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Perspective Projection

• In fact, for simplicity we prefer to use the

canonical view frustum:

1

-1

x or y

z -1

Front plane

Back plane

Why is the back plane

at z = -1, not z = 1?

Page 46: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Under Perspective

• So we have to refine our pipeline model:

• Note that this model forces us to separate

projection from modeling & viewing transforms

Apply

normalizing

transformation

projection

matrix;

homogeneous

divide

Transform into

viewport for

2-D display

3-D world

coordinate

primitives

2-D device

coordinates

Clip against

canonical

view

volume

Page 47: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Homogeneous Coords

• Another option is to clip the homogeneous

coordinates directly.

o This allows us to clip after perspective projection:

o What are the advantages?

Clip

against

view

volume

Apply

projection

matrix

Transform into

viewport for

2-D display

3-D world

coordinate

primitives

2-D device

coordinates

Homogeneous

divide

Page 48: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Advantages of Clipping

Homogeneous Coords

• Can transform the canonical view volume

for perspective projections to the canonical

view volume for parallel projections

o Clip in the latter (only works in homogeneous coords)

o Allows an optimized (hardware) implementation

• Some primitives will have w 1

o For example, polygons that result from tessellating

splines

o Without clipping in homogeneous coords, must perform

divide twice on such primitives

Page 49: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping Homogeneous Coords

• So how do we clip homogeneous coordinates?

• Briefly, thus:

o Remember that we have applied a transform to

normalized device coordinates

• x, y [-1, 1]

• z [0, 1]

oWhen clipping to (say) right side of the screen (x =

1), instead clip to (x = w)

Page 50: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Clipping: The Real World

• In some renderers, a common shortcut used to

be:

• But in today’s hardware, everybody just clips

in homogeneous coordinates

Projection

matrix;

homogeneous

divide

Clip in 2-D

screen

coordinates

Clip against

front and

back planes

Transform into

screen

coordinates

Page 51: Clipping - cs.boisestate.educs.boisestate.edu/~alark/cs464/lectures/Clipping.pdf · • When can we trivially accept/reject a polygon as opposed to the line segments that make up

Trivia

• Turing Award given annually by the Association for Computing Machinery to a person selected for technical contributions to the computing community.

• The contributions should be of lasting and major technical importance to the computer field.

• The award is named after Alan Turing (1912-1954), a British mathematician considered to be one of the fathers of modern computer science (remember Turing machines?)

• The Turing Award is sometimes called the "Nobel Prize of computing". It is sponsored by Intel Corporation and currently has a value of US $100,000.