Clipping - cs.bgu.ac.ilgraph161/wiki.files/09b-Clipping.pdf · Image Clipping Scan Conversion Draw...

Preview:

Citation preview

Clipping

3D Rendering Pipeline 3D Geometric Primitives

Modeling Transformation

Viewing Transformation

Projection Transformation

Lighting

Image

Clipping

Scan Conversion Draw pixels (includes texturing, hidden surface, etc.)

Clip primitives outside camera’s view

Transform into 2D camera coordinate system

Transform into 3D camera coordinate system

Transform into 3D world coordinate system

Illuminate according to lighting and reflectance

2D Rendering Pipeline

Viewport Transformation

Scan Conversion

Clipping

2D Primitives

Image

Clip portions of geometric primitives

residing outside the window

Fill pixels representing primitives

in screen coordinates

Transform the clipped primitives

from screen to image coordinates

3D Primitives

Clipping

Avoid drawing parts of primitives outside window

Window defines part of scene being viewed

Must draw geometric primitives only inside window

Screen Coordinates

Window

Clipping

Avoid drawing parts of primitives outside window

Window defines part of scene being viewed

Must draw geometric primitives only inside window

Viewing

Window

Clipping

Avoid drawing parts of primitives outside window

Points

Lines

Polygons

Circles

etc.

Viewing

Window

Point Clipping

Is point (x,y) inside the clip window?

Window

wx1 wx2

wy2

wy1

(x,y)

inside =

(x >= wx1) &&

(x <= wx2) &&

(y >= wy1) &&

(y <= wy2);

Line Clipping

Find the part of a line inside the clip

window

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Before Clipping

P’8

P’7

P4 P3

P6

P’5

After Clipping

Line Clipping

Find the part of a line inside the clip

window

Cohen Sutherland Line Clipping

Use simple tests to classify easy cases

first

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Key Idea: Encode Regions

Bit

Value

0 1

xxmin x<xmin 1

xxmax x>xmax 2

yymin y<ymin 3

yymax y>ymax 4

Endpoint Code Classification

Assign code to end points of each line segment

line is inside if (c1 OR c2 ==0)

line is outside if )c1 AND c2 != 0)

Remove all such lines

Compute intersections with window boundary for all

lines that can’t be classified quickly

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P10

P9

P’8

P’7

P4 P3

P6

P’5

Cohen Sutherland Line

Clipping

Bit 1 Bit 2

Bit 3

Bit 4

0000 0100 1000

0001 0101 1001

0010 0110 1010

P’8

P’7

P4 P3

P6

P’5

Intersecting An Edge and a Line

Denote p(t)=p0+(p1-p0)t t[0..1]

Let qi be a point on the edge Li with outside pointing normal Ni.

V(t) = p(t)-qi is a parameterized vector from qi to the segment p(t).

We are looking for t satisfying the equation: Ni· V)t) = 0

p0

p1

Ni

Li

p(t) V(t)

qi

Finding the Parameter t of the

Intersection Point

0 = Ni· V)t)

= Ni· )p)t)-qi)

= Ni· )p0+(p1-p0)t-qi)

= Ni· )p0-qi) + Ni· )p1-p0)t

p0

p1

Ni qi

Li

)( where

)(

)(

)(

01

0

01

0

pp

N

qpN

ppN

qpNt

i

ii

i

iii

p(ti)

V(ti)

t at intersection

Cyrus-Beck Line Clipping Example

p1

p0

PL

PE

p0

p1

PE

PE

PL

PL

p1

PL

PE p0

Line is outside

Cyrus-Beck Line Clipping

The intersection of p(t) with all four edges Li is computed,

resulting in up to four ti values per segment.

If ti<0 or ti>1 , ti can be discarded (why?).

Based on the sign of Ni· , each intersection point is classified

as PE (potentially entering) or PL (potentially leaving).

PE with the largest t and PL with the smallest t provide the

domain of p(t) inside W.

If PL comes before PE the line is outside!

Note: If Ni· =0, t has no solution. However, in this case V(t)

Ni and there is no defined intersections.

Polygon Clipping

Find the part of a polygon inside the clip

window? Before Clipping

Polygon Clipping

Find the part of a polygon inside the clip

window? After Clipping

Naïve Clipping

Find intersections

Naïve Clipping

Find intersections

Remove all outside vertices

Sutherland Hodgeman

Clipping

Clip to each window boundary one at a time

Sutherland Hodgeman

Clipping

Clip to each window boundary one at a time

Sutherland Hodgeman

Clipping

Clip to each window boundary one at a time

Sutherland Hodgeman

Clipping

Clip to each window boundary one at a time

Sutherland Hodgeman

Clipping

Clip to each window boundary one at a time

Clipping to a Boundary

Assumes you have vertices ordered!

Loop around polygon:

Do inside test for each vertex in polygon

sequence

When crossing window boundary insert

new point

Remove points outside window boundary

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

P’

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

P’

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

P’

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P5

P4

P3

P’ P’’

Clipping to a Boundary

Outside

Inside

Window

Boundary

P1 P2

P’ P’’

Relation Between Successive

Vertices

clip boundary

inside outside u

v

clip boundary

inside outside

u

v

clip boundary

inside outside v u w

u

clip boundary

inside outside

v w

v added to output list

w added to output list no output w and v added to

output list

Assume vertex u has been dealt with, vertex v follows:

Sutherland Hodgeman

Clipping

Assumes you have vertices ordered in List L

Initialize: Lin = L, Lout =

For each window edge e {

u = last(Lin)

Loop over v Lin {

If v is inside e then

if u is outside e

then Lout = Lout intersect(e,<u,v>)

Lout = Lout v

Else

if u is inside e

then Lout = Lout intersect(e,<u,v>)

u = v

}

Lin = Lout

}

e

Notes

Works for any convex clipping polygon

Good for drawing – not for modeling:

Overlapping edges

2D Rendering Pipeline

Viewport Transformation

Scan Conversion

Clipping

2D Primitives

Image

Clip portions of geometric primitives

residing outside the window

Fill pixels representing primitives

in screen coordinates

Transform the clipped primitives

from screen to image coordinates

3D Primitives

Viewport Transformation

Transform 2D geometric primitives from

screen coordinate system (normalized device

coordinates) to image coordinate system (pixels)

Image

Screen

Viewport

Window

Window-to-Viewport Mapping

vx1 vx2 vy1

vy2

wx1 wx2 wy1

wy2 Window Viewport

Screen Coordinates Image Coordinates

(wx,wy) (vx,vy)

)(

)(

1

12

12

1

1

12

121

yy

yy

yy

yy

xx

xx

xxxx

wwww

vvvv

wwww

vvvv

Summary

Viewport Transformation

Scan Conversion

Clipping

2D Primitives

Image

Clip portions of geometric primitives

residing outside the window

Fill pixels representing primitives

in screen coordinates

Transform the clipped primitives

from screen to image coordinates

3D Primitives

Summary of Transformations

Modeling Transformation

Viewing Transformation

2D Image Coordinates

Projection Transformation

Window-to-Viewport Transformation

3D Object Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

p(x,y,z)

p’)x’,y’)

Viewing transformations

Modeling transformation

Viewport transformation

Recommended