hw2

在屏幕上画出一个实心三角形.
相较于hw1,多了一些内容,需要好好看下框架.

判断$(x,y)$对应的pixel否在三角形内部.

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
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 vertex2p[3], vertex2vertex[3];
int i;
//储存顶点到测试点的向量
for (i = 0;i < 3;i++) {
vertex2p[i] << x - _v[i].x(), y - _v[i].y();
}
//储存顶点到顶点之间的向量
for (i = 0;i < 3;i++) {
vertex2vertex[i] << _v[(i + 1) % 3].x() - _v[i].x(), _v[(i + 1) % 3].y() - _v[i].y();
}
//计算叉乘法.
int result[3];
for (i = 0;i < 3;i++) {
result[i] = vertex2vertex[i].x()*vertex2p[i].y()
-vertex2vertex[i].y()*vertex2p[i].x();
}
if(result[0]>0 && result[1]>0 && result[2]>0){
return true;
}
else if(result[0]<0 && result[1]<0 && result[2]<0){
return true;
}
else{
return false;
}

}

先限定下founding box范围,提高效率

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
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
float xmin,ymin,xmax,ymax;
xmin=std::min(std::min(v[0].x(),v[1].x()),v[2].x());
ymin=std::min(std::min(v[0].y(),v[1].y()),v[2].y());
xmax=std::max(std::max(v[0].x(),v[1].x()),v[2].x());
ymax=std::max(std::max(v[0].y(),v[1].y()),v[2].y());
xmin=std::floor(xmin);
xmax=std::ceil(xmax);
ymin=std::floor(ymin);
ymax=std::ceil(ymax);

for(int x=xmin;x<=xmax;x++){
for(int y=ymin;y<=ymax;y++){
if(insideTriangle(x+0.5,y+0.5,t.v)){
// 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.
if (z_interpolated<depth_buf[get_index(x,y)]){
set_pixel(Vector3f(x,y,z_interpolated),t.getColor());
depth_buf[get_index(x,y)]=z_interpolated;
}
}
}
}