Upload
neil-hodge
View
227
Download
0
Tags:
Embed Size (px)
Citation preview
3/4/04 © University of Wisconsin, CS559 Spring 2004
Last Time
• Clipping Lines– Cohen-Sutherland: The use of outcodes and early reject/accept tests
– Liang-Barsky: Parametric clipping, find intersection parameters, label as entering leaving, reason based on ordering of enter/leaving
• Clipping Polygons– Weiler-Atherton lets you clip general polygons against general clip
regions
• Drawing Lines:– Bresenham’s algorithm draws lines using only addition, no
rounding
3/4/04 © University of Wisconsin, CS559 Spring 2004
Today
• Drawing lines– Bresenham example
• Drawing polygons
• Basic anti-aliasing
3/4/04 © University of Wisconsin, CS559 Spring 2004
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
• For floating point, initialization is harder, x and y will be floating point, but still no rounding required
3/4/04 © University of Wisconsin, CS559 Spring 2004
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
3/4/04 © University of Wisconsin, CS559 Spring 2004
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).
– Assume integers in window coordinates to simplify things for now
3/4/04 © University of Wisconsin, CS559 Spring 2004
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
3/4/04 © University of Wisconsin, CS559 Spring 2004
Polygon
ParityNon-zero Winding No.
Non-exterior
Inside/Outside Rules
3/4/04 © University of Wisconsin, CS559 Spring 2004
What is inside - 2?
• Assume sampling with an array of spikes
• If spike is inside, pixel is inside
3/4/04 © University of Wisconsin, CS559 Spring 2004
What is inside - 2?
• Assume sampling with an array of spikes
• If spike is inside, pixel is inside
3/4/04 © University of Wisconsin, CS559 Spring 2004
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 (for arbitrarily small )
• What if a pixel is on a vertex? Does our rule still work?
3/4/04 © University of Wisconsin, CS559 Spring 2004
Ambiguous Case 1
• Rule:– On edge? If (x+, y+) is in,
pixel is in
– Which pixels are colored?
3/4/04 © University of Wisconsin, CS559 Spring 2004
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?
3/4/04 © University of Wisconsin, CS559 Spring 2004
Ambiguous Case 2
3/4/04 © University of Wisconsin, CS559 Spring 2004
Ambiguous Case 2
?
?
?
?
or
3/4/04 © University of Wisconsin, CS559 Spring 2004
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
3/4/04 © University of Wisconsin, CS559 Spring 2004
Exploiting Coherence
• When filling a polygon– Several contiguous pixels along a row tend to be in the polygon - 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
3/4/04 © University of Wisconsin, CS559 Spring 2004
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?
3/4/04 © University of Wisconsin, CS559 Spring 2004
Spans
• Fill rows from bottom to top, one at a time
• Have xmin, xmax for each span
• Define:– floor(x): largest integer < x
– ceiling(x): smallest integer >=x
• Fill from ceiling(xmin) up to floor(xmax)
• Consistent with convention
xmin,, ceiling(xmin)xmax
floor(xmax)
3/4/04 © University of Wisconsin, CS559 Spring 2004
Algorithm
• For each row in the polygon:– Throw away irrelevant edges
– Obtain newly relevant edges
– Fill span
– Update current edges
• Issues:– How do we update existing edges?
– When is an edge relevant/irrelevant?
• All can be resolved by referring to our convention about what polygon pixel belongs to
3/4/04 © University of Wisconsin, CS559 Spring 2004
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
cmyxcmxy or
mxcmyx ii )1(1
3/4/04 © University of Wisconsin, CS559 Spring 2004
When are Edges Relevant (1)
• Use figures and convention to determine when edge is irrelevant– For y<ymin and y>=ymax of edge
• Similarly, edge is relevant when y>=ymin and y<ymax of edge
• What about horizontal edges?– m’ is infinite
3/4/04 © University of Wisconsin, CS559 Spring 2004
When are Edges Relevant (2)
1
2
3
43,4
1,3
1,2
Convex polygon:Always only two edges active
3/4/04 © University of Wisconsin, CS559 Spring 2004
When are Edges Relevant (3)
1,3
Horizontal edges?
1
2
3
44?
2?
3/4/04 © University of Wisconsin, CS559 Spring 2004
Sweep Fill Details
• For convex polygons there are always 2 edges, a left and a right– Fixes the amount of memory required, good for hardware
• Can generate memory addresses (for pixel writes) efficiently– You know address of leftmost pixel, and address of rightmost, can
fill all in between quickly
• In practice, use a version of the midpoint algorithm to update edges– What is the “midpoint” tested?
– Fast increment, no rounding, handles floating point
3/4/04 © University of Wisconsin, CS559 Spring 2004
yi
yi+1
xi+1
Midpoint Method For Polygon Edges
• Consider a left edge with slope [0-1]
• Always want to draw point on line or below
• Consider the point (xi+1,yi+1). Is it above or below the line?
xi
Choose (xi+1,yi+1)
yi
yi+1
xi+1xi
Choose (xi+1,yi)
3/4/04 © University of Wisconsin, CS559 Spring 2004
Extending to Arbitrary Polygons
• Can be more than one span in any row
• Keep sorted list of edges across row, fill between pairs of edges
3/4/04 © University of Wisconsin, CS559 Spring 2004
Anti-Aliasing
• Recall: We can’t sample and then accurately reconstruct an image that is not band-limited– Infinite Nyquist frequency
– Attempting to sample sharp edges gives “jaggies”, or stair-step lines
• Solution: Band-limit by filtering (pre-filtering)– What sort of filter will give a band-limited result?
• But when doing computer rendering, we don’t have the original continuous function
3/4/04 © University of Wisconsin, CS559 Spring 2004
Pre-Filtered Primitives
• We can simulate filtering by rendering “thick” primitives, with , and compositing
• Expensive, and requires the ability to do compositing
• Hardware method: Keep sub-pixel masks tracking coverage
Ideal
1/62/3
1/6
Filter =1/6=2/3=1/6 over =1/6
=2/3=1/6
Pre-Filtered and composited
3/4/04 © University of Wisconsin, CS559 Spring 2004
Post-Filtering (Supersampling)
• Sample at a higher resolution than required for display, and filter image down– Easy to implement in hardware
– Typical is 2x2 sampling per pixel, with simple averaging to get final
• What kind of filter?
• More advanced methods generate different samples (eg. not on regular grid) and filter properly– Issues of which samples to take, and how to
filter them
3/4/04 © University of Wisconsin, CS559 Spring 2004
Where We Stand
• At this point we know how to:– Convert points from local to window coordinates
– Clip polygons and lines to the view volume
– Determine which pixels are covered by any given line or polygon
– Anti-alias
• Next thing:– Determine which polygon is in front