Chapter 9cs314/Vsep2005/lec_pdf/ch9.pdf · University of 4 British Columbia Analytic Solution...

Post on 27-Jun-2019

220 views 0 download

Transcript of Chapter 9cs314/Vsep2005/lec_pdf/ch9.pdf · University of 4 British Columbia Analytic Solution...

Clipping - 1

University ofUniversity ofBritish ColumbiaBritish Columbia

Chapter 9Clipping

2University ofUniversity ofBritish ColumbiaBritish Columbia

The Rendering Pipeline

GeometryDatabaseGeometryGeometryDatabaseDatabase

Model/ViewTransform.Model/ViewModel/ViewTransform.Transform. LightingLightingLighting Perspective

Transform.PerspectivePerspectiveTransform.Transform. ClippingClippingClipping

ScanConversion

ScanScanConversionConversion

DepthTest

DepthDepthTestTestTexturingTexturingTexturing BlendingBlendingBlending

Frame-buffer

FrameFrame--bufferbuffer

3University ofUniversity ofBritish ColumbiaBritish Columbia

Problem:Given a 2D line/polygon and a window, clip the line/polygon to their regions that are inside the window.

Problem:Given a 2D line/polygon and a window, clip the line/polygon to their regions that are inside the window.

Line/Polygon Clipping (2D)

Objectives• Efficiency• Memory access

4University ofUniversity ofBritish ColumbiaBritish Columbia

Analytic Solution

Intersection of convex regions is convex Why?

L & D are convex - intersection is convex

single connected segment of LQuestion: Can boundary of two convex shapes intersect more than twice?Clipping - compute intersection of Lwith four boundary segments of window D

D

L

D

L

D

L

University ofUniversity ofBritish ColumbiaBritish Columbia

Line-Line Intersection

( ) ( )( ) ( )

( ) ( )( ) ( )

Gx t x x x t

y t y y y tt G

x r x x x r

y r y y y rr1

101

11

01

101

11

01 2

202

12

02

202

12

02

0 1 0 1== + −

= + −

⎧⎨⎪

⎩⎪∈ =

= + −

= + −

⎧⎨⎪

⎩⎪∈[ , ] [ , ]

( )x y11

11,

( )x y01

01,

( )x y02

02,

( )x y12

12,

Γ1 Γ2

Intersection: x & y values equal in both representations - two linear equations in two unknowns (r,t)

( ) ( )( ) ( )

x x x t x x x r

y y y t y y y r

01

11

01

02

12

02

01

11

01

02

12

02

+ − = + −

+ − = + −

University ofUniversity ofBritish ColumbiaBritish Columbia

Intersection with vertical/horizontal lines

( ) ( )( ) ( )

( )( ) ( ) ]1,0[]1,0[

20

21

20

2

20

2

210

11

10

1

10

11

10

1

1 ∈⎪⎩

⎪⎨⎧

−+=

==∈

⎪⎩

⎪⎨⎧

−+=

−+== r

ryyyry

xrxGt

tyyyty

txxxtxG

( )x y11

11,

( )x y01

01,

( )x y02

02,

( )21

20 , yx

Γ1 Γ2

Intersection: x & y values equal in both representations - two linear equations in two unknowns (r,t)

( )

( ) ( )ryyytyyy

xxxxt

xtxxx

20

21

20

10

11

10

10

11

10

20

20

10

11

10

−+=−+

−−

=

=−+

7University ofUniversity ofBritish ColumbiaBritish Columbia

Purpose:Fast treatment of line segments that are trivially inside/outside window.

Purpose:Fast treatment of line segments that are trivially inside/outside window.

Cohen-Sutherland Algorithm

P = (x,y) - point to be classified against window D

Idea: Assign to P a binary code consisting of a bit for each edge of D, using lookup table:

0000

0101

0001

1001

0100

1000 1010

0010

0110

01

1234

bit

y y< min

y y> max

x x> maxx x< min

y y≥ miny y≤ max

x x≤ maxx x≥ min

8University ofUniversity ofBritish ColumbiaBritish Columbia

Cohen-Sutherland Algorithm (cont’d)

Why?

Given L from ),( to),( 1100 yxyx

If bitwise and of the codes of ( , ) ( , )x y x y0 0 1 1 and is not zero,

& rectangle D.

then L can be trivially handled (it is either totally outside or totally inside D).

or the bitwise or is zero,

0000

0101

0001

1001

0100

1000 1010

0010

0110

9University ofUniversity ofBritish ColumbiaBritish Columbia

end); , , , , , (

;,

; ofbit zero-nonleftmost ofboundary Windowelseend

); , , , , , (;,

; ofbit zero-nonleftmost ofboundary Windowbegin

then) ) ndow((OutsideWi if else;) , draw( then ) 0 ) or ( ( if

;return then ) 0 ! ) and ( ( if);( code );( code

) , , , ),,( ),,( (

maxminmaxmin20

102

1

maxminmaxmin12

102

0

0

1010

10

1100

maxminmaxmin111000

yyxxPPEdgePPP

CEdge

yyxxPPEdgePPP

CEdge

PPPCC

CCPCPC

yyxxyxPyxP

Clip-S-C

Clip-S-C

Clip-S-C

∩⇐

∩⇐

===

⇐⇐==

0000

0101

0001

1001

0100

1000 1010

0010

0110A

B

CD

E

AB CB DB DE

Cohen-Sutherland Algorithm (cont’d)

01

1234

bit

y y< min

y y> max

x x> maxx x< min

y y≥ miny y≤ max

x x≤ maxx x≥ min

10University ofUniversity ofBritish ColumbiaBritish Columbia

3D clipping

Determine portion of line inside axis-aligned parallelpiped (viewing frustum in NDC)Simple extension to 2D algorithmsAfter perspective transform

means that clipping volume always the samexmin=ymin= -1, xmax=ymax= 1 in OpenGL

boundary lines become boundary planesbut bit-codes still work the same wayadditional front and back clipping plane

zmin = -1, zmax = 1 in OpenGL

11University ofUniversity ofBritish ColumbiaBritish Columbia

Triangle Clipping

How does intersection of rectangle & triangle looks like?

How many sides?

How to expand clipping to triangles?Hint: it is convexWill develop on the board…

12University ofUniversity ofBritish ColumbiaBritish Columbia

end); , , , ,,...,,, ,.., (

;,

;,

; ofbit zero-nonleftmost ofboundary Window begin

then) ) ndow((OutsideWi ifn to1ifor else

;)poly draw( then ) 0 )or ...or or ( ( if;return then ) 0 ! ) and ... and and ( ( if

);( code n to1ifor ) , , , , ,..,poly (

maxminmaxmin11,,110

11,

1,1

10

10

maxminmaxmin0

yyxxPPPPPPEdgePPP

EdgePPP

CEdge

P

CCCCCC

PCyyxxPP

niiiiii

iiii

iiii

i

i

n

n

ii

n

++−−

++

−−

∩⇐

∩⇐

=

===

⇐==

Clip-S-C

Clip-S-C

Cohen-Sutherland Algorithm for convex polygons

01

1234

bit

y y< min

y y> max

x x> maxx x< min

y y≥ miny y≤ max

x x≤ maxx x≥ min