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
| struct SamplePoint { float p_x; float p_y; int isInsideTriangle;
SamplePoint(float _x, float _y, int _isInsideTriangle) { p_x = _x; p_y = _y; isInsideTriangle = _isInsideTriangle; } };
for (int x = x_min; x < x_max; x++) { for (int y = y_min; y < y_max; y++) { std::vector<SamplePoint> samplePoint;
samplePoint.push_back(SamplePoint(x + 0.25, y + 0.25, insideTriangle(x + 0.25, y + 0.25, t.v) == true ? 1 : 0)); samplePoint.push_back(SamplePoint(x + 0.25, y + 0.75, insideTriangle(x + 0.25, y + 0.75, t.v) == true ? 1 : 0)); samplePoint.push_back(SamplePoint(x + 0.75, y + 0.25, insideTriangle(x + 0.75, y + 0.25, t.v) == true ? 1 : 0)); samplePoint.push_back(SamplePoint(x + 0.75, y + 0.75, insideTriangle(x + 0.75, y + 0.75, t.v) == true ? 1 : 0));
float minDep = INT_MAX; float avg_color = 0;
for (int i = 0; i < samplePoint.size(); i++) { if (samplePoint[i].isInsideTriangle > 0) { auto result = computeBarycentric2D(samplePoint[i].p_x, samplePoint[i].p_y, t.v); float alpha, beta, gamma; std::tie(alpha, beta, gamma) = result; 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;
minDep = std::min(z_interpolated, minDep); avg_color += 0.25; } }
if (minDep < depth_buf[get_index(x, y)]) { Eigen::Vector3f point = Eigen::Vector3f(x, y, 1); set_pixel(point, t.getColor() * avg_color); depth_buf[get_index(x, y)] = minDep; }
} }
|