> 本帖最后由 Z君好困 于 2023-4-12 11:44 编辑
MSAA多重采样抗锯齿处理:
在rasterizer.hpp中添加变量方法:
```cpp
std::vector(Eigen::Vector3f) frame_buf;
std::vector(Eigen::Vector3f) msaa_frame_buf;
std::vector<float> depth_buf;
std::vector<float> msaa_depth_buf;
int get_index(int x, int y);
int get_msaa_index(int x, int y);
```
在cpp中修改相应函数:
```cpp
void rst::rasterizer::clear(rst::Buffers buff)
{
if ((buff & rst::Buffers::Color) == rst::Buffers::Color)
{
std::fill(frame_buf.begin(), frame_buf.end(), Eigen::Vector3f{ 0, 0, 0 });
std::fill(msaa_frame_buf.begin(), msaa_frame_buf.end(), Eigen::Vector3f{ 0, 0, 0 });
}
if ((buff & rst::Buffers::Depth) == rst::Buffers::Depth)
{
std::fill(depth_buf.begin(), depth_buf.end(), std::numeric_limits<float>::infinity());
std::fill(msaa_depth_buf.begin(), msaa_depth_buf.end(), std::numeric_limits<float>::infinity());
}
}
rst::rasterizer::rasterizer(int w, int h) : width(w), height(h)
{
frame_buf.resize(w * h);
depth_buf.resize(w * h);
msaa_frame_buf.resize(w * h * 4);
msaa_depth_buf.resize(w * h * 4);
}
int rst::rasterizer::get_msaa_index(int x, int y)
{
return (height*2 - 1 - y) * width*2 + x;
}
```
最后修改光栅化函数:
确定一个2*2的四格采样点,分别对每个像素内的四个采样点做深度和颜色值的记录,最后通过深度测试的进行颜色加和。
```cpp
void rst::rasterizer::rasterize_triangle(const Triangle& t) {
auto v = t.toVector4();
std::vector<Eigen::Vector2f> Multiple_sampler_step
{
{0.25,0.25},
{0.25,0.75},
{0.75,0.25},
{0.75,0.75},
};
// 画出三角形所在边界
// x_l = x_min ; x_r = x_max ; y_b = y_min ; y_t = y_max
int x_l = std::floor(std::min(v, std::min(v, v))); //floor向下取整
int x_r = std::ceil(std::max(v, std::max(v, v))); //ceil向上取整
int y_b = std::floor(std::min(v, std::min(v, v)));
int y_t = std::ceil(std::max(v, std::max(v, v)));
for (int x = x_l; x <= x_r; x++)
for (int y = y_b; y <= y_t; y++) {
bool pass = false;//判断深度测试是否通过
for (int i = 0; i < 4; i++)
{
if (insideTriangle(x + Multiple_sampler_step, y + Multiple_sampler_step, t.v)) {
auto tup = computeBarycentric2D(x + Multiple_sampler_step, y + Multiple_sampler_step, t.v);//重心坐标插值
float alpha, beta, gamma;
std::tie(alpha, beta, gamma) = tup;
float w_reciprocal = 1.0f / (alpha / v.w() + beta / v.w() + gamma / v.w());
float z_interpolated =
alpha * v.z() / v.w() + beta * v.z() / v.w() + gamma * v.z() / v.w();
z_interpolated *= w_reciprocal;
if (msaa_depth_buf > z_interpolated) {
pass = true;
//将深度值和颜色值存入相应缓冲
msaa_depth_buf = z_interpolated;
msaa_frame_buf = t.getColor();
}
}
}
if (pass)
{
Vector3f point{ (float)x,(float)y,0 };
Vector3f color = (msaa_frame_buf + msaa_frame_buf +
msaa_frame_buf + msaa_frame_buf) / 4;
set_pixel(point, color);
}
}
}
```
页:
[1]