c – 抗锯齿像素渲染
作者:互联网
我正在尝试实现抗锯齿像素渲染.我的基本想法是渲染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权重表示从虚拟到真实像素的轴的距离.所以,实际距离是sqrt(w_x ^ 2 w_y ^ 2).解释为什么总和更好 – 它更接近这种形式而不是乘法.
标签:c,pixel,rendering,antialiasing 来源: https://codeday.me/bug/20190729/1575566.html