880 likes | 1.05k Views
Implementation. Dr. Amy Zhang. Reading. Hill, Chapters 9.4-9.7 Hill, Chapter 10. Outline. The Rasterization Problem Scan converting lines Filling polygons Clipping Hidden surface removal (z-buffer). 3D Graphics Pipeline. The rasterization step scan converts the object into pixels.
E N D
Implementation Dr. Amy Zhang
Reading • Hill, Chapters 9.4-9.7 • Hill, Chapter 10
Outline • The Rasterization Problem • Scan converting lines • Filling polygons • Clipping • Hidden surface removal (z-buffer)
3D Graphics Pipeline • The rasterization step scan converts the object into pixels
Implicit Lines • Implicit equation in two dimensions: • Points with f(x,y) = 0 are on the line • Points with f(x,y) != 0 are not on the line
Implicit Lines • The implicit form of the slope‐intercept equation:
The slope‐intercept form can not represent some lines, such as x = 0. • A more general implicit form is more useful: y = mx + b or • The implicit line through two points (x0,y0) and (x1,y1):
Example • What is the implicit equation of this line?
Example • Solution 1: ‐2X+4Y=0 • Solution 2: 2X‐4Y=0 • What’s the lesson here? • k f(x,y) = 0 is the same line, for any value of k
Example • The value of f(x,y) = ‐2x +4y tells us which side of the line a point (x,y) is on
The Rasterization Problem • Primitives map to discrete display space
Solution • Selection of discrete representation values
Scan converting lines • Characterizing the problem: 2 cases • Move vertical scanline from x0 to xn • Move horizontal scanline from bottom to top • dot: center of pixel
Exactly one pixel per column: • Fewer: disconnected • More: too thick • Only discuss m≤1 case • The strategy • Pick pixels closest to endpoints • Select in between pixels “closest”to ideal line • Objective: To minimize the required calculations.
Disadvantage of DDA algorithm: • Floating point addition. Slow!! • Solution: integer operation
Bresenham’s Algorithm • Allowable Pixel Selections • Standard algorithm used in hardware/software rasterizers.
Decision function: Q(x,y)=y-mx-b above line L: +; on: 0; below: - F(x,y)=ax+by+c=0 (implicit equation of the line) if F(xi+1, yi+1/2)<0, M lies above the line, chose E if F(xi+1, yi+1/2)>0, M lies below the line, chose NE
Calculating the decision function (x0,y0): the point on the line Initial condition:
Problem: • Complete computation of d along the line • Solution: incremental calculation of di
Bresenham’s Algorithm • An example
Scan Line Algorithm • Compute the bounding pixels • Fill the spans
Scan Line Algorithm • Find the intersections of current scan line with all edges of the polygon. • Sort the intersections by increasing x coordinate. • Fill in pixels that lie between pairs of intersections that lie interior to the polygon using the odd/even parity rule. • Parity: even, change parity once encounter an edge • Special parity: no change of the parity (draw 1 pixel)
Filling polygons: scan line algorithm • http://www.cs.rit.edu/~icss571/filling/example.html
Edge table • Initializing the all_edges table: determine how the polygon's vertices are related • Each adjacent set of vertices defines an edge. For each edge, we need to keep: • The minimum y value of the 2 vertices: ymin • The maximum y value of the 2 vertices: ymax • The x value associated with the minimum y value: xval • 1/The slope of the edge:1/m (?)
Global edge table • Initializing the Global Edge Table (GET): • keep track of the edges that are still needed to complete the polygon. • place the edges with m≠0 (?) • be inserted with edges grouped by increasing minimum y values and further by x values
Active Edge Table • Initializing Parity • even since no edges have been crossed yet . • Initializing the Scan-Line • is equal to the lowest y value for all of the global edges.(10) • Initializing the Active Edge Table (AET) • keep track of the ordered edges that are intersected by the current scan-line.
Scanline = 10: at x=10, parity = odd. draw pixels left to x=22, parity = even. at x=28, draw a pixel (the special parity case)
Filling the polygon • Scanline=11: update x = x +1/m sort by xval • at x=10, parity = odd. • draw pixels left to x=23, parity = even. • at x=27, parity = odd. • draw pixels left to x=28, parity = even.
Scanline+=1, until ymax is equal to the next scan-line • Scanline = 15: • at x=10, parity = odd. • draw pixels left to x=22, parity = even. • at x=27, parity = odd. • draw pixels left to x=28, parity = even.
Scanline++ (16) • remove the edges if ymax=scanline from the active edge table (for the edges at indices 0, 2, and 3) • update the x values for all remaining edges in the active edge table
Now add the edges from the global edge table to the active edge table since ymin =scanline. reorder
Scanline=17: update = x +1/m, sort by xva • at x=12, parity = odd. • draw pixels left to x=20, parity = even.l
Scanline ++, until scanline=19 • at x=15, parity = odd. • draw pixels left to x=18, parity = even.
scanline++ • remove the edges if ymax=scanline from the active edge table (for the edges at indices 0, 1) • add the edges from the global edge table to the active edge table if ymin =scanline. • Iterate until both tables are empty.
Demo • Algorithm • Initiate the GET, scanline, AET • Draw the pixels based on AET and the parity • Scanline++ • Remove the edges from AET is scanline=ymax ,terminate if both AET and GET are empty • Update X values • Add edges to AET if GET is not empty • Reorder AET • Goto step 2.
Problem • Antialiasing by Area Averaging • Color multiple pixels for each x depending on coverage by ideal line
Aliasing problems can be serious for polygons • Jaggedness of edges • Small polygons neglected • Need compositing so color of one polygon does not totally determine color of pixel All three polygons should contribute to color
Outline • The Rasterization Problem • Scan converting lines • Filling polygons • Clipping • Hidden surface removal (z-buffer)
Clipping • Clipping Against a Rectangular Region Multiple Cases
Trivial Acceptance: • O( P0 ) = O( P1) = 0