53
March 1, 2009 Dr. Muhammed Al-Mulhem 1 ICS 415 Computer Graphics Clipping Dr. Muhammed Al-Mulhem Dr. Muhammed Al-Mulhem March 1, 2009 March 1, 2009

ICS 415 Computer Graphics Clipping

  • Upload
    vic

  • View
    30

  • Download
    3

Embed Size (px)

DESCRIPTION

ICS 415 Computer Graphics Clipping. Dr. Muhammed Al-Mulhem March 1, 2009. Transform. Illuminate. Transform. Clip. Project. Rasterize. Model & Camera Parameters. Rendering Pipeline. Framebuffer. Display. The Rendering Pipeline. Model World. World Camera. Why clip?. - PowerPoint PPT Presentation

Citation preview

Page 1: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 1

ICS 415Computer Graphics

Clipping

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem

March 1, 2009March 1, 2009

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem

March 1, 2009March 1, 2009

Page 2: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 2

The Rendering Pipeline

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

ModelWorld

WorldCamera

Page 3: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 3

Why clip?

We don’t want to waste time rendering objects that are We don’t want to waste time rendering objects that are outside the outside the viewing window (or clipping window)viewing window (or clipping window)

We don’t want to waste time rendering objects that are We don’t want to waste time rendering objects that are outside the outside the viewing window (or clipping window)viewing window (or clipping window)

Page 4: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 4

What is clipping?

Analytically calculating the portions of primitives within Analytically calculating the portions of primitives within the view windowthe view window

Analytically calculating the portions of primitives within Analytically calculating the portions of primitives within the view windowthe view window

Page 5: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 5

Clip to what?

View Window

Eye position(focal point)

Viewing Frustum

Page 6: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 6

Why illuminate before clipping?

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

ModelWorld

WorldCamera

Page 7: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 7

Why WorldCamera before clipping?

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

ModelWorld

WorldCamera

Page 8: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 8

Why?

Why illuminate before clipping?Why illuminate before clipping?

Why WorldWhy WorldCamera before clipping?Camera before clipping?

EfficiencyEfficiency

Why illuminate before clipping?Why illuminate before clipping?

Why WorldWhy WorldCamera before clipping?Camera before clipping?

EfficiencyEfficiency

Page 9: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 9

Clip to what?

View Window

Eye position(focal point)

Page 10: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 10

Why clip before project and rasterize?

Transform

Illuminate

Transform

Clip

Project

Rasterize

Model & CameraModel & CameraParametersParameters Rendering PipelineRendering Pipeline FramebufferFramebuffer DisplayDisplay

ModelWorld

WorldCamera

Page 11: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 11

Why Clip?

• Bad idea to rasterize outside of framebuffer bounds Bad idea to rasterize outside of framebuffer bounds

• Also, don’t waste time scan converting pixels Also, don’t waste time scan converting pixels outside windowoutside window

• Bad idea to rasterize outside of framebuffer bounds Bad idea to rasterize outside of framebuffer bounds

• Also, don’t waste time scan converting pixels Also, don’t waste time scan converting pixels outside windowoutside window

Page 12: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 12

Clipping

The naïve approach to clipping lines:The naïve approach to clipping lines:

for each line segmentfor each line segment

for each edge of view_windowfor each edge of view_window

find intersection pointfind intersection point

pick “nearest” pointpick “nearest” point

if anything is left, draw itif anything is left, draw it

What do we mean by “nearest”?What do we mean by “nearest”?

How can we optimize this?How can we optimize this?

The naïve approach to clipping lines:The naïve approach to clipping lines:

for each line segmentfor each line segment

for each edge of view_windowfor each edge of view_window

find intersection pointfind intersection point

pick “nearest” pointpick “nearest” point

if anything is left, draw itif anything is left, draw it

What do we mean by “nearest”?What do we mean by “nearest”?

How can we optimize this?How can we optimize this?A

B

CD

Page 13: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 13

Trivial Accepts

Big optimization: trivial accept/rejectsBig optimization: trivial accept/rejects

How can we quickly determine whether a line segment is How can we quickly determine whether a line segment is entirely inside the view window?entirely inside the view window?

A: test both endpoints.A: test both endpoints.

Big optimization: trivial accept/rejectsBig optimization: trivial accept/rejects

How can we quickly determine whether a line segment is How can we quickly determine whether a line segment is entirely inside the view window?entirely inside the view window?

A: test both endpoints.A: test both endpoints.

Page 14: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 14

Trivial Rejects

How can we know a line is outside view window?How can we know a line is outside view window?

A: if both endpoints on wrong side of A: if both endpoints on wrong side of samesame edge, can edge, can trivially reject linetrivially reject line

How can we know a line is outside view window?How can we know a line is outside view window?

A: if both endpoints on wrong side of A: if both endpoints on wrong side of samesame edge, can edge, can trivially reject linetrivially reject line

Page 15: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 15

Clipping Lines To View Window

Combining trivial accepts/rejectsCombining trivial accepts/rejects

• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the view windowinside all edges of the view window

• Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the view outside the same edge of the view windowwindow

• Otherwise, reduce to trivial casesOtherwise, reduce to trivial cases by splitting into two segmentsby splitting into two segments

Combining trivial accepts/rejectsCombining trivial accepts/rejects

• Trivially Trivially acceptaccept lines with both endpoints lines with both endpoints inside all edges of the view windowinside all edges of the view window

• Trivially Trivially reject reject lines with both endpoints lines with both endpoints outside the same edge of the view outside the same edge of the view windowwindow

• Otherwise, reduce to trivial casesOtherwise, reduce to trivial cases by splitting into two segmentsby splitting into two segments

Page 16: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 16

Cohen-Sutherland Line Clipping• Divide Divide view windowview window into regions defined by into regions defined by windowwindow edges edges

• Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode: Bit 1 indicates y-value of points are : Bit 1 indicates y-value of points are above yabove ymaxmax

• Divide Divide view windowview window into regions defined by into regions defined by windowwindow edges edges

• Assign each region a 4-bit Assign each region a 4-bit outcodeoutcode: Bit 1 indicates y-value of points are : Bit 1 indicates y-value of points are above yabove ymaxmax

0000 00100001

1001

0101 0100

1000 1010

0110

ymax

xmax

Page 17: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 17

Cohen-Sutherland Line Clipping

For each line segmentFor each line segment

• Assign an outcode to each vertex Assign an outcode to each vertex

• If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept

– Same as performing Same as performing if (bitwise OR = 0)if (bitwise OR = 0)

• ElseElse

– bitwise AND vertex outcodes togetherbitwise AND vertex outcodes together

– if result if result 0, trivial reject 0, trivial reject

For each line segmentFor each line segment

• Assign an outcode to each vertex Assign an outcode to each vertex

• If both outcodes = 0, trivial acceptIf both outcodes = 0, trivial accept

– Same as performing Same as performing if (bitwise OR = 0)if (bitwise OR = 0)

• ElseElse

– bitwise AND vertex outcodes togetherbitwise AND vertex outcodes together

– if result if result 0, trivial reject 0, trivial reject

Page 18: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 18

Cohen-Sutherland Line Clipping

• If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedthat one or both segments can be discarded

– Pick an edge of view window that the line crosses (Pick an edge of view window that the line crosses (how?how?))

– Intersect line with edge (Intersect line with edge (how?how?))

– Discard portion on wrong side of edge and assign new Discard portion on wrong side of edge and assign new outcode to new vertexoutcode to new vertex

– Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary

• If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedthat one or both segments can be discarded

– Pick an edge of view window that the line crosses (Pick an edge of view window that the line crosses (how?how?))

– Intersect line with edge (Intersect line with edge (how?how?))

– Discard portion on wrong side of edge and assign new Discard portion on wrong side of edge and assign new outcode to new vertexoutcode to new vertex

– Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary

Page 19: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 19

Cohen-Sutherland Line Clipping

If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedthat one or both segments can be discarded

Pick an edge that the line crossesPick an edge that the line crosses• Check against edges in same order each time Check against edges in same order each time

• ((For example: top, bottom, right, left)For example: top, bottom, right, left)

If line cannot be trivially accepted or rejected, subdivide so If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discardedthat one or both segments can be discarded

Pick an edge that the line crossesPick an edge that the line crosses• Check against edges in same order each time Check against edges in same order each time

• ((For example: top, bottom, right, left)For example: top, bottom, right, left)

A

B

D E

C

Page 20: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 20

Cohen-Sutherland Line Clipping

Intersect line with edge (Intersect line with edge (how?how?) – Chapter 6.) – Chapter 6.Intersect line with edge (Intersect line with edge (how?how?) – Chapter 6.) – Chapter 6.

A

B

D E

C

Page 21: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 21

Discard portion on wrong side of edge and assign outcode to new Discard portion on wrong side of edge and assign outcode to new vertexvertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Discard portion on wrong side of edge and assign outcode to new Discard portion on wrong side of edge and assign outcode to new vertexvertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Cohen-Sutherland Line Clipping

A

B

DC

Page 22: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 22

Discard portion on wrong side of edge and assign outcode to new Discard portion on wrong side of edge and assign outcode to new vertexvertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Discard portion on wrong side of edge and assign outcode to new Discard portion on wrong side of edge and assign outcode to new vertexvertex

Apply trivial accept/reject tests and repeat if necessary Apply trivial accept/reject tests and repeat if necessary

Cohen-Sutherland Line Clipping

A

B

C

Page 23: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 23

View Window Intersection Code

• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright

– yyintersectintersect = y = y11 + m(x + m(xrightright – x1) – x1)

where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom

– xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m – y1)/m

where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with vertical edge at x) intersect with vertical edge at xrightright

– yyintersectintersect = y = y11 + m(x + m(xrightright – x1) – x1)

where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))

• (x(x11, y, y11), (x), (x22, y, y22) intersect with horizontal edge at y) intersect with horizontal edge at ybottombottom

– xxintersectintersect = x = x11 + (y + (ybottombottom – y1)/m – y1)/m

where m=(ywhere m=(y22-y-y11)/(x)/(x22-x-x11))

Page 24: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 24

Cohen-Sutherland Review

• Use Outodes to quickly eliminate/include linesUse Outodes to quickly eliminate/include lines

– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common

• Must compute viewing window clipping of remaining linesMust compute viewing window clipping of remaining lines

– Non-trivial clipping costNon-trivial clipping cost

– Redundant clipping of some linesRedundant clipping of some lines

More efficient algorithms existMore efficient algorithms exist

• Use Outodes to quickly eliminate/include linesUse Outodes to quickly eliminate/include lines

– Best algorithm when trivial accepts/rejects are commonBest algorithm when trivial accepts/rejects are common

• Must compute viewing window clipping of remaining linesMust compute viewing window clipping of remaining lines

– Non-trivial clipping costNon-trivial clipping cost

– Redundant clipping of some linesRedundant clipping of some lines

More efficient algorithms existMore efficient algorithms exist

Page 25: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 25

Solving Simultaneous Equations

Equation of a lineEquation of a line

• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b

• Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0

• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11

– PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT

– x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t

– y(t) = yy(t) = y00 + (y + (y1 1 - y- y00) t) t

Equation of a lineEquation of a line

• Slope-intercept (explicit equation): y = mx + bSlope-intercept (explicit equation): y = mx + b

• Implicit Equation: Ax + By + C = 0Implicit Equation: Ax + By + C = 0

• Parametric Equation: Line defined by two points, PParametric Equation: Line defined by two points, P00 and P and P11

– PP(t) = (t) = PP00 + ( + (PP11 - - PP00) t, where ) t, where PP is a vector [x, y] is a vector [x, y]TT

– x(t) = xx(t) = x00 + (x + (x1 1 - x- x00) t) t

– y(t) = yy(t) = y00 + (y + (y1 1 - y- y00) t) t

Page 26: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 26

Parametric Line EquationDescribes a finite lineDescribes a finite line

Works with vertical lines (like the view window edge)Works with vertical lines (like the view window edge)

• 0 <=t <= 10 <=t <= 1

– Defines line between PDefines line between P00 and P and P11

• t < 0t < 0

– Defines line before PDefines line before P00

• t > 1t > 1

– Defines line after PDefines line after P11

Describes a finite lineDescribes a finite line

Works with vertical lines (like the view window edge)Works with vertical lines (like the view window edge)

• 0 <=t <= 10 <=t <= 1

– Defines line between PDefines line between P00 and P and P11

• t < 0t < 0

– Defines line before PDefines line before P00

• t > 1t > 1

– Defines line after PDefines line after P11

Page 27: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 27

Parametric Lines and Clipping

Define each line in parametric form: Define each line in parametric form:

• PP00(t)…P(t)…Pn-1n-1(t)(t)

Define each edge of view window in parametric form:Define each edge of view window in parametric form:

• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)

Perform Cohen-Sutherland intersection tests using Perform Cohen-Sutherland intersection tests using appropriate view window edge and lineappropriate view window edge and line

Define each line in parametric form: Define each line in parametric form:

• PP00(t)…P(t)…Pn-1n-1(t)(t)

Define each edge of view window in parametric form:Define each edge of view window in parametric form:

• PPLL(t), P(t), PRR(t), P(t), PTT(t), P(t), PBB(t)(t)

Perform Cohen-Sutherland intersection tests using Perform Cohen-Sutherland intersection tests using appropriate view window edge and lineappropriate view window edge and line

Page 28: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 28

Line / Edge Clipping EquationsFaster line clippers use parametric equationsFaster line clippers use parametric equations

Line 0:Line 0:• xx00 = x = x00

00 + (x + (x0011 - x - x00

00) t) t00

• yy00 = y = y0000 + (y + (y00

11 - y - y0000) t) t00

xx0000 + (x + (x00

11 - x - x0000) t) t0 = 0 = xxLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

yy0000 + (y + (y00

11 - y - y0000) t) t0 = 0 = yyLL

00 + (y + (yLL11 - y - yLL

00) t) tLL

• Solve for tSolve for t00 and/or t and/or tLL

Faster line clippers use parametric equationsFaster line clippers use parametric equations

Line 0:Line 0:• xx00 = x = x00

00 + (x + (x0011 - x - x00

00) t) t00

• yy00 = y = y0000 + (y + (y00

11 - y - y0000) t) t00

xx0000 + (x + (x00

11 - x - x0000) t) t0 = 0 = xxLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

yy0000 + (y + (y00

11 - y - y0000) t) t0 = 0 = yyLL

00 + (y + (yLL11 - y - yLL

00) t) tLL

• Solve for tSolve for t00 and/or t and/or tLL

View Window Edge L:View Window Edge L:• xxLL = x = xLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

• yyLL = y = yLL00 + (y + (yLL

11 - y - yLL00) t) tLL

View Window Edge L:View Window Edge L:• xxLL = x = xLL

00 + (x + (xLL11 - x - xLL

00) t) tLL

• yyLL = y = yLL00 + (y + (yLL

11 - y - yLL00) t) tLL

Page 29: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 29

Cyrus-Beck Algorithm

We wish to optimize line/line intersectionWe wish to optimize line/line intersection

• Start with parametric equation of line:Start with parametric equation of line:

– P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t

• And a point and normal for each edgeAnd a point and normal for each edge

– PPLL, N, NLL

We wish to optimize line/line intersectionWe wish to optimize line/line intersection

• Start with parametric equation of line:Start with parametric equation of line:

– P(t) = PP(t) = P00 + (P + (P1 1 - P- P00) t) t

• And a point and normal for each edgeAnd a point and normal for each edge

– PPLL, N, NLL

Page 30: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 30

Cyrus-Beck AlgorithmFind t such thatFind t such that

NNLL [P(t) - P [P(t) - PLL] = 0] = 0

Substitute line equation for P(t): Substitute line equation for P(t):

• NNLL [ [PP00 + (P + (P1 1 - P- P00) t) t - P - PLL] = 0] = 0

Solve for tSolve for t

• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]

Find t such thatFind t such that

NNLL [P(t) - P [P(t) - PLL] = 0] = 0

Substitute line equation for P(t): Substitute line equation for P(t):

• NNLL [ [PP00 + (P + (P1 1 - P- P00) t) t - P - PLL] = 0] = 0

Solve for tSolve for t

• t = Nt = NLL [P [PLL – P – P00] / -N] / -NLL [P [P11 - P - P00]]

PL

NL

P(t)Inside

P0

P1

Page 31: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 31

Cyrus-Beck Algorithm

Compute t for line intersection with all four edgesCompute t for line intersection with all four edges

Discard all (t < 0) and (t > 1)Discard all (t < 0) and (t > 1)

Classify each remaining intersection asClassify each remaining intersection as

• Potentially Entering (PE)Potentially Entering (PE)

• Potentially Leaving (PL)Potentially Leaving (PL)

NNLL [P [P11 - P - P00] > 0 implies PL] > 0 implies PL

NNLL [P [P11 - P - P00] < 0 implies PE] < 0 implies PE

Compute t for line intersection with all four edgesCompute t for line intersection with all four edges

Discard all (t < 0) and (t > 1)Discard all (t < 0) and (t > 1)

Classify each remaining intersection asClassify each remaining intersection as

• Potentially Entering (PE)Potentially Entering (PE)

• Potentially Leaving (PL)Potentially Leaving (PL)

NNLL [P [P11 - P - P00] > 0 implies PL] > 0 implies PL

NNLL [P [P11 - P - P00] < 0 implies PE] < 0 implies PE

Page 32: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 32

Compute PE with largest tCompute PE with largest t

Compute PL with smallest tCompute PL with smallest t

Clip to these two pointsClip to these two points

Cyrus-Beck Algorithm

PE

PLP1

PL

PE

P0

Page 33: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 33

Cyrus-Beck AlgorithmBecause of horizontal and vertical clip lines:Because of horizontal and vertical clip lines:

• Many computations reduceMany computations reduce

Normals: (-1, 0), (1, 0), (0, -1), (0, 1)Normals: (-1, 0), (1, 0), (0, -1), (0, 1)

Pick constant points on edgesPick constant points on edges

solution for t:solution for t:

• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))

• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))

• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))

• (y(y00 - y - ytoptop) / -(y) / -(y11 - y - y00))

Because of horizontal and vertical clip lines:Because of horizontal and vertical clip lines:• Many computations reduceMany computations reduce

Normals: (-1, 0), (1, 0), (0, -1), (0, 1)Normals: (-1, 0), (1, 0), (0, -1), (0, 1)

Pick constant points on edgesPick constant points on edges

solution for t:solution for t:

• -(x-(x00 - x - xleftleft) / (x) / (x11 - x - x00))

• (x(x00 - x - xrightright) / -(x) / -(x11 - x - x00))

• -(y-(y00 - y - ybottombottom) / (y) / (y11 - y - y00))

• (y(y00 - y - ytoptop) / -(y) / -(y11 - y - y00))

Page 34: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 34

ComparisonCohen-SutherlandCohen-Sutherland

• Repeated clipping is expensiveRepeated clipping is expensive

• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines

Cyrus-BeckCyrus-Beck

• Computation of t-intersections is cheapComputation of t-intersections is cheap

• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once

• Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects

• Best when many lines must be clippedBest when many lines must be clipped

Liang-Barsky: Optimized Cyrus-BeckLiang-Barsky: Optimized Cyrus-Beck

Nicholl et al.: Fastest, but doesn’t do 3DNicholl et al.: Fastest, but doesn’t do 3D

Cohen-SutherlandCohen-Sutherland

• Repeated clipping is expensiveRepeated clipping is expensive

• Best used when trivial acceptance and rejection is possible for most linesBest used when trivial acceptance and rejection is possible for most lines

Cyrus-BeckCyrus-Beck

• Computation of t-intersections is cheapComputation of t-intersections is cheap

• Computation of (x,y) clip points is only done onceComputation of (x,y) clip points is only done once

• Algorithm doesn’t consider trivial accepts/rejectsAlgorithm doesn’t consider trivial accepts/rejects

• Best when many lines must be clippedBest when many lines must be clipped

Liang-Barsky: Optimized Cyrus-BeckLiang-Barsky: Optimized Cyrus-Beck

Nicholl et al.: Fastest, but doesn’t do 3DNicholl et al.: Fastest, but doesn’t do 3D

Page 35: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 35

Clipping Polygons

• Clipping polygons is more complex than clipping the Clipping polygons is more complex than clipping the individual linesindividual lines

• Input: polygonInput: polygon

• Output: Original polygon, new polygon, or nothingOutput: Original polygon, new polygon, or nothing

• OptimizationOptimization• The biggest line optimizer we had was trivial accept or reject. The biggest line optimizer we had was trivial accept or reject.

• When can we trivially accept/reject a polygon as opposed to the When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?line segments that make up the polygon?

• Clipping polygons is more complex than clipping the Clipping polygons is more complex than clipping the individual linesindividual lines

• Input: polygonInput: polygon

• Output: Original polygon, new polygon, or nothingOutput: Original polygon, new polygon, or nothing

• OptimizationOptimization• The biggest line optimizer we had was trivial accept or reject. The biggest line optimizer we had was trivial accept or reject.

• When can we trivially accept/reject a polygon as opposed to the When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?line segments that make up the polygon?

Page 36: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 36

What happens to a triangle during clipping?What happens to a triangle during clipping?

Possible outcomesPossible outcomes::

What happens to a triangle during clipping?What happens to a triangle during clipping?

Possible outcomesPossible outcomes::

triangle triangle

Why Is Clipping Hard?

triangle quad triangle 5-gon

How many sides can a clipped triangle have?How many sides can a clipped triangle have?

Page 37: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 37

How many sides?

SevenSeven……SevenSeven……

Page 38: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 38

A really tough case: A really tough case: A really tough case: A really tough case:

Why Is Clipping Hard?

Page 39: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 39

A really tough case: A really tough case: A really tough case: A really tough case:

Why Is Clipping Hard?

concave polygon multiple polygons

Page 40: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 40

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the view window individuallyConsider each edge of the view window individually

• Clip the polygon against the view window edge’s equationClip the polygon against the view window edge’s equation

Basic idea:Basic idea:

• Consider each edge of the view window individuallyConsider each edge of the view window individually

• Clip the polygon against the view window edge’s equationClip the polygon against the view window edge’s equation

Page 41: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 41

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 42: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 42

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 43: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 43

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 44: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 44

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 45: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 45

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 46: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 46

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 47: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 47

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 48: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 48

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

Page 49: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 49

Sutherland-Hodgman Clipping

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Basic idea:Basic idea:

• Consider each edge of the viewport individuallyConsider each edge of the viewport individually

• Clip the polygon against the edge equationClip the polygon against the edge equation

• After doing all edges, the polygon is fully clippedAfter doing all edges, the polygon is fully clipped

Page 50: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 50

Sutherland-Hodgman Clipping

• Input/output for algorithm:Input/output for algorithm:

• Input: List of polygon vertices in order. Input: List of polygon vertices in order.

• Output: List of clipped polygon vertices consisting of Output: List of clipped polygon vertices consisting of old vertices (maybe) and new vertices (maybe).old vertices (maybe) and new vertices (maybe).

• Note: this is exactly what we expect from Note: this is exactly what we expect from the clipping operation against each edgethe clipping operation against each edge

• Input/output for algorithm:Input/output for algorithm:

• Input: List of polygon vertices in order. Input: List of polygon vertices in order.

• Output: List of clipped polygon vertices consisting of Output: List of clipped polygon vertices consisting of old vertices (maybe) and new vertices (maybe).old vertices (maybe) and new vertices (maybe).

• Note: this is exactly what we expect from Note: this is exactly what we expect from the clipping operation against each edgethe clipping operation against each edge

Page 51: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 51

Sutherland-Hodgman Clipping

Sutherland-Hodgman basic routine:Sutherland-Hodgman basic routine:

• Go around polygon one vertex at a timeGo around polygon one vertex at a time

• Current vertex has position Current vertex has position pp

• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriate.the output if appropriate.

Sutherland-Hodgman basic routine:Sutherland-Hodgman basic routine:

• Go around polygon one vertex at a timeGo around polygon one vertex at a time

• Current vertex has position Current vertex has position pp

• Previous vertex had position Previous vertex had position ss, and it has been added to , and it has been added to the output if appropriate.the output if appropriate.

Page 52: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 52

Sutherland-Hodgman Clipping

• Edge from Edge from ss to to pp takes one of four cases: takes one of four cases:

(Orange line can be a line or a plane)(Orange line can be a line or a plane)

• Edge from Edge from ss to to pp takes one of four cases: takes one of four cases:

(Orange line can be a line or a plane)(Orange line can be a line or a plane)

inside outside

s

p

p output

inside outside

s

p

no output

inside outside

sp

i output

inside outside

sp

i outputp output

Page 53: ICS 415 Computer Graphics  Clipping

March 1, 2009 Dr. Muhammed Al-Mulhem 53

Sutherland-Hodgman ClippingFour cases:Four cases:

• ss inside plane and inside plane and pp inside plane inside plane

– Add Add pp to output to output

– Note: Note: ss has already been added has already been added

• ss inside plane and inside plane and pp outside plane outside plane

– Find intersection point Find intersection point ii

– Add Add ii to output to output

• ss outside plane and outside plane and pp outside plane outside plane

– Add nothingAdd nothing

• ss outside plane and outside plane and pp inside plane inside plane

– Find intersection point Find intersection point ii

– Add Add ii to output, followed by to output, followed by pp

Four cases:Four cases:

• ss inside plane and inside plane and pp inside plane inside plane

– Add Add pp to output to output

– Note: Note: ss has already been added has already been added

• ss inside plane and inside plane and pp outside plane outside plane

– Find intersection point Find intersection point ii

– Add Add ii to output to output

• ss outside plane and outside plane and pp outside plane outside plane

– Add nothingAdd nothing

• ss outside plane and outside plane and pp inside plane inside plane

– Find intersection point Find intersection point ii

– Add Add ii to output, followed by to output, followed by pp