clipping - lecture 10 sections 7.4 - 7people.hsc.edu/faculty-staff/robbk/coms331/lectures/lectures...

Post on 24-May-2020

6 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

ClippingLecture 10

Sections 7.4 - 7.7

Robb T. Koether

Hampden-Sydney College

Fri, Sep 18, 2009

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Outline

1 ClippingPointsLinesPolygons

2 The Cohen-Sutherland Algorithm

3 Assignment

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping

An object whose coordinates are not entirely within therange of the clipping frustum must be clipped.That is, the part that is outside the frustum must beeliminated before the object is drawn.Why is it necessary to clip?

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping

There are three types of object to clip.PointsLinesPolygons

We will study a simple case of clipping lines in detail.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Points

Clipping points is simpler than clipping lines.If the point is outside the frustum, clip it.If the point is inside the frustum, do not clip it.

This is done in clip coordinates where the point(x, y, z, w) is inside the frustum if and only if

−w ≤ x ≤ w−w ≤ y ≤ w−w ≤ z ≤ w

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Points

P(x, y, z, w)

Q(x, y, z, w)

(f, f, f, f)

(n, -n, -n, n)

(n, n, -n, n)

(f, -f, f, f)

Frustum, right side, in clip coordinates.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Lines

If a line is partially in the frustum, then we need torecalculate its endpoints.There are four possible cases.

The line is entirely within the frustum.The line is entirely outside the frustum.One endpoint is in and the other is out.Both endpoints are out, but the middle part is in.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Lines

B

A

C

E

D

F

H

G

Before clipping

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Lines

B

A

C

E

D

F

H

G

After clipping

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clipping polygons requires that their edges be clippingby the line-clipping algorithm, and then new edges becreated.Keep in mind that OpenGL treats all polygons asthough they were convex.How does this make clipping more efficient?

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

PolygonClip Region

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the right edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the right edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the top edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the top edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the left edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the left edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the bottom edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

Clip the bottom edge

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

The clipped polygon

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Polygons

When an edge is clipped, a shade is assigned to thenew vertex by interpolating between the endpoints.These interpolated shades are used when the interiorof the polygon is shaded.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(5, -2)

(2, 3)

(-6, 0)

(-5, -2)

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(5, -2)

(2, 3)

(-6, 0)

(-5, -2)

[1, 1, 0]

[0, 1, 0]

[0, 0, 1]

[1, 0, 0]

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(5, -2)

(2, 3)

(-6, 0)

(-5, -2)

[1, 1, 0]

[0, 1, 0]

[0, 0, 1]

[1, 0, 0]

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(5, -2)

(2, 3)

(-6, 0)

(-5, -2)

[1, 1, 0]

[0, 1, 0]

[0, 0, 1]

[1, 0, 0]

(-1, 3) (1.5, 3)

(-4, 1)

(-4, -2) (4, -2)

(4, 0)

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(5, -2)

(2, 3)

(-6, 0)

(-5, -2)

[1, 1, 0]

[0, 1, 0]

[0, 0, 1]

[1, 0, 0]

(-1, 3) (1.5, 3)

(-4, 1)

(-4, -2) (4, -2)

(4, 0)

[1, 0.25, 0]

[1, 0.75, 0][0.83, 1, 0]

[0.33, 1, 0]

[0, 0.9, 0.1][0, 0.1, 0.9]

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Interpolating the Clipped Polygon

-4

3

-34

(-1, 3) (1.5, 3)

(-4, 1)

(-4, -2) (4, -2)

(4, 0)

[1, 0.25, 0]

[1, 0.75, 0][0.83, 1, 0]

[0.33, 1, 0]

[0, 0.9, 0.1][0, 0.1, 0.9]

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

Let the x-coordinates of the window boundaries bexmin and xmax and let the y-coordinates be ymin andymax.

ymin

ymax

xmaxxmin

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

For each point P we will define a codeword c3c2c1c0

consisting of 4 true/false values (bits).c3 = true if P is left of the window.c2 = true if P is above the window.c1 = true if P is right of the window.c0 = true if P is below the window.

How many different values are possible for a codeword?

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

How many different combinations of values are possiblefor the codewords of two endpoints of a segment?How do we compute a codeword?

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

Initialize the codeword to 0.Consider the vertical edge x = xmin.Compare the x-coordinate of P to xmin.If it is less than xmin, then “set” bit 3 of the codeword.if (p.x < xmin) codeword = 1 << 3;

Similar calculations comparing to ymax, xmax, andymin will set bits 2, 1, and 0, respectively.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

After the codewords for both endpoints are computed,we divide the possibilities into three cases:

Trivial accept - both codewords are FFFF.Trivial reject - both codewords have T in the sameposition.Non-trivial - Investigate further.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

What is the quickest way to separate these threecases?Use bitwise “and” and “or.”If codeword1 | codeword2 == 0, then we triviallyaccept.If codeword1 & codeword2 != 0, then we triviallyreject.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

What about the third case?We clip against each edge of the window, in sequence,as necessary.After clipping against an edge, we may test again fortrivial accept or trivial reject before moving on to thenext edge.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clip

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clip

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clip

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clip

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

ymin

ymax

xmaxxmin

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

This raises some questions?What is the worst case?What is the best case?How do we decide whether to clip against a particularedge?If we do clip, how to we determine the new endpoint?

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

Consider again the vertical edge x = xmin. (You cando the other edges.)Compute codeword1 ˆ codeword2.This shows where they disagree.“And” this with 1 << 3.If the result is not 0, then clip.Otherwise do not clip.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

If we clip, then the new point will be on the linex = xmin.So its x-coordinate will be xmin.We must calculate its y-coordinate.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

x = xmin

P

Q

R

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

x = xmin

P

Q

R

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

x = xmin

P

Q

R

Q.x - P.xR.x - P.x

Q.y - P.y

R.y - P.y

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

The Cohen-Sutherland Clipping Algorithm

Then, by similar triangles,

R.y − P.y

Q.y − P.y=

xmin− P.x

Q.x− P.x.

Therefore,

R.y = P.y + (Q.y − P.y)(

xmin− P.x

Q.x− P.x

).

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Clipping Lines

Example (Clipping Lines)The code.The executable.

Clipping

Robb T.Koether

ClippingPoints

Lines

Polygons

The Cohen-SutherlandAlgorithm

Assignment

Homework

HomeworkRead Section 7.4 – Cohen-Sutherland clipping.Read Section 7.5 – polygon clipping.Read Section 7.6 – clipping other objects.Read Section 7.7 – 3D clipping.

top related