10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be...

30
10/15/02 (c) 2002 University of Wiscon sin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already http://www.cs.wisc.edu/~schenney/courses/gues t/cs559-sept1603.ppt

Transcript of 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be...

Page 1: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Who Am I?

• Prof Stephen Chenney

• These notes will be online after the lecture – in fact they’re online already– http://www.cs.wisc.edu/~schenney/courses/guest/cs559-sept1603.pp

t

Page 2: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Sampling and Drawing

• Today we’ll talk about rasterization

• A regular, rectangular grid is referred to as a raster– A piece of history – there are also vector displays

– The raster name comes from the ways the pixels are drawn to the screen – left to right, top to bottom

• Rasterization is the problem of determining which pixels to color – which samples to use – to draw an object

Page 3: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Drawing Points

• You can draw points at any continuous position, not just at a pixel center (a sample point)– How do we know which pixel to turn on?

Page 4: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Drawing Points

• You can draw points at any continuous position, not just at a pixel center (a sample point)– How do we know which pixel to turn on?

• Solution is the simple one– Find the closest sample and turn it on – fill the pixel

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

• The best solution is anti-aliased points, where you partially color multiple pixels

Page 5: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Drawing Lines

• Lines have a similar problem to points: they are not likely to hit any pixel centers exactly

• What might we do instead?

• What are the aesthetic/perceptual issues in drawing lines?

Page 6: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Drawing Lines

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

• 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”)

Page 7: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 8: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 9: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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)• Amazing thing: You can do this with only integer add/subtract and

if/then

xi

Choose (xi+1,yi)

yi

yi+1

xi+1xi

Choose (xi+1,yi+1)

Page 10: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 11: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 12: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 13: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 14: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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 15: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

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

x=5, y=4x y d2 2 33 3 14 4 -15 4 76 5 57 6 3

1

2 3 4 5 6 7 81

2

3

4

5

6

7

Page 16: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Filling Triangles

• Sampling triangles:– When is a pixel inside a triangle?

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

• Triangle representation:– Triangle defined by three vertices, in known order (clockwise, ccw)

• We care most about triangles for 3D graphics

• We care most about rectangles for 2D graphics

• Filling more general shapes is difficult

Page 17: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

What is inside?

• Assume sampling with an array of spikes

• If spike is inside, pixel is inside

Page 18: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

What is inside?

• Assume sampling with an array of spikes

• If spike is inside, pixel is inside

Page 19: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Ambiguous Case

• Ambiguous cases: What if a pixel lies on an edge?– Problem because if two polygons

share a common edge, we don’t want pixels on the edge to belong to both

– Ambiguity would lead to different results if the drawing order were different

• Rule: if (x+, y+) is in, (x,y) is in ( is a small number)

• What if a pixel is on a vertex? Does our rule still work?

??

?

?

?

?

?

?

?

?

?

?

?

?

?

?

Page 20: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Ambiguous Case 1

• Rule:– On edge? If (x+, y+) is in,

pixel is in

– Which pixels are colored?

Page 21: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Ambiguous Case 1

• Rule:– Keep left and bottom edges

– Assuming y increases in the up direction

– If rectangles meet at an edge, how often is the edge pixel drawn?

?

Page 22: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Ambiguous Case 2

Page 23: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Ambiguous Case 2

?

?

?

?

or

Page 24: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Really Ambiguous

• We will accept ambiguity in such cases– The center pixel may end

up colored by one of two polygons in this case

– Which two?

1

2

34

5

6

Page 25: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Exploiting Coherence

• When filling a triangle (or polygon)– Several contiguous pixels along a row tend to be in the triangle - a

span of pixels• Scanline coherence

– Consider whole spans, not individual pixels

– The pixels required don’t vary much from one span to the next• Edge coherence

– Incrementally update the span endpoints

Page 26: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Sweep Fill Algorithms

• Algorithmic issues:– Reduce to filling many spans

– Which edges define the span of pixels to fill?

– How do you update these edges when moving from span to span?

– What happens when you cross a vertex?

Page 27: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Spans

• Process - fill the bottom horizontal span of pixels; move up and keep filling

• Have xmin, xmax for each span

• Define:– floor(x): largest integer < x

(not standard floor)

– ceiling(x): smallest integer >=x

• Fill from ceiling(xmin) up to floor(xmax)

• Consistent with convention

Page 28: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Updating Edges

• Each edge is a line of the form:

• Next row is:

• So, each current edge can have it’s x position updated by adding a constant stored with the edge

• Other values may also be updated, such as depth or color information

• For max efficiency, use a version of the midpoint algorithm

cmyxcmxy or

mxcmyx ii )1(1

Page 29: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

When are Edges Relevant?

• Edge is relevant when y>=ymin and y<ymax of edge

• What about horizontal edges?– m’ is infinite

Page 30: 10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.

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

Avoiding Floating Point

• For edge, m=x/y, which is a rational number

• View x as xi+xn/y, with xn<y. Store xi and xn

• Then x->x+m’ is given by:– xn=xn+x

– if (xn>=y) { xi=xi+1; xn=xn- y }

• Advantages:– no floating point

– can tell if x is an integer or not, and get floor(x) and ceiling(x) easily, for the span endpoints