Skip to content

Commit 0d2db14

Browse files
author
root
committed
homework2 add msaa
1 parent 4c7b199 commit 0d2db14

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Assignment2/codes/rasterizer.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,23 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {
131131
int minY = std::min(std::min(v[0].y(), v[1].y()), v[2].y());
132132
int maxX = std::max(std::max(v[0].x(), v[1].x()), v[2].x());
133133
int maxY = std::max(std::max(v[0].y(), v[1].y()), v[2].y());
134+
//MSAA超采样(有黑边),着色时仅仅考虑了背景色和三角形颜色,没有考虑其他三角形的颜色
135+
std::vector<Eigen::Vector2f> super_sample_step {
136+
{0.25,0.25},
137+
{0.75,0.25},
138+
{0.25,0.75},
139+
{0.75,0.75},
140+
};
134141

135142
for (int x = minX; x < maxX; x++) {
136143
for (int y = minY; y < maxY; y++) {
137-
if (insideTriangle(x + 0.5, y + 0.5, t.v)) {
144+
int count = 0;
145+
for (int z = 0; z < 4; z++) {
146+
if (insideTriangle(x + super_sample_step[z][0], y + super_sample_step[z][1], t.v)) {
147+
count++;
148+
}
149+
}
150+
if (count > 0) {
138151
auto[alpha, beta, gamma] = computeBarycentric2D(x + 0.5, y + 0.5, t.v);
139152
float w_reciprocal = 1.0/(alpha / v[0].w() + beta / v[1].w() + gamma / v[2].w());
140153
float z_interpolated = alpha * v[0].z() / v[0].w() + beta * v[1].z() / v[1].w() + gamma * v[2].z() / v[2].w();
@@ -145,7 +158,7 @@ void rst::rasterizer::rasterize_triangle(const Triangle& t) {
145158
depth_buf[cur_index] = z_interpolated;
146159
Vector3f vertex;
147160
vertex << x, y, z_interpolated;
148-
set_pixel(vertex, t.getColor());
161+
set_pixel(vertex, t.getColor() * count / 4);
149162
}
150163
}
151164
}

0 commit comments

Comments
 (0)