抗锯齿像素渲染

anti-aliased pixel rendering

本文关键字:像素      更新时间:2023-10-16

我正在尝试实现抗锯齿像素渲染。我的基本想法是渲染4个像素而不是1个,并根据每个"真实"像素到"虚拟"像素的距离为其赋予权重:

void put_aa_pixel(double x, double y)
{
    int x0 = int(x);
    int x1 = x0 + 1;
    int y0 = int(y);
    int y1 = y0 + 1;
    double weight_x1 = x - x0;
    double weight_x0 = 1 - weight_x1;
    double weight_y1 = y - y0;
    double weight_y0 = 1 - weight_x1;
    put_pixel(x0, y0, int((weight_x0 * weight_y0) * 255));
    put_pixel(x1, y0, int((weight_x1 * weight_y0) * 255));
    put_pixel(x0, y1, int((weight_x0 * weight_y1) * 255));
    put_pixel(x1, y1, int((weight_x1 * weight_y1) * 255));
}

将x和y的权重相乘,得到虚拟像素在每个实像素内的重叠区域。我天真地认为这会给我一个完美的抗锯齿效果,但在我的测试程序中的移动像素只是显示一个可怕的闪烁。它看起来比没有任何抗锯齿的简单像素差得多。

然而,当我从乘法切换到加法时,它看起来好多了:

    put_pixel(x0, y0, int((weight_x0 + weight_y0) * 127.5));
    put_pixel(x1, y0, int((weight_x1 + weight_y0) * 127.5));
    put_pixel(x0, y1, int((weight_x0 + weight_y1) * 127.5));
    put_pixel(x1, y1, int((weight_x1 + weight_y1) * 127.5));

添加权重似乎没有任何几何意义。那么,为什么这种方法效果更好呢?第一个版本有什么问题?还有更好的方法吗?

按要求:)

直观地:你的x和y权重表示从虚拟到真实像素沿轴的距离。所以实际距离是√(wx ^2 + wy ^2)这解释了为什么和的效果更好——它比乘法更接近这种形式。

有一个bug在我的代码中潜伏了半年:

double weight_x1 = x - x0;
double weight_x0 = 1 - weight_x1;
double weight_y1 = y - y0;
double weight_y0 = 1 - weight_x1;   // BUG

你能看到虫子吗?是的,这是一个典型的复制粘贴错误:

double weight_y0 = 1 - weight_y1;   // FIXED

修复错误后,原来的乘法方法看起来非常好。