Games101-hw6

Assignment 6

1. Basic

1.1 Render() in Renderer.cpp

这一部分直接按照作业5即可,在使用castRay函数时需要进行改动。

注意:记得对dir向量归一化。

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
void Renderer::Render(const Scene& scene)
{
std::vector<Vector3f> framebuffer(scene.width * scene.height);

float scale = tan(deg2rad(scene.fov * 0.5));
float imageAspectRatio = scene.width / (float)scene.height;
Vector3f eye_pos(-1, 5, 10);
int m = 0;
for (uint32_t j = 0; j < scene.height; ++j) {
for (uint32_t i = 0; i < scene.width; ++i) {
// generate primary ray direction
float x = (2 * (i + 0.5) / (float)scene.width - 1) *
imageAspectRatio * scale;
float y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale;
// TODO: Find the x and y positions of the current pixel to get the
// direction
// vector that passes through it.
// Also, don't forget to multiply both of them with the variable
// *scale*, and x (horizontal) variable with the *imageAspectRatio*
// x = (2 * (i + 0.5f) / (float)(scene.width-1) - 1) * imageAspectRatio * scale;
// y = (1 - 2 * (j + 0.5f) / (float)(scene.height-1)) * scale;

/*作业6添加部分*/
Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
Ray ray(eye_pos, normalize(dir));
framebuffer[m++] = scene.castRay(ray, 0);
/*作业6添加部分*/

// Don't forget to normalize this direction!

}
UpdateProgress(j / (float)scene.height);
}
UpdateProgress(1.f);

// save framebuffer to file
FILE* fp = fopen("binary.ppm", "wb");
(void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height);
for (auto i = 0; i < scene.height * scene.width; ++i) {
static unsigned char color[3];
color[0] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].x));
color[1] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].y));
color[2] = (unsigned char)(255 * clamp(0, 1, framebuffer[i].z));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}

1.2 Triangle::getIntersection in Triangle.hpp

这一部分出错较多。首先需要阅读一下框架中的计算,将其中参与运算的变量对应上之前作业5中的计算中间变量。需要编写的部分其实很简单,但是需要注意要把inter的各个成员变量都进行赋值,尤其注意不能漏掉m,normal,obj这些,同时赋值时需要注意参看上面的类的定义。

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
inline Intersection Triangle::getIntersection(Ray ray)
{
Intersection inter;

if (dotProduct(ray.direction, normal) > 0)
return inter;
double u, v, t_tmp = 0;
Vector3f pvec = crossProduct(ray.direction, e2); //s1
double det = dotProduct(e1, pvec); //dotProduct(s1, e1)
if (fabs(det) < EPSILON)
return inter;

double det_inv = 1. / det;
Vector3f tvec = ray.origin - v0; //s
u = dotProduct(tvec, pvec) * det_inv; //b1
if (u < 0 || u > 1)
return inter;
Vector3f qvec = crossProduct(tvec, e1); //s2
v = dotProduct(ray.direction, qvec) * det_inv; //b2
if (v < 0 || u + v > 1)
return inter;
t_tmp = dotProduct(e2, qvec) * det_inv;

// TODO find ray triangle intersection
if (t_tmp>0.0f)
{
inter.distance = t_tmp;
// inter.coords = Vector3f(u, v, 1.0); //**Wrong.
// inter.coords = ray(t_tmp);
inter.coords = ray.origin + ray.direction * t_tmp;
inter.happened = true;
inter.m = m; //**Missed. Pay attention to the "m" above!(int class definition)
inter.normal = normal; //*Missed. But what is "normal" referring to ?
inter.obj = this; //*Missed. obj? this?
}


return inter;
}

1.3 IntersectP(const Ray& ray, const Vector3f& invDir,const std::array& dirIsNeg) in the Bounds3.hpp

这部分没出什么问题,只需要注意一下eigen库的使用,不要把中括号和小括号搞反。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
const std::array<int, 3>& dirIsNeg) const
{
// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply
// is faster that Division
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)],
// use this to simplify your logic
// TODO test if ray bound intersects
double tenter, texit, tmin, tmax;
texit = std::numeric_limits<double>::max();
tenter = 0.0;
for (int i = 0;i < 3;i++)
{
double tmin = std::min((pMin[i] - ray.origin[i]) * invDir[i],
(pMax[i] - ray.origin[i]) * invDir[i]);
double tmax = std::max((pMin[i] - ray.origin[i]) * invDir[i],
(pMax[i] - ray.origin[i]) * invDir[i]);
tenter = std::max(tenter, tmin);
texit = std::min(texit, tmax);
}
if (tenter<texit && texit>0)
return true;
return false;
}

1.4 getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp

这部分也相对简单,出错的地方是对于叶子节点返回其中所有物体表面与光线相交的最近点时没写对,这也需要注意相关类的定义。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
{
// TODO Traverse the BVH to find intersection
Intersection isect;
std::array<int, 3> dirIsNeg;
for (int i=0;i<3;i++)
dirIsNeg[i] = int(ray.direction[i]>0);
if (!node->bounds.IntersectP(ray, ray.direction_inv, dirIsNeg))
return isect; //若光线与bounding box不相交,结束
//若相交
//叶子节点,判断节点内部所有物体,返回最近的交点
if (node->left == nullptr && node->right == nullptr)
// return Intersect(ray); *Wrong.
return node->object->getIntersection(ray); //Pay attention.
Intersection hit1 = getIntersection(node->left, ray);
Intersection hit2 = getIntersection(node->right, ray);
//若节点不是叶子节点,返回两个子节点交点的最近值
if (hit1.distance < hit2.distance)
return hit1;
else
return hit2;
}

1.5 基础部分实现效果

image-20220227222351813


Games101-hw6
http://example.com/2022/03/02/Games101-hw6/
作者
Thunderbolt
发布于
2022年3月2日
许可协议