home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 064.lha / antialias.txt < prev    next >
Encoding:
Text File  |  1986-11-20  |  6.3 KB  |  145 lines

  1.  
  2. What is ANTIALIASING?
  3.  
  4.   Everything you see on your computer monitor is the illumination of pixels
  5. (dots, that is) in various patterns.  These dots, however, are not infinitely
  6. small, and therefore, if you are drawing lines, circles, or any polygons with
  7. edges, you will notice jaggies.  Jaggies (or staircasing), is an inevitable
  8. by-product of approximating these smooth surfaces on a grid... and the lower
  9. the resolution of your display (or coarser the grid), the worse the problem.
  10.  
  11.   Well, since higher resolution reduces the problem, why not have displays
  12. with resolutions of 1,000,000 x 1,000,000 , instead of the measly 640 x 400
  13. or 320 x 200 resolutions we have on the Amiga?  One awfully good reason is
  14. that whenever you double the resolution of a computer, the amount of memory
  15. the display takes up is quadrupled. That starts to get rather costly.  Also,
  16. one of the reasons our computers and monitors are so reasonably priced is 
  17. that they use hardware that is mass produced for television, which is about
  18. 640 x 482 pixels of resolution, in the US.  Go much beyond that, and you'd
  19. better be rich to afford the monitors and display circuitry.  
  20.  
  21.   So what to do?  Fortunately, higher resolution isn't the correct approach.
  22. No matter how fine your grid is, there are certain kinds of images that are
  23. sure to produce nasty artifacts, such as the classic "checkerboard floor
  24. extending to infinity".  You will start to get fascinating, but unwanted
  25. moire patterns.... trust me.  The best way to deal with the dreaded jaggies
  26. is - ANTIALIASING!   But to do antialiasing, you need colors. Lots of them.
  27. Sorry.  On the Amiga, you can fake it though, and that is what I've done for
  28. this demo/tutorial.  Colors are cheaper (in terms of memory) than pixels, and
  29. we should bow our heads with gratitude that this is the case. 
  30.  
  31.   I know that people sing rhapsodic praise about the wonders of Amiga graphics,
  32. and I'm one of them, as far as it goes.... but effective anti-aliasing really
  33. seperates the 'adults from the children'.  You not only need to have lots of
  34. colors, but you want to know where they are fast!  What we are effectively
  35. doing is blending colors together- blurring the edges, as it were.  The more
  36. colors you have between any color you may wish your line/circle/polygon to be,
  37. and what color(s) you are writing it on, the better.  Fortunately, we live in
  38. times in which the price of framebuffers is plummeting.  I know of one 
  39. manufacturer who will be marketing a 32,000 color (all on the screen at
  40. once) frame buffer for about $1500, for the IBM PC.  We can expect to see 
  41. similar technology in the Amiga universe shortly, I would wager.
  42.  
  43.   For those of you with limited patience, or limited budgets, who wish to 
  44. enter the exciting world of programming anti-aliased graphics, I humbly
  45. provide this code, which plots concentric circles, in 320 x 200, both
  46. anti-aliased and normally.  You won't have to ask which is which.  I get 
  47. around the requirement for a zillion colors by filling the 16 color map
  48. with a range of intensities of yellow to black, so it only draws yellow
  49. figures on a solid black background, or vice-versa.  But you'll get the
  50. idea.  
  51.  
  52.  
  53. How does ANTI-ALIASING work?
  54.  
  55.   I'm glad you asked.  A common way of drawing lines or circles involves
  56. the use of something called a DDA, or Digital Differential Analyzer (or 
  57. something like that).  What this means is, you keep track of when to 
  58. increment (or decrement) your 'X' coordinate (or 'Y'), depending on the
  59. value of a "remainder" variable, which we'll conventionally call 'R'.
  60.  
  61. Here's a simple example:
  62.  
  63.     line(x1,y1,x2,y2)
  64.      int x1,y1,x2,y2;
  65.     {
  66.     int dx, dy;
  67.     int r;
  68.  
  69.     dx = x2-x1;
  70.     dy = y2-y1;
  71.     r  = (dx-dy)/2
  72.     while (x1<=x2)
  73.     {
  74.         x1 = x1+1;
  75.         r  = r+dy;
  76.         if (r>=dx)
  77.         {
  78.             r=r-dx;
  79.             y1=y1+1;
  80.         }
  81.     Draw_a_Dot(x1,y1);
  82.     }
  83.     return;
  84.     }
  85.  
  86. (For simplicity, this routine will only plot lines between 0 + 45 degrees -
  87.  it can be adapted for all lines, at your leisure.)
  88.  
  89.   Lets say you want to draw a line from 10,10 (x,y) to 70,20.
  90. The change (delta) of X is 60 (call this DX), the delta of Y (DY) is 10.
  91. For the hell of it, we initialize the remainder, R, to half of DX-DY (or 25).
  92. We keep adding DY to R (while incrementing X, and drawing dots at (X,Y)), 
  93. until R exceeds DX, at which time we subtract DX from it, and increment Y.
  94.  
  95. In our example it would look like this:
  96.  
  97.         X    Y    R    R/DX
  98.         11    10    35    .58
  99.         12    10    45    .75
  100.         13    10    55    .92
  101.         14    11    5    .08
  102.         15    11    15    .25
  103.         16    11    25    .42
  104.         17    11    35    .58
  105.         18    11    45    .75
  106.         19    11    55    .92
  107.         20    12    5    .08
  108.         ...    ...    ...    ...
  109.  
  110.   Now here's where the anti-aliasing comes in: the DDA's 'R' can be pressed into
  111. double duty by thinking of its relationship to DX as a PERCENTAGE of coverage
  112. of a pixel by the line.  In other words, assuming a line of one pixel thickness,
  113. 58% of the pixel at (11,10) is "covered" by that line, so by interpolating
  114. the color of the line and the color of the pixel by 58%, you get the color you
  115. should paint there!  If the pixel is color 0 (black) and the line is color 15
  116. (yellow), color 8 (medium yellow) should be painted there.   If 58% of the
  117. pixel is covered, and the line is one pixel thick, where is the other 42% of 
  118. that line?  At (X,Y-1), since our line is going "up", the trailing edge is 
  119. "under".   So you paint a "trailing edge" of that line at 42% yellow (color 6)
  120. interpolated with backround color.  Do this for each point you plot, and 
  121. voila, you have an anti-aliased line.  Congratulations.
  122.  
  123. [ interpolation: getting the difference between two values, multiplying that
  124. difference by a percentage, and adding it back in to the first value. i.e. 
  125. a 10% interpolation between 10 and 50 is 14 ((50-10)*.10)+10) ]
  126.  
  127. Antialias.c uses this method with a circle drawing DDA.... due to the
  128. aspect ratio of the pixels, the circles aren't round, but you can easily
  129. fix that yourselves, if you have a yen to.
  130.  
  131.   At this point, I wish to acknowledge my indebtedness to Mike Higgens, 
  132. who published this method of anti-aliasing edges quickly, using integer math.
  133. He was my colleague at Time Arts, and for years I will be rediscovering 
  134. things that he's already forgotten.... Thanks Mike!
  135.  
  136.   You can leave comments, gripes, questions, and blonde virgins to me
  137. at AMIC or CIS # 73267,2124.
  138.  
  139.         Miles Keith Kurland
  140.         The Programmer General
  141.  
  142.         "Warning! The Programmer General has determined that 
  143.          failure to back up your hard disks may result in
  144.          lost data, acute anxiety, and random acts of violence"
  145.