1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| void rst::rasterizer::rasterize_triangle(const Triangle& t) { auto v = t.toVector4(); auto ext_pt_x = std::minmax_element(v.begin(), v.end(), [](const Eigen::Vector4f& lhs, const Eigen::Vector4f & rhs){ return lhs.x() < rhs.x(); }); auto ext_pt_y = std::minmax_element(v.begin(), v.end(), [](const Eigen::Vector4f& lhs, const Eigen::Vector4f & rhs){ return lhs.y() < rhs.y(); });
int min_x = floor(ext_pt_x.first->x()); int max_x = ceil(ext_pt_x.second->x()); int min_y = floor(ext_pt_y.first->y()); int max_y = ceil(ext_pt_y.second->y());
Eigen::Vector3f v3f[3]; for (int idx : {0, 1, 2}) { v3f[idx] << v[idx].x(), v[idx].y(), 1.0f; }
for (int x=min_x;x<=max_x;++x) { for (int y=min_y;y<=max_y;++y) { int sup_px_cnt = 0; for (int x_sup: {0, 1}) { for (int y_sup: {0, 1}) { float x_pos = (float)x + 0.25 + x_sup*0.5; float y_pos = (float)y + 0.25 + y_sup*0.5;
auto[alpha, beta, gamma] = computeBarycentric2D(x_pos, y_pos, 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;
int buff_ind = (x*2 + x_sup) + (y*2 + y_sup)*width*2; if (insideTriangle(x_pos, y_pos, v3f)) { if ((-z_interpolated<depth_buf_msaa2x2[buff_ind])) { depth_buf_msaa2x2[buff_ind] = -z_interpolated; ++sup_px_cnt; } } } } if ((sup_px_cnt>0)) { float intensity = (float)sup_px_cnt / 4.0f; mix_pixel(Eigen::Vector3f(x, y, 1.0f), t.getColor()*intensity); } }
} }
|