Games101 hw2 advanced

文章详细描述了如何在三角形渲染中实现MSAA(多重采样抗锯齿),包括使用fineness变量计算子像素样本颜色并进行平均,以及两种方法(简单MSAA和更好MSAA)来处理每个像素内的多边形判断和颜色合并。
摘要由CSDN通过智能技术生成

 two parts included:

1. write the bool function insideTriangle

2. rasterize function

MSAA means that there will be supersampling inside each pixel. Yan supposed there are 4 samples. We need to figure out whether these samples of each pixel are inside the triangle separately.

Two ways to do this:

1. Define a variable fineness, which is used as the color percentage of the final pixel.

2. Calculates the color of each sub-pixel sample individually and averages them to get the final color of the pixel.

// // MSAA
    // for (int x = xmin; x <= xmax; ++x)
    // {
    //     for (int y = ymin; y <= ymax; ++y)
    //     {
    //         float fineness = 0;
    //         if (insideTriangle(x + 0.25, y + 0.25, t.v)) fineness += 0.25;
    //         if (insideTriangle(x + 0.25, y + 0.75, t.v)) fineness += 0.25;
    //         if (insideTriangle(x + 0.75, y + 0.25, t.v)) fineness += 0.25;
    //         if (insideTriangle(x + 0.75, y + 0.75, t.v)) fineness += 0.25;

    //         if (fineness != 0)
    //         {
    //             auto[alpha, beta, gamma] = computeBarycentric2D(x, y, t.v);
    //             float w_reciprocal = 1.0/(alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
    //             float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
    //             z_interpolated *= w_reciprocal;

    //             if (z_interpolated < depth_buf[get_index(x, y)])
    //             {
    //                 set_pixel(Vector3f(x, y, z_interpolated), t.getColor() * fineness);
    //                 depth_buf[get_index(x, y)] = z_interpolated;
    //             }
    //         }
    //     }
    // }
    
    // Better MSAA
    for (int x = xmin; x <= xmax; ++x)
    {
        for (int y = ymin; y <= ymax; ++y)
        {
            Vector3f color_sum(0, 0, 0);
            int sample_count = 0;
            std::vector<Vector2f> offsets = {{0.25, 0.25}, {0.25, 0.75}, {0.75, 0.25}, {0.75, 0.75}};

            for (const auto& offset : offsets)
            {
                if (insideTriangle(x + offset.x(), y + offset.y(), t.v))
                {
                    auto[alpha, beta, gamma] = computeBarycentric2D(x, y, t.v);
                    float w_reciprocal = 1.0/(alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
                    float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
                    z_interpolated *= w_reciprocal;
                    if (z_interpolated < depth_buf[get_index(x, y)])
                    {
                        color_sum += t.getColor();
                        depth_buf[get_index(x, y)] = z_interpolated;
                        sample_count++;
                    }
                }
            }
            if (sample_count > 0)
            {
                set_pixel(Vector3f(x, y, depth_buf[get_index(x, y)]), color_sum / sample_count);
            }
        }
    }

Reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值