CMPE 466 COMPUTER GRAPHICS Chapter 6 Algorithms for Graphics Primitives Instructor: D. Arifler Material based on -Computer Graphics with OpenGL ®, Fourth

Embed Size (px)

Citation preview

  • Slide 1
  • CMPE 466 COMPUTER GRAPHICS Chapter 6 Algorithms for Graphics Primitives Instructor: D. Arifler Material based on -Computer Graphics with OpenGL , Fourth Edition by Donald Hearn, M. Pauline Baker, and Warren R. Carithers -Fundamentals of Computer Graphics, Third Edition by by Peter Shirley and Steve Marschner -Computer Graphics by F. S. Hill 1
  • Slide 2
  • Device-level algorithms for implementing primitives Line-drawing algorithms Circle-generating algorithms Ellipse-generating algorithms Scan-line polygon-fill algorithms Antialiasing 2
  • Slide 3
  • Jaggies in line-drawing 3 Figure 6-1 Stair-step effect (jaggies) produced when a line is generated as a series of pixel positions.
  • Slide 4
  • Line equations 4 Figure 6-2 Line path between endpoint positions (x 0, y 0 ) and (x end, y end).
  • Slide 5
  • Determining deflection voltages 5
  • Slide 6
  • Digital differential analyzer (DDA) A scan-conversion line algorithm For m1 Sample at unit x intervals: x=1 Compute y k+1 =y k +m Calculated y values must be rounded to nearest int For m>1 Sample at unit y intervals: y=1 Compute x k+1 =x k +1/m Calculated x values must be rounded to nearest int If we process from right to left and m1, then we have x=-1 and y k+1 =y k -m If we process from right to left and m>1, then we have y=-1 and x k+1 =x k -1/m For negative slopes, look at the the magnitude of m 6
  • Slide 7
  • DDA 7
  • Slide 8
  • Problems of DDA Accumulation of round-off errors Calculated pixel positions may drift away from the true line path for long segments Rounding operations and floating point arithmetic are time-consuming 8
  • Slide 9
  • Bresenhams line algorithm Goal: Use only incremental integer calculations (fast!) 9 Figure 6-4 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 6-5 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.
  • Slide 10
  • Bresenhams line algorithm Consider lines with slope m
  • Bresenham We would like to use only integer calculations Define a decision parameter p k. Letting m=y/x: Sign of p k is the same as the sign of d lower -d upper because x>0 Parameter c is constant and is 2y+x(2b-1) which is independent of the pixel position and will be eliminated in the recursive calculation 13
  • Slide 14
  • Bresenham If pixel at y k is closer to the line path than pixel at y k+1 (i.e. d lower
  • Ellipse in standard position 32 Figure 6-18 The bounding circle and eccentric angle for an ellipse with r x > r y.
  • Slide 33
  • Midpoint ellipse algorithm 33 Figure 6-20 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.
  • Slide 34
  • Midpoint ellipse algorithm 34
  • Slide 35
  • Midpoint ellipse algorithm (region 1) 35 Figure 6-21 Midpoint between candidate pixels at sampling position x k + 1 along an elliptical path.
  • Slide 36
  • Ellipse 36
  • Slide 37
  • Ellipse (region 2) 37 Figure 6-22 Midpoint between candidate pixels at sampling position y k 1 along an elliptical path.
  • Slide 38
  • Ellipse 38
  • Slide 39
  • Midpoint ellipse algorithm 39
  • Slide 40
  • Midpoint ellipse algorithm 40
  • Slide 41
  • Example 41
  • Slide 42
  • Example 42
  • Slide 43
  • Example 43
  • Slide 44
  • Example 44 Figure 6-23 Pixel positions along an elliptical path centered on the origin with r x = 8 and r y = 6, using the midpoint algorithm to calculate locations within the first quadrant.
  • Slide 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 color 45 Figure 6-46 Interior pixels along a scan line passing through a polygon fill area.
  • Slide 46
  • Complexities of polygon filling 46 Figure 6-47 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.
  • Slide 47
  • Polygon filling For scan line y, the two edges sharing an intersection vertex are on opposite sides of the scan line For scan line y, the two intersection edges are both above the scan line A 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 47
  • Slide 48
  • Polygon filling: endpoint adjustments 48 Figure 6-48 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.
  • Slide 49
  • Edge tables for polygon filling To perform a polygon-fill efficiently, we first store polygon boundary in a sorted edge table Proceeding 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 positions kth position points to a list of all edges having their lower endpoint on scan line k At 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 boundaries The active edge list for a scan line contains all edges crossed by that scan line 49
  • Slide 50
  • Edge tables 50 Figure 6-50 A polygon and its sorted edge table, with edge DC shortened by one unit in the y direction.
  • Slide 51
  • Fill methods for areas with irregular boundaries Start at an inside position and paint the interior point by point out to the boundary Boundary-fill Fill the interior of a region pixel by pixel until the boundary color is encountered Flood-fill Fill in (recolor) an area that is not defined within a single color boundary Paint by replacing a specified interior color instead of searching for a particular boundary color 51
  • Slide 52
  • Boundary-fill Boundary-ll algorithm starts from an interior point (x, y) and tests the color of neighboring positions If 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 52
  • Slide 53
  • Processing neighboring pixels 53 Figure 6-53 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.
  • Slide 54
  • 4-connected vs. 8-connected algorithm 54 Figure 6-54 The area defined within the color boundary (a) is only partially filled in (b) using a 4-connected boundary-fill algorithm.
  • Slide 55
  • Boundary-fill 55
  • Slide 56
  • Flood-fill Sometimes we want to ll in (or recolor) an area that is not dened within a single color boundary We can paint such areas by replacing a specied interior color instead of searching for a particular boundary color. 56 Figure 6-56 An area defined within multiple color boundaries.
  • Slide 57
  • Flood-fill 57
  • Slide 58
  • Antialiasing Aliasing: Distortion of information due to low-frequency sampling (undersampling) Increase sampling rate in raster systems Simply 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 cant discern individual pixels at normal viewing distance With raster systems that are capable of displaying more than two intensity levels per color, we can apply antialiasing methods to modify pixel intensities Antialiasing methods: Supersampling, postfiltering, prefiltering, pixel phasing We will focus on antialiasing methods for straight-line segments 58
  • Slide 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 borders When looking from a distance, eye blends the gracefully varying shades together 59
  • Slide 60
  • Supersampling (a special case of post-filtering) Sampling object characteristics at a higher resolution and displaying the results at a lower resolution Divide 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 Postfiltering: involves computing intensities at subpixel grid positions and then combining the results to obtain the pixel intensities 60
  • Slide 61
  • Supersampling 61 Figure 6-59 Supersampling subpixel positions along a straight-line segment whose left endpoint is at screen coordinates (10, 20). Figure 6-60 Supersampling subpixel positions in relation to the interior of a line of finite width.
  • Slide 62
  • Postfiltering: Subpixel weighting masks Give more weight to subpixel near the center of a pixel area 62 Figure 6-61 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.
  • Slide 63
  • 63 Example masks or window functions (b) and (c) are approximations to the Bartlett window: Weights grow linearly from the edges toward the center
  • Slide 64
  • Application of weighting masks or window functions 64
  • Slide 65
  • Area sampling (prefiltering) Set pixel intensity proportional to the area of overlap of pixel with the finite-width line Prefiltering: the intensity of the pixel as a whole is determined without calculating subpixel intensities Computationally expensive 65
  • Slide 66
  • General filtering techniques More accurate method for antialiasing Similar to applying a weighted pixel mask but now we have a continuous weighting surface (or filter function) covering the pixel. 66 Figure 6-62 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.
  • Slide 67
  • Pixel phasing On raster systems that can address subpixel positions within the screen grid (hardware solution) Move (microposition) pixel positions closer to the line path Electron beam can be shifted by a fraction of a pixel diameter 67 Figure 6-63 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 2304. (Courtesy of Peritek Corp.)