42
Informationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines Intersecting lines and planes

Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Embed Size (px)

Citation preview

Page 1: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 1

Today’s classClippingParametric and point-normal form of linesIntersecting lines and planes

Page 2: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 2

Normalized device coordinates

Graphics imaging is device dependentImage development is device independentTo simplify the process images are frequently generated using normalized device coordinates(NDC), where x and y values are between -1.0 and 1.0 (for OpenGL; other systems may use 0.0 to 1.0)

Page 3: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 3

Converting NDC to DCTo display an image a transformation from NDC to device coordinates (DC) needs to take placeTo convert NDC to DC:

multiply by ½ the number of pixels in the dimensionadd ½ the number of pixels in the dimension

Page 4: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 4

Viewing transformationIt is usually inconvenient to represent an image in DC or NDCWant to do it in world coordinates (WC)Setting the window and the viewport accomplishes the transformation from WC to DC, thus providing the viewing transformation

Page 5: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 5

Clipping volumeThe combination of setting the window and the viewport defines the clipping volumeIt is possible some primitives lie wholly or partially outside this volumeNeed to insure these items are not displayedThis process is known as clipping

Page 6: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 6

Clipping pointsClipping points is trivialA point is visible if

xmin ≤ x ≤ xmax

ymin ≤ y ≤ ymax

Page 7: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 7

Clipping linesLines lie:

wholly inside windowwholly outside windowpartially inside and partially outside window

Define a 4-bit outcode (b0b1b2b3) describing relationship of line segment to window

b0 = 1 if y > ymax (point above window)b1 = 1 if y < ymin (point below window)b2 = 1 if x > xmax (point to right of window)b3 = 1 if x < xmin (point to left of window)

Page 8: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 8

Cohen-Sutherland OutcodeAlgorithm

code1 ← outcode (p1); code2 ← outcode (p2);loop

if code1 = 0 and code2 = 0 thendisplay line segment; exit loop;

if code1 & code2 ≠ 0 thenreject line segment; exit loop;

if code1 = 0 thenswap (p1, p2); swap (code1, code2);

find a nonzero bit in code1find intersection of line with corresponding window boundary;p1 ← intersection point; code1 ← outcode (p1);

Page 9: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 9

ObservationsClipping of one line segment may occur more than onceDon’t want to change the slope of the lineTherefore, do the work in floating point numbersWorks well when explicit form of line equation is used

Page 10: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 10

Vertical linesVertical lines have an infinite slopeWill this cause a problem for the Cohen-Sutherland algorithm?Pair off and determine the answer to the above question.

If no, great!If yes, how can you overcome the problem?

Page 11: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 11

Vertical lines and Cohen-Sutherland clipping

ypy

xx ytop

Δ

−=

Δ1

top

p1

p2

xΔx

Δy

)( 1 ytop pyyxx −

ΔΔ

=

For a vertical line Δx=0, but you’ll never divide by it!

Page 12: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 12

Parametric form of a lineDescribes a “travelling” motion along a line or line segmentThe x and y coordinates of points on the line are described by linear equations involving a parameter variable (frequently t, u, or v)Example:

x = ty = 2t + 1

Page 13: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 13

General formIn general, the parametric form for the equation of a line is

x = at + by = ct + d

Page 14: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 14

Line segmentsFrequently, in computer graphics we restrict t by 0 ≤ t ≤ 1This generates a line segment

Page 15: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 15

Finding the parametric form of a line segment

Given two endpoints, (x1, y1) and (x2, y2)Let t = 0 be at (x1, y1) and t = 1 be at (x2, y2)Then:

x = x1 + (x2 - x1)ty = y1 + (y2 - y1)t

Page 16: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 16

Vector form of parametric equation

Let the two endpoints of a line segment be denoted A and BThe line between them is described by

P(t) = A + (B - A)t 0 ≤ t ≤ 1

Page 17: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 17

Slope vectorThe slope of a line is (y2-y1)/(x2-x1)The slope vector is defined as the vector (x2-x1, y2-y1)Note that the slope vector’s components are the coefficients of the linear terms of the parametric equations

Page 18: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 18

Perpendicular linesSlopes are negative reciprocals of each otherGiven a line with a slope of (y2-y1)/(x2-x1), which implies a slope vector of (x2-x1, y2-y1), a perpendicular line will have slope -(x2-x1)/(y2-y1) and a slope vector (-(y2-y1), (x2-x1))To get the equation of the perpendicular line choose the midpoint (mx, my) of the line to base it fromParametric form is now

x(t) = mx - (y2 - y1)ty(t) = my + (x2 - x1)t

Page 19: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 19

Point normal formn • r = D, where n is the normal to the line, r represents all points on the line, and D = n • A, where A is any point on the line

A

c

R

n

n • (R - A) = 0

Page 20: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 20

Half spacesA line divides space into two halves, an outside half space and an inside half spaceGiven a line through point A and outward normal n, then any point Q lies

in the outside half space if (Q-A)•n > 0on the line if (Q-A)•n = 0in the inside half space if (Q-A)•n < 0

Page 21: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 21

Intersections of lines with lines and planes

Let a ray be represented by B+ctBoth lines and planes are represented in point normal form by n ∙ (R – A) = 0 or n ∙ R = DTo find the intersection, substitute the ray equation into the point normal form for R:

n ∙ (B+ct) = DSolve for t, which we will designate thit, the time at which the ray hits the line or plane:

thit = (D - n ∙ B) / (n ∙ C)

Page 22: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 22

Intersecting a ray with a polygon

We frequently need to answer the questions

Where does a ray hit an object?What part of a line is inside an object?

We can solve these problems by determining the intersections of a ray with a polygon

Page 23: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 23

Two intersectionsLet the ray in question be denoted B + ctIt will hit the boundary of a convex polygon at most twice, once upon entering the polygon and once upon leavingCall these times tin and tout

Need to find intersection of ray with each edge and determine portion within polygon

Page 24: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 24

Entering or leaving?When the ray intersects an edge, we need to decide if it is entering or leaving the polygonLet n represent the normal to an edgeThe following conditions hold:

If n ∙ c < 0 the ray is enteringIf n ∙ c = 0 the ray is parallelIf n ∙ c > 0 the ray is leaving

Page 25: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 25

Times to keep For entering times, want to keep the largest intersection time (or 0 if the ray starts inside the polygon)For leaving times, want to keep the smallest intersection timeHow would this be modified if we consider a line segment intersecting the polygon rather than a ray?

Page 26: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 26

What’s left?The ray between tin and tout is the part inside the polygonThe rest is clipped

Page 27: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 27

Liang-Barksy clippingUseful when clipping against a rectangular windowUses parametric form of line equation

x(t) = x1 + t(x2-x1) = x1 + tΔxy(t) = y1 + t(y2-y1) = y1 + tΔy0 ≤ t ≤ 1

Clipping conditions becomexmin ≤ x1 + tΔx ≤ xmax

ymin ≤ y1 + tΔy ≤ ymax

Page 28: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 28

Inequality formEach of the four inequalities is of the form tpk ≤ qk, where

p1 = -Δx, q1 = x1-xmin

p2 = Δx, q2 = xmax-x1

p3 = -Δy, q3 = y1-ymin

p4 = Δy, q4 = ymax-y1

Page 29: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 29

Lines parallel to window boundary

For lines that are parallel to a window boundary, the corresponding pk will be 0If the corresponding qk < 0 the line is outside the boundary and can be ignored

Page 30: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 30

Other linesIf pk < 0 the infinite extension of the line enters the clipping regionIf pk > 0 the infinite extension of the line leaves the clipping regionIn either case, the value of t for which the line crosses the boundary is qk / pk

Page 31: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 31

Liang-Barksy algorithmLet t1 and t2 define the ends of the clipped line that will be visiblet1 is the entering time, t2 the leaving timeInitialize t1 to 0 and t2 to 1For each of the four boundaries:

if pk = 0 thenif qk < 0 then reject line

else if pk < 0 thencompute entering time

Page 32: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 32

Liang-Barksy algorithm (cont.)

if entering time > t1 then• change t1 to entering time• if t1 > t2 then reject line

else // pk > 0compute leaving timeif leaving time < t2 then

• change t2 to leaving time• if t1 > t2 then reject line

If line has not been rejected then draw line between t1 and t2

Page 33: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 33

Problems with polygonsEdges of polygons are made of line segmentsRecognize that when we clip a polygon’s edges, some of the edges may be completely removed and others may be partially removedThus, we may change the shape of the polygon during clipping

Page 34: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 34

Clipping polygons to a rectangular window

Number vertices of polygon and window in a clockwise mannerClip against one window edge at a timeInput is a set of polygon verticesOutput is an updated set of polygon vertices

Page 35: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 35

Sutherland-Hodgmanalgorithm

Start with edge from last polygon vertex to first one, and then repeat for each successive edge:

Assume beginning vertex has been handledIf both vertices are inside window with respect to clipping edge output second vertex

Page 36: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 36

Sutherland-Hodgmanalgorithm (cont.)

If first vertex is inside and second vertex is outside, find intersection of polygon edge with clipping edge and output intersectionIf both vertices are outside window output nothingIf first vertex is outside and second vertex is inside, find intersection of polygon edge with clipping edge and output intersection and then second vertex

Page 37: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 37

Bounding boxClipping polygons can be time consuming, especially if it is many sided and lies completely outside the windowUse a bounding box (extent) to eliminate these quickly

Page 38: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 38

What’s visible?Once clipped, we now have a list of polygons to displayWe must determine which surfaces are visible and which are hidden

Page 39: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 39

Object-space approachesGiven a list of n polygonsTake one polygon and compare it with the remaining ones to determine the part that is visible; render itThe above polygon no longer needs to be consideredTake one of the remaining polygons and repeat the processThis is O(n2) and thus should only be used for scenes with few polygons

Page 40: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 40

Image-space approachesFollow ray tracing paradigmConsider a ray from viewpoint that passes through a pixelFind the closest intersection of the ray with all polygonsColor pixel by the shade of the face closest to viewpointRepeat for all pixelsThis is O(n) if there are n polygonsHowever, they tend to be more jagged since they work at the pixel level

Page 41: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 41

Back face removalA back face is one that is turned away from the eye of the cameraThe camera will not see it if the faces in front are opaqueFor a convex polyhedron:

every face is either wholly visible or wholly invisibleeliminating back faces is equivalent to hidden surface removal

Page 42: Today’s class - it.uu.se fileInformationsteknologi Thursday, November 22, 2007 Computer Graphics - Class 11 1 Today’s class Clipping Parametric and point-normal form of lines

Info

rmat

ions

tekn

olog

i

Thursday, November 22, 2007 Computer Graphics - Class 11 42

CullingThe process of removing something is known as cullingIn OpenGL, turn on culling of back faces with glEnable (GL_CULL_FACE); and then glCullFace (GL_BACK);