26
10/15/02 (c) 2002 University of Wiscon sin, CS559 Last Time • Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

Embed Size (px)

Citation preview

Page 1: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Last Time

• Clipping

Page 2: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Today

• Homework 3 due

• Line drawing

• Polygon filling

• Next time: Midterm– Web page has details

– Note that you are allowed one page (double sided) of anything

Page 3: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

General Liang-Barsky

• Liang-Barsky works for any convex clip region– Compute intersection t for all clip lines/planes and label them as

entering or exiting

– Parameter value for small t end of line is:tsmall= max(0, entering t’s)

– Parameter value for large t end of line is: tlarge=min(1, leaving t’s)

– if tsmall<tlarge, there is a line segment - compute endpoints by substituting t values

Page 4: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Weiler Atherton Polygon Clipping

• Faster than Sutherland-Hodgman for complex polygons

• For clockwise polygon:– for out-to-in pair, follow usual

rule

– for in-to-out pair, follow clip edge

• Easiest to start outside

Inside

1

2

3

4

5

6

7

8

go up

go up

Page 5: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

General Clipping

• Clipping general against general polygons is quite hard

• Outline of Weiler algorithm:– Replace crossing points with vertices

– Double all edges and form linked lists of edges

– Change links at vertices

– Enumerate polygon patches

• Can use clipping to break concave polygon into convex pieces; main issue is inside-outside for edges

Page 6: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Weiler Algorithm (1)

Page 7: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Changes to

Rearranging pointersmakes it possible to enumerate all componentsof the intersection

Page 8: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Where We Stand

• At this point we know how to:– Convert points from local to screen coordinates

– Clip polygons and lines to the view volume

• Next thing:– Determine which pixels are covered by any given point, line or

polygon

Page 9: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Weiler Atherton Polygon Clipping

• Faster than Sutherland-Hodgman for complex polygons

• For clockwise polygon:– for out-to-in pair, follow usual

rule

– for in-to-out pair, follow clip edge

• Easiest to start outside

Inside

1

2

3

4

5

6

7

8

go up

go up

Page 10: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

General Clipping

• Clipping general against general polygons is quite hard

• Outline of Weiler algorithm:– Replace crossing points with vertices

– Double all edges and form linked lists of edges

– Change links at vertices

– Enumerate polygon patches

• Can use clipping to break concave polygon into convex pieces; main issue is inside-outside for edges

Page 11: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Weiler Algorithm (1)

Page 12: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Changes to

Rearranging pointersmakes it possible to enumerate all componentsof the intersection

Page 13: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Where We Stand

• At this point we know how to:– Convert points from local to screen coordinates

– Clip polygons and lines to the view volume

• Next thing:– Determine which pixels are covered by any given point, line or

polygon

Page 14: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Drawing Points

• When points are mapped into window coordinates, they could land anywhere – not just at a pixel center

• Solution is the simple, obvious one– Map to window space

– Fill the closest pixel

– Can also specify a radius – fill a square of that size, or fill a circle• Square is faster

• What function are we sampling with?

Page 15: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Drawing Lines

• Task: Decide which pixels to fill (samples to use) to represent a line

• We know that all of the line lies inside the visible region (clipping gave us this!)

• Issues:– If slope between -1 and 1, one pixel per column. Otherwise, one

pixel per row– Constant brightness? Lines of the same length should light the same

number of pixels (we normally ignore this)– Anti-aliasing? (Getting rid of the “jaggies”)– Sampling theory?

Page 16: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Line Drawing Algorithms

• Consider lines of the form y=m x + c, where m=y/x, 0<m<1, integer coordinates– All others follow by symmetry

• Variety of slow algorithms (Why slow?):– step x, compute new y at each step by equation, rounding:

– step x, compute new y at each step by adding m to old y, rounding:

)( ,1 111 bmxroundyxx iiii

)( ,1 11 myroundyxx iiii

Page 17: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Bresenham’s Algorithm Overview

• Aim: For each x, plot the pixel whose y-value is closest to the line

• Given (xi,yi), must choose from either (xi+1,yi+1) or (xi+1,yi)

• Idea: compute a decision variable– Value that will determine which pixel to draw

– Easy to update from one pixel to the next

• Bresenham’s algorithm is the midpoint algorithm for lines– Other midpoint algorithms for conic sections (circles, ellipses)

Page 18: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

yi

yi+1

xi+1

Midpoint Methods

• Consider the midpoint between (xi+1,yi+1) and (xi+1,yi)

• If it’s above the line, we choose (xi+1,yi), otherwise we choose (xi+1,yi+1)

xi

Choose (xi+1,yi)

yi

yi+1

xi+1xi

Choose (xi+1,yi+1)

Page 19: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Midpoint Decision Variable

• Write the line in implicit form:

• The value of F(x,y) tells us where points are with respect to the line– F(x,y)=0: the point is on the line

– F(x,y)<0: The point is above the line

– F(x,y)>0: The point is below the line

• The decision variable is the value of di = 2F(xi+1,yi+0.5)– The factor of two makes the math easier

11, xyyxyxxycbyaxyxF

Page 20: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

What Can We Decide?

• di negative => next point at (xi+1,yi)

• di positive => next point at (xi+1,yi+1)

• At each point, we compute di and decide which pixel to draw

• How do we update it? What is di+1?

)12(2)1(2 cxxyxyd iii

Page 21: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Updating The Decision Variable

• dk+1 is the old value, dk, plus an increment:

• If we chose yi+1=yi+1:

• If we chose yi+1=yi:

• What is d1 (assuming integer endpoints)?

• Notice that we don’t need c any more

)( 11 kkkk dddd

xydd kk 221

ydd kk 21

xyd 21

Page 22: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Bresenham’s Algorithm

• For integers, slope between 0 and 1:– x=x1, y=y1, d=2dy - dx, draw (x, y)

– until x=x2

• x=x+1

• If d>0 then { y=y+1, draw (x, y), d=d+2y - 2x }

• If d<0 then { y=y, draw (x, y), d=d+2y }

• Compute the constants (2y-2x and 2y ) once at the start– Inner loop does only adds and comparisons

• Floating point has slightly more difficult initialization, but is otherwise the same

• Care must be taken to ensure that it doesn’t matter which order the endpoints are specified in (make a uniform decision if d==0)

Page 23: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Example: (2,2) to (7,6)

x=5, y=4x y d

1

2 3 4 5 6 7 81

2

3

4

5

6

7

Page 24: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Filling polygons

• Sampling polygons:– When is a pixel inside a polygon?

– Given a pixel, which polygon does it lie in? Point location

• Polygon representation:– Polygon defined by a list of edges - each is a pair of vertices

– All vertices are inside the view volume and map to valid pixels. (Clipping gave us this.) Also, assume integers in window coordinates to simplify things for now

Page 25: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

What is inside - 1?

• Easy for simple polygons - no self intersections or holes– OpenGL requires these. Undefined for other cases

– OpenGL also requires convex polygons

• For general polygons, three rules are possible:– Non-exterior rule: A point is inside if every ray to infinity intersects

the polygon

– Non-zero winding number rule: Draw a ray to infinity that does not hit a vertex, if the number of edges crossing in one direction is not equal to the number crossing the other way, the point is inside

– Parity rule: Draw a ray to infinity and count the number or edges that cross it. If even, the point is outside, if odd, it’s inside

Page 26: 10/15/02 (c) 2002 University of Wisconsin, CS559 Last Time Clipping

10/15/02 (c) 2002 University of Wisconsin, CS559

Polygon

ParityNon-zero Winding No.

Non-exterior

Inside/Outside Rules