本文为闫令琪老师的GAMES 101课程的作业7的个人实现,文中如有错漏欢迎指出。
作业要求
castRay(const Ray ray, int depth)
in Scene.cpp: 在其中实现 Path Tracing 算法
可能用到的函数:
-
intersect(const Ray ray)
in Scene.cpp: 求一条光线与场景的交点 -
sampleLight(Intersection pos, float pdf)
in Scene.cpp: 在场景的所有光源上按面积 uniform 地 sample 一个点,并计算该 sample 的概率密度 -
sample(const Vector3f wi, const Vector3f N)
in Material.cpp: 按照该材质的性质,给定入射方向与法向量,用某种分布采样一个出射方向 -
pdf(const Vector3f wi, const Vector3f wo, const Vector3f N)
in Material.cpp: 给定一对入射、出射方向与法向量,计算该出射方向的概率密度 -
eval(const Vector3f wi, const Vector3f wo, const Vector3f N)
in Material.cpp: 给定一对入射、出射方向与法向量,计算这种情况下的f_r
值
代码实现
Path Tracing的理论知识,在此不多赘述。下图为作业文档中提供的伪代码,我们要做的就是把它写成C++代码:
castRay
// Implementation of Path Tracing
Vector3f Scene::castRay(const Ray &ray, int depth) const
{
Vector3f L_dir, L_indir;
// TODO Implement Path Tracing Algorithm here
// ray and scene intersect at p
Intersection inter_p = intersect(ray);
if (!inter_p.happened)
{
return Vector3f();
}
if (inter_p.m->hasEmission())
{
return inter_p.m->getEmission();
}
Vector3f& p = inter_p.coords;
Vector3f& N = inter_p.n