textfiles/programming/AMIGA/antialia.txt

144 lines
6.4 KiB
Plaintext

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