Games101 作业2

是否在三角形内

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]
    Vector2f v0(_v[0].x(), _v[0].y());
    Vector2f v1(_v[1].x(), _v[1].y());
    Vector2f v2(_v[2].x(), _v[2].y());
    Vector2f point(x, y);

    Vector2f AB = v1 - v0;
    Vector2f CA = v0 - v2;
    Vector2f AP = point - v0;
    Vector2f BC = v2 - v1;
    Vector2f BP = point - v1;

   float crossAB_AP = crossProduct(AB, AP);
   float crossAC_AP = crossProduct(CA, AP);
   float crossBC_BP = crossProduct(BC, BP);

   // 检查点是否在边 AB 和 AC 的同一侧  
    bool isSameSideAB = (crossAB_AP >= 0.0f);  
    bool isSameSideAC = (crossCA_AP >= 0.0f);  
  
    // 如果点不在 AB 和 AC 的同一侧,则点不在三角形内  
    if (isSameSideAB != isSameSideAC) return false;     
  
    // 如果点不在 BC 的同一侧(与 AB 和 AC 相同的一侧),则点不在三角形内  
    if (isSameSideAB != (crossBC_BP >= 0.0f)) return false;  

   if (point.isApprox(v0) || point.isApprox(v1) || point.isApprox(v2))
   {
    return true;
   }
   
   return true;
}

光栅化

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

    // 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.
     rst::BoundingBox2D bbox;
    bbox = createBoundingBox2D(v);
    for (int x = bbox.minX; x < bbox.maxX; ++x)
    {
        for (int y = bbox.minY; y < bbox.maxY; ++y)
        {
           bool isInside = insideTriangle(x,y, t.v);
           if (isInside)
           {
            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;

            std::size_t index = static_cast<std::size_t>(get_index(x, y));
            Eigen::Vector3f point(x,y,z_interpolated);
            if (index < depth_buf.size() && z_interpolated < depth_buf[index])
            {
                
                Eigen::Vector3f interpolated_color = t.getColor();
                depth_buf[index] = z_interpolated;
               
                set_pixel(point, interpolated_color);
            }
          
           }
        }
    }
}

./rasterizer aa.png在这里插入图片描述./rasterizer aa.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值