/* * 在三角形内检测函数 * parameters: * point:x,y * triangle:_v */ staticboolinsideTriangle(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] // float xp = x, yp = y; // float xa = _v[0].x(), ya = _v[0].y(); // float xb = _v[1].x(), yb = _v[1].y(); // float xc = _v[2].x(), yc = _v[2].y();
//Screen space rasterization //此版本为使用MSAA版本,如不使用直接使用insideTriangle函数同时判断z-buffer即可 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 int left_bound = std::min(v[0].x(), std::min(v[1].x(), v[2].x())); int right_bound = std::max(v[0].x(), std::max(v[1].x(), v[2].x())); int bottom_bound = std::min(v[0].y(), std::min(v[1].y(), v[2].y())); int up_bound = std::max(v[0].y(), std::max(v[1].y(), v[2].y()));
// 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. Eigen::Vector3f p; for(int x = left_bound;x<=right_bound;x++){ for (int y = bottom_bound; y <= up_bound; y++) { // 使用MSAA float percent = insideTrianglePercent(x,y,t.v,4); if (percent>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;