3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple...

84
Clipping קורס גרפיקה ממוחשבת2009/2010 סמסטר א' ליאור שפירא3D Polygon Rendering Pipeline שיעור6

Transcript of 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple...

Page 1: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping

קורס גרפיקה ממוחשבת'אסמסטר 2009/2010

ליאור שפירא

3D Polygon Rendering Pipeline

6שיעור

Page 2: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

3D Rendering Pipeline (for direct illumination)

3D Primitives

Modeling Transformation

Projection Transformation

Clipping

Lighting

Image

Viewport Transformation

Scan Conversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

Viewing Transformation

3D World Coordinates

2D Image Coordinates

Page 3: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

3D Rendering Pipeline (for direct illumination)

3D Primitives

Modeling Transformation

Projection Transformation

Clipping

Lighting

Image

Viewport Transformation

Scan Conversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

Viewing Transformation

3D World Coordinates

2D Image Coordinates

Page 4: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 5: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 6: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 7: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping Avoid drawing parts of primitives outside window ◦ Window defines part of scene being viewed

◦ Must draw geometric primitives only inside window

Viewing Window

Page 8: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping Avoid drawing parts of primitives outside window ◦ Points

◦ Lines

◦ Polygons

◦ Circles

◦ etc.

Viewing Window

Page 9: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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);

Page 10: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Line Clipping Find the part of a line inside the clip window

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Before Clipping

Page 11: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

P’8

P’7

P4 P3

P6

P’5 After Clipping

Line Clipping Find the part of a line inside the clip window

Page 12: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first

P1

P10

P9

P8

P7

P4 P3

P6

P5

P2

Page 13: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first

Clipping is performed by the computation of the intersections with four boundary segments of the window: Li, i=1,2,3,4 Purpose: Fast treatment of lines that are trivially inside/outside the window. Let P=(x,y) be a point to be classified against window W. Idea: Assign P a binary code consisting of a bit for each edge of W, whose value is determined according to the following table:

Page 14: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

bit 1 0

1 2 3

y < ymin y > ymax x > xmax

y ≥ ymin y ≤ ymax x ≤ xmax

4 x < xmin x ≥ xmin

0101

0001

0110 0100

1001

0010

1010

0000

1000 ymin

ymax

xmin xmax

1

2

3 4

Page 15: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

0101

0001

0110 0100

1001

0010

1010

0000

1000

Given a line segment S from p0=(x0,y0) to p1=(x1,y1) to be clipped

against a window W.

If code(p0) AND code(p1) is not zero - then S is trivially rejected.

If code(p0) OR code(p1) is zero - then S is trivially accepted.

Page 16: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

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

Page 17: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

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

Page 18: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

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

Page 19: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Classify some lines quickly by AND of bit codes

representing regions of two endpoints (must be 0)

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

Page 20: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 21: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 22: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 23: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 24: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 25: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 26: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 27: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 28: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 29: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 30: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 31: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 32: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 33: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 34: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cohen Sutherland Line Clipping Compute intersections with window boundary for lines

that can’t be classified quickly

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

Page 35: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

CompOutCode (x, y : real; var code : outcode); /* Compute outcode for the point (x,y) */ begin code := 0; if Y > Ymax then code := code | B1000 else if Y < Ymin then code := code | B0100; if X > Xmax then code := code | B0010 else if X < Xmin then code := code | B0001; end;

Cohen-Sutherland Algorithm

Page 36: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

CS (x0,y0,x1,y1,xmin,xmax,ymin,ymax) { boolean accept, done :; float outcode0, outcode1, x, y; accept := false ; done := false ; CompOutCod (x0,y0,outcode0); CompOutCod (x1,y1,outcode1); repeat if ((outcode0 | outcode1 ) == 0) { /* Trivial accept */ accept := true ; done := true ; } else if ((outcode0 & outcode1 ) <> 0){ /* Trivial reject */ done := true } else /* Failed both tests, so calculate the line segment to clip from an outside point to an intersection with clip edge */

Cohen-Sutherland Algorithm (cont.)

Page 37: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

/* At least one endpoint is outside the clip rectangle, pick it*/ if (outcode0 <> 0) { outcodeOut := outcode0 else outcodeOut := outcode1; /* now find the intrsection point by using the formulas: y= y0 + slope*(x-x0), and x = x0 + (1/slope)*(y-y0) */ if (outcodeOut & 0x1000) then divide line at top of clip rectangle; else if (outcodeOut & 0x0100) then divide line at bottom of clip rectangle; else if (outcodeOut & 0x0010) then divide line at right edge of clip rectangle; else if (outcodeOut & 0x0001) then divide line at left edge of clip rectangle;

Cohen-Sutherland Algorithm (cont.)

Page 38: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

/* Now we move outside point to intersection point to clip, and get ready for next pass */ if (outcodeOut == outcode0) { x0:=x;y0:=y;CompOutCod (x0,y0,outcode0); } else { x1:=x;y1:=y;CompOutCod (x1,y1,outcode1); } } /* Subdivide */ until (done) ; if (accept) draw_line (x0,y0,y0,y1); } /* end */

Cohen-Sutherland Algorithm (cont.)

Page 39: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 40: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Vector Calculus - Preliminaries

A 2D vector V is defined as: V=(Vx,Vy).

Scalar (dot) product between two vectors V and U is defined:

If then V and U are perpendicular to each other.

xx y

y

x x y y

UV U V V

U

V U V U V cosU θ

⋅ = =

+ =

0=⋅UV

Page 41: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

P0

P1

P1 - P0

Vector Subtraction

P0 P1

Vector Addition

Page 42: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

P0=P(t=0)

P1=P(t=1)

Parametric Line

P(t)=P0+(P1-P0)t

V(t=0)

V(t=1)

Changing the Origin

V(t)=P(t)-Q

Q

Page 43: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Inside/Outside Test

Assume WLOG that V=(V1-V0) is the border vector where "inside" is to its right.

If V=(Vx,Vy), N is a prep' vector pointing outside, where we define:

N=(-Vy,Vx)

Vector U points "outside" if

Otherwise U points "inside".

V0

V1 inside

N

U

0>⋅UN

Page 44: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Segment-Line Intersection

The parametric line P(t)=P0+(P1-P0)t

The parametric line V(t)=P(t)-Q

The segment intersects the line L at t0 satisfying V(t0)·N=0.

The intersection point is P(t0).

The vector ∆=P1-P0 points "inside" if (P1-P0)·N<0.

Otherwise it points "outside".

If L is vertical, intersection can be computed using the explicit equation.

P0

inside

N Q

P1 V(t)

L

Page 45: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cyrus-Beck Line Clipping

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).

Ni· V(t) = 0 iff V(t) ⊥ Ni

We are looking for t satisfying the above equation:

p0

p1

Ni Qi

Page 46: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cyrus-Beck Clipping (cont.)

0 = Ni· V(t) = Ni· (p(t)-Qi)

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

Solving for t we get:

where ∆=(p1-p0)

• Comment: If Ni· ∆=0, t has no solution. However, in this case V(t) ⊥ Ni and there is no intersection.

Ni· (p0-Qi) -Ni· (p1-p0) t =

Ni· (p0-Qi) -Ni· ∆

=

Page 47: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

p0

p1

PE PE

PL

PL

potentially Entering and potentially Leaving

Page 48: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Cyrus-Beck Algorithm: The intersection of p(t) with all four edges Li is computed,

resulting in up to four ti values.

If ti<0 or ti>1 , ti can be discarded.

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.

The domain, if inverted, signals that p(t) is totally outside.

Page 49: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

p1

p0

PL

PE

p0

p1

PE PE

PL

PL

p1 PL

PE p0

Page 50: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

precalculate Ni and select a Pei for each edge; for each line segment to be clipped if ( P1 = P2 ) then line is degenerate so clip as a point; else { tPE = 0; tPL = 1; for each candidate intersection with a clip edge if (( <Ni, D> ) <> 0 ) then { /* Ignore edges parallel to line for now */ calculate t; sign of <Ni, D> categorizes as PE or PL ; if PE then tPE = max (tPE, t); if PL then tPL = min (tPL, t); } if (tPE > tPL) return null else return P(tPE) and P(tPL) ; /* as true clip intersections */ };

Cyrus-Beck Line Clipping

Page 51: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Polygon Clipping

Page 52: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping Avoid drawing parts of primitives outside

window ◦ Points ◦ Lines ◦ Polygons ◦ Circles ◦ etc.

2D Screen Coordinates

Window

Page 53: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Polygon Clipping Find the part of a polygon inside the clip

window?

Before Clipping

Page 54: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Polygon Clipping Find the part of a polygon inside the clip

window?

After Clipping

Page 55: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 56: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 57: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 58: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 59: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 60: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 61: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the
Page 62: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Sutherland Hodgeman Clipping Clip to each window boundary one at a time

After each clipping a new set of vertices is produced.

Page 63: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Sutherland Hodgeman Clipping Clip to each window boundary one at a time

Page 64: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Sutherland Hodgeman Clipping Clip to each window boundary one at a time

Page 65: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Sutherland Hodgeman Clipping Clip to each window boundary one at a time

Page 66: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Sutherland Hodgeman Clipping Clip to each window boundary one at a time

Page 67: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

Page 68: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

Page 69: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

Page 70: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

Page 71: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

P’

Page 72: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

P’

Page 73: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

P’

Page 74: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P5

P4

P3

P’ P’’

Page 75: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Clipping to a Boundary Do inside test for each point in sequence,

Insert new points when cross window boundary, Remove points outside window boundary

Outside Inside

Window Boundary

P1 P2

P’ P’’

Page 76: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

For each clip edge - consider the relation between successive vertices of the polygon; Assume vertex s has been dealt with, vertex p follows:

clip boundary

inside outside s

p clip boundary

inside outside

s

p

clip boundary

inside outside p s i

s clip boundary

inside outside

p i

p added to output list

i added to output list no output i and p added to

output list

Page 77: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

type vertex = point; /* point holds real x, y */ edge = array [1..2] of vertex; /* Max declared as constant */ vertexArray = array [1..MAX] of vertex; procedure SutherlandHodgmanPolygonClip ( inVertexArray: vertixArray; /* input vertex array */ var outVertexArray: vertexArray; /*output vertex array */ inLength: integer; /* num of entries in inVertexArray */ var outLength:integer; /*num of entries in outVertexArray */ clipBoundary : edge /* Edge of clip polygon */ );

Sutherland - Hodgman polygon Cliping Algoruthm

Page 78: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

var s, p /* Start, End point of current polygon edge */ i : vertix; /* Intersection point with clip boundary */ j : integer; /* vertex loop counter */ begin outLength :=0; s := inVertexArray [inLength]; /* Start with the kast vertex in inVertexArray*/ for j:=1 to inLength do begin p := inVertexArray[j ]; if Inside (p, clipBoundary ) then if Inside (s, clipBoundary ) then Output (p,outLength, outVertexArray) /*case #1*/ else begin /* case # 4 */ Intersect (s, p, clipBoundary, i); Output (i, outLength, outVertexArray ); Output (p, outLength, outVertexArray ); end else if Inside (s, clipBoundary ) then begin /* case # 2 */ Intersect (s, p, clipBoundary, i ); Output (i, outLength, outVertexArray ); end; s := p ; /* Advance to next pair of vertices */ end /* for */ end; /* SutherlandHodgmanPolygonClip */

Sutherland - Hodgman (cont.)

Page 79: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 80: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Viewport Transformation Transform 2D geometric primitives from

screen coordinate system (normalized device coordinates) to image coordinate system (pixels)

Image Screen

Viewport

Window

Page 81: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Viewport Transformation Window-to-viewport mapping

vx1 vx2 vy1

vy2

wx1 wx2 wy1

wy2 Window Viewport

Screen Coordinates Image Coordinates

(wx,wy) (vx,vy)

vx = vx1 + (wx - wx1) * (vx2 - vx1) / (wx2 - wx1); vy = vy1 + (wy - wy1) * (vy2 - vy1) / (wy2 - wy1);

Page 82: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 83: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

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

Page 84: 3D Polygon Rendering Pipelinedcor/Graphics/pdf.slides/...Cohen-Sutherland Line Clipping Use simple tests to classify easy cases first Clipping is performed by the computation of the

Summary 3D Primitives

Modeling Transformation

Projection Transformation

Clipping

Lighting

Image

Viewport Transformation

Scan Conversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

Viewing Transformation

3D World Coordinates

2D Image Coordinates

Viewing Window