Presentation on theme: "CMPE 466 COMPUTER GRAPHICS"— Presentation transcript:
1 CMPE 466 COMPUTER GRAPHICS Chapter 6Algorithms for Graphics PrimitivesInstructor: D. AriflerMaterial based onComputer Graphics with OpenGL®, Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. CarithersFundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve MarschnerComputer Graphics by F. S. Hill
6 Digital differential analyzer (DDA) A scan-conversion line algorithmFor m≤1Sample at unit x intervals: δx=1Compute yk+1=yk+mCalculated y values must be rounded to nearest intFor m>1Sample at unit y intervals: δy=1Compute xk+1=xk+1/mCalculated x values must be rounded to nearest intIf we process from right to left and m≤1, then we have δx=-1 and yk+1=yk-mIf we process from right to left and m>1, then we have δy=-1 and xk+1=xk-1/mFor negative slopes, look at the the magnitude of m
8 Problems of DDA Accumulation of round-off errors Calculated pixel positions may drift away from the true line path for long segmentsRounding operations and floating point arithmetic are time-consuming
9 Bresenham’s line algorithm Goal: Use only incremental integer calculations (fast!)Figure A section of a display screen where a straight-line segment is to be plotted, starting from the pixel at column 10 on scan line 11.Figure A section of a display screen where a negative slope line segment is to be plotted, starting from the pixel at column 50 on scan line 50.
10 Bresenham’s line algorithm Consider lines with slope m<1Decide which pixel to plot in column xk+1=xk+1Choices: (xk+1, yk) or (xk+1, yk+1)Figure A section of the screen showing a pixel in column xk on scan line yk that is to be plotted along the path of a line segment with slope 0 < m < 1.
11 Bresenham’s line algorithm At sampling position xk+1, label vertical pixel separations from the line path as dlower and dupperFigure Vertical distances between pixel positions and the line y coordinate at sampling position xk + 1.
12 BresenhamTo determine which pixel is closet , we first calculate:
13 Bresenham We would like to use only integer calculations Define a decision parameter pk. Letting m=Δy/Δx:Sign of pk is the same as the sign of dlower-dupper because Δx>0Parameter c is constant and is 2Δy+Δx(2b-1) which is independent of the pixel position and will be eliminated in the recursive calculation
14 BresenhamIf pixel at yk is closer to the line path than pixel at yk+1 (i.e. dlower<dupper) the pk is negative. In that case, we plot the lower pixel; otherwise, we plot the upper pixelWe can obtain successive decision parameters recursively. At step k+1usingwhere yk+1-yk is either 0 or 1 depending on the sign of pkFirst parameter is calculated as
18 Circle-generating algorithms Figure Circle with center coordinates (xc , yc) and radius r .
19 Circle-generating algorithms One could useSqrt is time-consumingFigure Upper half of a circle plotted with Equation 6-27 and with (xc , yc) = (0, 0).
20 Circle equation in parametric polar form Trig calculations are time-consumingWe can use symmetryFigure Symmetry of a circle. Calculation of a circle point (x, y) in one octant yields the circle points shown for the other seven octants.
21 Midpoint circle algorithm First, define:We will perform the tests for the midpositions between pixels near the circle path at each sampling step
22 Midpoint circle algorithm Figure Midpoint between candidate pixels at sampling position xk + 1 along a circular path.
23 Circles: decision parameter Define the decision parameterIf pk<0 (midposition is inside), yk is closerOtherwise, midposition is outside or on the circle boundary and we select the pixel on scan line yk-1
29 ExampleFigure Pixel positions (solid circles) along a circle path centered on the origin and with radius r = 10, as calculated by the midpoint circle algorithm. Open (“hollow”) circles show the symmetry positions in the first quadrant.
30 Ellipse-generating algorithms Figure Ellipse generated about foci F1 and F2.
31 Ellipse in standard position Figure Ellipse centered at (xc, yc) with semimajor axis rx and semiminor axis ry.
32 Ellipse in standard position Figure The bounding circle and eccentric angle θ for an ellipse with rx > ry .
33 Midpoint ellipse algorithm Figure Ellipse processing regions. Over region 1, the magnitude of the ellipse slope is less than 1.0; over region 2, the magnitude of the slope is greater than 1.0.
44 ExampleFigure Pixel positions along an elliptical path centered on the origin with rx = 8 and ry = 6, using the midpoint algorithm to calculate locations within the first quadrant.
45 Scan-line polygon-fill algorithms For each scan-line that crosses the polygon, the edge intersections are sorted from left to right, and then pixel positions between and including each intersection pair are set to the specified fill colorFigure Interior pixels along a scan line passing through a polygon fill area.
46 Complexities of polygon filling Figure Intersection points along scan lines that intersect polygon vertices. Scan line y generates an odd number of intersections, but scan line y’ generates an even number of intersections that can be paired to identify correctly the interior pixel spans.
47 Polygon fillingFor scan line y, the two edges sharing an intersection vertex are on opposite sides of the scan lineFor scan line y’, the two intersection edges are both above the scan lineA vertex that has adjoining edge on opposite sides of an intersecting scan line should be counted as just one boundary intersection point. Endpoints of segments can be adjusted as shown in the next slide.In determining edge intersections, coherence methods are used. This often involves incremental calculations applied along a scan line or between scan lines
48 Polygon filling: endpoint adjustments Figure Adjusting endpoint y values for a polygon, as we process edges in order around the polygon perimeter. The edge currently being processed is indicated as a solid line. In (a), the y coordinate of the upper endpoint of the current edge is decreased by 1. In (b), the y coordinate of the upper endpoint of the next edge is decreased by 1.
49 Edge tables for polygon filling To perform a polygon-fill efficiently, we first store polygon boundary in a sorted edge tableProceeding around the edges (clockwise or counterclockwise), we can use a bucket sort to store the edges, sorted on the smallest y value of each edge, in the correct scan-line positionskth position points to a list of all edges having their lower endpoint on scan line kAt kth position, edges are in sorted order from left to right.Next, we process the scan lines from the bottom of the polygon to its top, producing an active edge list (AEL) for each scan line crossing the polygon boundariesThe active edge list for a scan line contains all edges crossed by that scan line
50 Edge tablesFigure A polygon and its sorted edge table, with edge DC shortened by one unit in the y direction.
51 Fill methods for areas with irregular boundaries Start at an inside position and paint the interior point by point out to the boundaryBoundary-fillFill the interior of a region pixel by pixel until the boundary color is encounteredFlood-fillFill in (recolor) an area that is not defined within a single color boundaryPaint by replacing a specified interior color instead of searching for a particular boundary color
52 Boundary-fillBoundary-ﬁll algorithm starts from an interior point (x, y) and tests the color of neighboring positionsIf a tested position is not displayed in the boundary color, its color is changed to the ﬁll color and its neighbors are tested (see next slide)This procedure continues until all pixels are processed up to the designated boundary color for the area
53 Processing neighboring pixels Figure Fill methods applied to a 4-connected area (a) and to an 8-connected area (b). Hollow circles represent pixels to be tested from the current test position, shown as a solid color.
54 4-connected vs. 8-connected algorithm Figure The area defined within the color boundary (a) is only partially filled in (b) using a 4-connected boundary-fill algorithm.
56 Flood-fillSometimes we want to ﬁll in (or recolor) an area that is not deﬁned within a single color boundaryWe can paint such areas by replacing a speciﬁed interior color instead of searching for a particular boundary color.Figure An area defined within multiple color boundaries.
58 AntialiasingAliasing: Distortion of information due to low-frequency sampling (undersampling)Increase sampling rate in raster systemsSimply display at higher resolution: Requires large frame-buffers!Apple MacBook Retina Display: Over 5 million on the 15‑inch model: With so high pixel density, your eyes can’t discern individual pixels at normal viewing distanceWith raster systems that are capable of displaying more than two intensity levels per color, we can apply antialiasing methods to modify pixel intensitiesAntialiasing methods: Supersampling, postfiltering, prefiltering, pixel phasingWe will focus on antialiasing methods for straight-line segments
59 Antialiasing Blurring to smooth an image E.g. Sharp transitions from black to white is softened by using a mixture of gray pixels near the bordersWhen looking from a distance, eye blends the gracefully varying shades together
60 Supersampling (a special case of post-filtering) Sampling object characteristics at a higher resolution and displaying the results at a lower resolutionDivide each pixel into a number of subpixels and count the number of subpixels that overlap the line path. The intensity for each pixel is set to a value proportional to this subpixel count“Post”filtering: involves computing intensities at subpixel grid positions and then combining the results to obtain the pixel intensities
61 SupersamplingFigure Supersampling subpixel positions along a straight-line segment whose left endpoint is at screen coordinates (10, 20).Figure Supersampling subpixel positions in relation to the interior of a line of finite width.
62 Postfiltering: Subpixel weighting masks Give more weight to subpixel near the center of a pixel areaFigure Relative weights for a grid of 3 × 3 subpixels.Center subpixel is weighted by a factor of 1/4; the top, bottom, and side subpixels are each weighted by a factor of 1/8; and the corner subpixels are each weighted by a factor of 1/16.
63 Example masks or window functions (b) and (c) are approximations to the Bartlett window:Weights grow linearly from the edges toward the center
64 Application of weighting masks or window functions
65 Area sampling (prefiltering) Set pixel intensity proportional to the area of overlap of pixel with the finite-width line“Pre”filtering: the intensity of the pixel as a whole is determined without calculating subpixel intensitiesComputationally expensive
66 General filtering techniques More accurate method for antialiasingSimilar to applying a weighted pixel mask but now we have a continuous weighting surface (or filter function) covering the pixel.Figure Common filter functions used to antialias line paths. The volume of each filter is normalized to 1.0, and the height gives the relative weight at any subpixel position.
67 Pixel phasingOn raster systems that can address subpixel positions within the screen grid (hardware solution)Move (microposition) pixel positions closer to the line pathElectron beam can be shifted by a fraction of a pixel diameterFigure Jagged lines (a), plotted on the Merlin 9200 system, are smoothed (b) with an antialiasing technique called pixel phasing. This technique increases the number of addressable points on the system from 768 by 576 to 3072 by (Courtesy of Peritek Corp.)