More on antialiasing...

Search this archive.

From: Jay Link (jlink@interlink-bbs.com)
Date: Sun 30 Jan 2000 - 20:56:54 IST


Yet another improvement. Now, the antialiased line reads the existing
colors before it writes, to ensure a better blend with the background.
However, some of the diagonal lines still look crappy, especially in
320 x 200 modes. This does mostly disappear in 640 x 480 and up.



#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();

   gl_clearscreen(0);
   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 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, compliment;
    int color;

    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;

    if (x1 == x2)
    {
       color = gl_rgbcolor(r, g, b);
       gl_line(x1, y1, x2, y2, color);
       return;
    } else if (y1 == y2) {
       color = gl_rgbcolor(r, g, b);
       gl_hline(x1, y1, x2, color);
       return;
    } else if (ax == ay) {
       color = gl_rgbcolor(r, g, b);
       gl_line(x1, y1, x2, y2, color);
       return;
    }

    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);
            compliment = 100 - percent;

            gl_getpixelrgb(x, y, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = (((highr - lowr) * percent) / 100);
            r2 = (r > r1) ? r2 : (255 - r2);
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = (((highg - lowg) * percent) / 100) + lowg;
            g2 = (g > g1) ? g2 : (255 - g2);
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = (((highb - lowb) * percent) / 100) + lowb;
            b2 = (b > b1) ? b2 : (255 - b2);
	    gl_setpixelrgb(x, y, r2, g2, b2);

            if (ay2 > ax)
            {
               gl_getpixelrgb(x, (y - sy), &r1, &g1, &b1);
               highr = (r > r1) ? r : r1;
               lowr = (r == highr) ? r1 : r;
               r2 = (((highr - lowr) * compliment) / 100) + lowr;
               r2 = (r > r1) ? r2 : (255 - r2);
               highg = (g > g1) ? g : g1;
               lowg = (g == highg) ? g1 : g;
               g2 = (((highg - lowg) * compliment) / 100) + lowg;
               g2 = (g > g1) ? g2 : (255 - g2);
               highb = (b > b1) ? b : b1;
               lowb = (b == highb) ? b1 : b;
               b2 = (((highb - lowb) * compliment) / 100) + lowb;
               b2 = (b > b1) ? b2 : (255 - b2);
               gl_setpixelrgb(x, (y - sy), r2, g2, b2);
            } else {
               gl_getpixelrgb(x, (y + sy), &r1, &g1, &b1);
               highr = (r > r1) ? r : r1;
               lowr = (r == highr) ? r1 : r;
               r2 = (((highr - lowr) * compliment) / 100) + lowr;
               r2 = (r > r1) ? r2 : (255 - r2);
               highg = (g > g1) ? g : g1;
               lowg = (g == highg) ? g1 : g;
               g2 = (((highg - lowg) * compliment) / 100) + lowg;
               g2 = (g > g1) ? g2 : (255 - g2);
               highb = (b > b1) ? b : b1;
               lowb = (b == highb) ? b1 : b;
               b2 = (((highb - lowb) * compliment) / 100) + lowb;
               b2 = (b > b1) ? b2 : (255 - b2);
               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);
            compliment = 100 - percent;

            gl_getpixelrgb(x, y, &r1, &g1, &b1);
            highr = (r > r1) ? r : r1;
            lowr = (r == highr) ? r1 : r;
            r2 = (((highr - lowr) * percent) / 100) + lowr;
            r2 = (r > r1) ? r2 : (255 - r2);
            highg = (g > g1) ? g : g1;
            lowg = (g == highg) ? g1 : g;
            g2 = (((highg - lowg) * percent) / 100) + lowg;
            g2 = (g > g1) ? g2 : (255 - g2);
            highb = (b > b1) ? b : b1;
            lowb = (b == highb) ? b1 : b;
            b2 = (((highb - lowb) * percent) / 100) + lowb;
            b2 = (b > b1) ? b2 : (255 - b2);
	    gl_setpixelrgb(x, y, r2, g2, b2);

            if (ax2 > ay)
            {
               gl_getpixelrgb((x - sx), y, &r1, &g1, &b1);
               highr = (r > r1) ? r : r1;
               lowr = (r == highr) ? r1 : r;
               r2 = (((highr - lowr) * compliment) / 100) + lowr;
               r2 = (r > r1) ? r2 : (255 - r2);
               highg = (g > g1) ? g : g1;
               lowg = (g == highg) ? g1 : g;
               g2 = (((highg - lowg) * compliment) / 100) + lowg;
               g2 = (g > g1) ? g2 : (255 - g2);
               highb = (b > b1) ? b : b1;
               lowb = (b == highb) ? b1 : b;
               b2 = (((highb - lowb) * compliment) / 100) + lowb;
               b2 = (b > b1) ? b2 : (255 - b2);
               gl_setpixelrgb((x - sx), y, r2, g2, b2);
            } else {
               gl_getpixelrgb((x + sx), y, &r1, &g1, &b1);
               highr = (r > r1) ? r : r1;
               lowr = (r == highr) ? r1 : r;
               r2 = (((highr - lowr) * compliment) / 100) + lowr;
               r2 = (r > r1) ? r2 : (255 - r2);
               highg = (g > g1) ? g : g1;
               lowg = (g == highg) ? g1 : g;
               g2 = (((highg - lowg) * compliment) / 100) + lowg;
               g2 = (g > g1) ? g2 : (255 - g2);
               highb = (b > b1) ? b : b1;
               lowb = (b == highb) ? b1 : b;
               b2 = (((highb - lowb) * compliment) / 100) + lowb;
               b2 = (b > b1) ? b2 : (255 - b2);
               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