在之前的练习中,我们实现了Whitted-Style Ray Tracing 算法,并且用BVH等加速结构对于求交过程进行了加速。在本次实验中,我们将在上一次实验的基础上实现完整的Path Tracing算法。
相比上一次实验,本次实验对框架的修改较大,主要在以下几方面:
你需要从上一次编程练习中直接拷贝以下函数到对应位置:
在本次实验中,只需要修改这一个函数:
可能用到的函数有:
可能用到的变量有:
按照本次实验给出的框架(wo定义与课程介绍相反),我们进一步可以将伪代码改写为:
// Implementation of Path Tracing
Vector3f Scene::castRay(const Ray &ray, int depth) const
{
// TO DO Implement Path Tracing Algorithm here
Intersection intersection = Scene::intersect(ray);
if(intersection.happened) {
Material *m = intersection.m;//Matiral
Vector3f L_dir(0.0), L_indir(0.0);
// sampleLight(inter, pdf_light)
Intersection inter;
float pdf_light;
sampleLight(inter, pdf_light);
// Get x, ws, NN, emit from inter
Vector3f X = inter.coords;
Vector3f P = intersection.coords;
Vector3f ws = (X - P).normalized();
Vector3f N = intersection.normal;
Vector3f NN = inter.normal;
Vector3f emit = inter.emit;
// Shoot a ray from p to x(inter)
Ray ray_p_x(P + EPSILON * N, ws);
Intersection intersection_p_x = Scene::intersect(ray_p_x);
// If the ray is not blocked in the middle
if((intersection_p_x.coords - inter.coords).norm() < EPSILON) {
L_dir = emit * m->eval(ray.direction, ws, N)
* dotProduct(ws, N)
* dotProduct(-ws, NN)
/ std::pow(intersection_p_x.distance,2)
/ pdf_light;
}
// 使用俄罗斯轮盘赌判断反射光线是否应停止继续反射
if(get_random_float() <= RussianRoulette) {
// Trace a ray r(p, wi)
Vector3f wi = m->sample(ray.direction, N).normalized();
Ray r(P, wi);
Intersection intersection_r = Scene::intersect(r);
// If ray r hit a non-emitting object at q
if(intersection_r.happened && !intersection_r.m->hasEmission()) {
L_indir = castRay(r, depth+1)
* m->eval(ray.direction, wi, N)
* dotProduct(wi, N)
/ m->pdf(ray.direction, wi, N)
/ RussianRoulette;
}
}
return m->getEmission() + L_dir + L_indir;
}
return this->backgroundColor;
}
#pragma omp parallel for
, 然后在CMakeLists.txt中加上set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
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(278, 273, -800);
int m = 0;
// change the spp value to change sample ammount
int spp = 16;
std::cout << "SPP: " << spp << "\n";
#pragma omp parallel for shared(m)
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;
Vector3f dir = normalize(Vector3f(-x, y, 1));
for (int k = 0; k < spp; k++){
//framebuffer[m] += scene.castRay(Ray(eye_pos, dir), 0) / spp;
framebuffer[j * scene.width + i] += scene.castRay(Ray(eye_pos, dir), 0) / spp;
}
//m++;
}
//UpdateProgress(j / (float)scene.height);
UpdateProgress(m / (float)scene.height);
m++;
}
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 * std::pow(clamp(0, 1, framebuffer[i].x), 0.6f));
color[1] = (unsigned char)(255 * std::pow(clamp(0, 1, framebuffer[i].y), 0.6f));
color[2] = (unsigned char)(255 * std::pow(clamp(0, 1, framebuffer[i].z), 0.6f));
fwrite(color, 1, 3, fp);
}
fclose(fp);
}
mkdir build
cd ./build
cmake ..
make
./RayTracing