GAMES101-Assignment7

一、问题总览

在之前的练习中,我们实现了Whitted-Style Ray Tracing 算法,并且用BVH等加速结构对于求交过程进行了加速。在本次实验中,我们将在上一次实验的基础上实现完整的Path Tracing算法。

二、代码框架

2.1 修改内容

相比上一次实验,本次实验对框架的修改较大,主要在以下几方面:

  • 修改了main.cpp,以适应本次实验的测试模型CornellBox
  • 修改了Render,以适应CornellBox 并且支持Path Tracing 需要的同一Pixel多次Sample
  • 修改了Object,Sphere,Triangle,TriangleMesh,BVH,添加了area属性与Sample方法,以实现对光源按面积采样,并在Scene中添加了采样光源的接口sampleLight
  • 修改了Material 并在其中实现了sample, eval, pdf三个方法用于Path Tracing变量的辅助计算

2.2 需要迁移的内容

你需要从上一次编程练习中直接拷贝以下函数到对应位置:

  • Triangle::getIntersection in Triangle.hpp: 将你的光线-三角形相交函数粘贴到此处,请直接将上次实验中实现的内容粘贴在此。
  • IntersectP(const Ray& ray, const Vector3f& invDir, const std::array<int, 3>& dirIsNeg) in the Bounds3.hpp: 这个函数的作用是判断包围盒BoundingBox 与光线是否相交,请直接将上次实验中实现的内容粘贴在此处,并且注意检查t_enter = t_exit 的时候的判断是否正确。
  • getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp: BVH查找过程,请直接将上次实验中实现的内容粘贴在此处.

三、代码框架

在本次实验中,只需要修改这一个函数:

  • 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: 给定一对入射、出射方向与法向量,计算sample 方法得到该出射方向的概率密度
  • eval(const Vector3f wi, const Vector3f wo, const Vector3f N) in Material.cpp: 给定一对入射、出射方向与法向量,计算这种情况下的f_r值

可能用到的变量有:

  • RussianRoulette in Scene.cpp: P_RR, Russian Roulette 的概率

四、参考答案

4.1 castRay(const Ray ray, int depth)in Scene.cpp

按照本次实验给出的框架(wo定义与课程介绍相反),我们进一步可以将伪代码改写为:
在这里插入图片描述

  • 如果严格按照上述算法实现,你会发现渲染结果中光源区域为纯黑。这是因为在计算渲染方程时,没有加上自身释放的光
  • 不同于以往从观察点射出光线,上述代码是从光源射出光线。此举是为了解决光线浪费的问题(从观察点产生光线时,受光源大小的影响有些光线可能永远都不会打到光源上,导致这些光线浪费了)
  • 根据以上代码,可以将各个参数的关系画出来,如下图。黄线是 path tracing 的方向,和课上定义相反。(参考:https://zhuanlan.zhihu.com/p/488882096)
    在这里插入图片描述
// 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;
}

4.2 多线程加速

  • 在Render.cpp中使用多线程加速
  • 多线程会影响部分变量的值(如变量m),所以需要修改下函数
  • 使用#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

附件

作业7压缩包

  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值