games101作业2

作业总览

  • 这次的作业是让我们栅格化三角形,就是让我们对三角形进行着色处理。
    在这里插入图片描述

  • 在这之前你需要把上次作业mian()函数中get_model_matrix(),get_projection_matrix()这两个函数中的代码复制粘贴过来。

  • 接着实现rasterizer.cpp中的两个函数,如下图
    在这里插入图片描述

具体实现

  • insideTriangle()
// 测试点是否在三角形内
static bool insideTriangle(int x, int y, const Vector3f* _v)
{   
    // TODO : Implement this function to check if the point (x, y) is inside the triangle represented by _v[0], _v[1], _v[2]
    bool flag = false;
    Vector3f p = {x,y,0};
    Eigen::Vector3f v1,v2,v3;
    Eigen::Vector3f tmp1,tmp2;
    tmp1 = _v[1] - _v[0];
    tmp2 = p - _v[0];
    v1 = tmp1.cross(tmp2);

    tmp1 = _v[2] - _v[1];
    tmp2 = p - _v[1];
    v2 = tmp1.cross(tmp2);

    tmp1 = _v[0] - _v[2];
    tmp2 = p - _v[2];
    v3 = tmp1.cross(tmp2);

    if(v1.z() > 0 && v2.z() > 0 && v3.z() > 0) flag = true;  //在三角形内部
    if(v1.z() < 0 && v2.z() < 0 && v3.z() < 0) flag = true;

    return flag;
}
  • rasterize_triangle()
    • 找到三角形的Bounding Box
    • 然后对这个Bounding Box中的像素进行遍历
    • 看是否在三角形的内部
    • 在内部接着判断该像素的深度是不是比原来记录该像素的深度更靠近相机
    • 更靠近相机也就是深度更浅就更新该位置的depth_buf 以及该像素的颜色
void rst::rasterizer::rasterize_triangle(const Triangle& t) {
    auto v = t.toVector4();
    
    // TODO : Find out the bounding box of current triangle.
    // iterate through the pixel and find if the current pixel is inside the triangle
    //1. 找到Bounding Box
    int xmin = std::min(std::min(v[0].x(), v[1].x()), v[2].x());
    int ymin = std::min(std::min(v[0].y(), v[1].y()), v[2].y());
    int xmax = std::max(std::max(v[0].x(), v[1].x()), v[2].x());
    int ymax = std::max(std::max(v[0].y(), v[1].y()), v[2].y());

    for(int i=xmin;i<=xmax;i++)
    {
        for(int j=ymin;j<=ymax;j++)
        {
            if(insideTriangle(i,j,t.v)){  //点在三角形内部
                auto[alpha, beta, gamma] = computeBarycentric2D(i, j, 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(i,j)]){
                    set_pixel(Eigen::Vector3f(i,j,z_interpolated),t.getColor());
                    depth_buf[get_index(i,j)] = z_interpolated;
                }
            }
        }
    }

    // If so, use the following code to get the interpolated z value.
    //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;

    // TODO : set the current pixel (use the set_pixel function) to the color of the triangle (use getColor function) if it should be painted.
}

运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

译制片~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值