Re: Help with antialiasing

Search this archive.

From: Jay Link (jlink@interlink-bbs.com)
Date: Sun 30 Jan 2000 - 18:27:35 IST


> But to be honest, I think the advice you're after is "look in a
> computer graphics book". :-)

Thanks, Rus.  :-)   BUT, I *have* been looking in tons o' graphics books,
and none of 'em seem to have actual code. The closest I've found was
something by Michael Abrash, but even that didn't help 100%.

However, after hours & hours, I *have* managed to improve the previous
code I posted. Now it works "pretty well", and it's geared to draw lines
in every direction (vga_getch() pauses execution between lines) to prove
it. It appears that the only remaining problem is that some pixels are
drawn on the wrong side of the line during the transitional times when
the line is somewhat diagonal. If you run the code, you'll see what I
mean.


#include <stdlib.h>
#include <vga.h>
#include <vgagl.h>

void wu_line(int x1, int y1, int x2, int y2, int r, int g, int b);

int main(void)
{
   int i;

   vga_init();
   vga_setmode(G320x200x256);
   gl_setcontextvga(G320x200x256);
   gl_setrgbpalette();

   for (i = 60; i < 260; i++)
   {
      wu_line(160, 100, i, 199, 255, 255, 255);
      vga_getch();
      gl_clearscreen(0);
   }
   for (i = 199; i >= 0; i--)
   {
      wu_line(160, 100, 259, i, 255, 255, 255);
      vga_getch();
      gl_clearscreen(0);
   }
   for (i = 259; i >= 60; i--)
   {
      wu_line(160, 100, i, 0, 255, 255, 255);
      vga_getch();
      gl_clearscreen(0);
   }
   for (i = 0; i < 200; i++)
   {
      wu_line(160, 100, 60, i, 255, 255, 255);
      vga_getch();
      gl_clearscreen(0);
   }


   vga_setmode(TEXT);

   return 0;
}

void wu_line(int x1, int y1, int x2, int y2, int r, int g, int b)
{
    #define ABS(a) (((a)<0) ? -(a) : (a))

    int temp;
    int dx, dy, ax, ay, ax2, ay2, sx, sy, x, y;
    int r1, g1, b1, r2, g2, b2;
    int highr, lowr, highg, lowg, highb, lowb;
    int percent;

    dx = x2 - x1;
    dy = y2 - y1;
    ax = ABS(dx) << 1;
    ax2 = ax * 2;
    ay = ABS(dy) << 1;
    ay2 = ay * 2;
    sx = (dx >= 0) ? 1 : -1;
    sy = (dy >= 0) ? 1 : -1;

    x = x1;
    y = y1;

    gl_setpixelrgb(x, y, r, g, b);

    if (ax > ay)
    {
	int d = ay - (ax >> 1);
	while (x != x2)
        {
	    if (d > 0 || (d == 0 && sx == 1))
            {
		y += sy;
		d -= ax;
            }
	    x += sx;
	    d += ay;
            percent = ((ABS(d) * 100) / ax);

            r1 = ((r * percent) / 100);
            r2 = 255 - r1;
            g1 = ((g * percent) / 100);
            g2 = 255 - g1;
            b1 = ((b * percent) / 100);
            b2 = 255 - b1;

	    gl_setpixelrgb(x, y, r1, g1, b1);

            if (ay2 > ax)
            {
               gl_setpixelrgb(x, (y - sy), r2, g2, b2);
            } else {
               gl_setpixelrgb(x, (y + sy), r2, g2, b2);
            }
	}
    } else {
	int d = ax - (ay >> 1);
	while (y != y2)
        {
	    if (d > 0 || (d == 0 && sy == 1))
            {
		x += sx;
		d -= ay;
            }
	    y += sy;
	    d += ax;
            percent = ((ABS(d) * 100) / ay);

            r1 = ((r * percent) / 100);
            r2 = 255 - r1;
            g1 = ((g * percent) / 100);
            g2 = 255 - g1;
            b1 = ((b * percent) / 100);
            b2 = 255 - b1;

            gl_setpixelrgb(x, y, r1, g1, b1);

            if (ax2 > ay)
            {
               gl_setpixelrgb((x - sx), y, r2, g2, b2);
            } else {
               gl_setpixelrgb((x + sx), y, r2, g2, b2);
            }
	}
    }
    gl_setpixelrgb(x, y, r, g, b);

    return;
}


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST