cs559-sept1603

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/cs559sept1603.ppt
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
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?
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
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?
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”)
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:
xi 1  xi  1,
yi 1  round (mxi 1  b)
– step x, compute new y at each step by adding m to old y, rounding:
xi 1  xi  1,
10/15/02
yi 1  round ( yi  m)
(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)
10/15/02
(c) 2002 University of Wisconsin, CS559
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
yi+1
yi+1
yi
yi
xi
xi+1
xi
Choose (xi+1,yi)
10/15/02
xi+1
Choose (xi+1,yi+1)
(c) 2002 University of Wisconsin, CS559
Midpoint Decision Variable
• Write the line in implicit form:
F x, y   ax  by  c  y  x  x  y  x  y1  y  x1 
• 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
10/15/02
(c) 2002 University of Wisconsin, CS559
What Can We Decide?
di  2y( xi  1)  2xyi  x(2c  1)
• 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?
10/15/02
(c) 2002 University of Wisconsin, CS559
Updating The Decision Variable
• dk+1 is the old value, dk, plus an increment:
d k 1  d k  (d k 1  d k )
• If we chose yi+1=yi+1:
d k 1  d k  2y  2x
• If we chose yi+1=yi:
d k 1  d k  2y
• What is d1 (assuming integer endpoints)?
• Notice that we don’t need c any more
10/15/02
(c) 2002 University of Wisconsin, CS559
d1  2y  x
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+2y - 2x }
• If d<0 then { y=y, draw (x, y), d=d+2y }
• Compute the constants (2y-2x and 2y ) 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)
10/15/02
(c) 2002 University of Wisconsin, CS559
Example: (2,2) to (7,6)
x=5, y=4
x
y
7
6
5
4
3
2
1
1 2
10/15/02
3
4
5
6
7
8
(c) 2002 University of Wisconsin, CS559
d
Example: (2,2) to (7,6)
x=5, y=4
x
y
2
2
3
3
4
4
5
4
6
5
7
6
7
6
5
4
3
2
1
1 2
10/15/02
3
4
5
6
7
8
(c) 2002 University of Wisconsin, CS559
d
3
1
-1
7
5
3
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
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
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
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?
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?
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?
10/15/02
?
(c) 2002 University of Wisconsin, CS559
Ambiguous Case 2
10/15/02
(c) 2002 University of Wisconsin, CS559
Ambiguous Case 2
?
?
10/15/02
?
or
?
(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
6
2
5
3
4
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
10/15/02
(c) 2002 University of Wisconsin, CS559
Sweep Fill Algorithms
• Algorithmic issues:
–
–
–
–
10/15/02
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?
(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
10/15/02
(c) 2002 University of Wisconsin, CS559
Updating Edges
• Each edge is a line of the form:
y  mx  c or
x  y m  c 
• Next row is:
xi 1  ( y  1)m  c  xi  m
• 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
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
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
10/15/02
(c) 2002 University of Wisconsin, CS559