GAMES101-Assignment6

一、问题总览

需要加速结构来加速光线与场景的交点,本次练习中,重点关注物体划分算法Bounding Volume Hierarchy (BVH)。本练习要求实现Ray-Bounding Volume求交与BVH查找。
需要从上一次编程练习中引用以下函数:

  • Render() in Renderer.cpp: 将你的光线生成过程粘贴到此处,并且按照新框架更新相应调用的格式。
  • Triangle::getIntersection in Triangle.hpp: 将你的光线-三角形相交函数粘贴到此处,并且按照新框架更新相应相交信息的格式。

在本次编程练习中,你需要实现以下函数:

  • IntersectP(const Ray& ray, const Vector3f& invDir,
    const std::array<int, 3>& dirIsNeg) in the Bounds3.hpp: 这个函数的作用是判断包围盒BoundingBox与光线是否相交,需要按照课程介绍的算法实现求交过程。
  • getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp: 建立BVH之后,我们可以用它加速求交过程。该过程递归进行,你将在其中调用你实现的Bounds3::IntersectP.

二、代码框架

修改了代码框架中的如下内容:

  • Material.hpp: 我们从将材质参数拆分到了一个单独的类中,现在每个物体实例都可以拥有自己的材质。
  • Intersection.hpp: 这个数据结构包含了相交相关的信息。
  • Ray.hpp: 光线类,包含一条光的源头、方向、传递时间t和范围range.
  • Bounds3.hpp: 包围盒类,每个包围盒可由pMin和pMax两点描述(请思考为什么)。Bounds3::Union函数的作用是将两个包围盒并成更大的包围盒。与材质一样,场景中的每个物体实例都有自己的包围盒。
  • BVH.hpp: BVH加速类。场景scene拥有一个BVHAccel实例。从根节点开始,我们可以递归地从物体列表构造场景的BVH.

三、参考答案

3.1 Render() in Renderer.cpp

  • 从观察点向屏幕栅格一次发出若干条光线
    • 栅格每个单元只有一条光线穿过
    • 将穿过栅格的每一条光线按列优先的放射保存在framebuffer中
  • scene.castRay实现了Whitted-syle光线追踪算法,需要将光线(方向和起始点)和深度(光线反射次数)作为参数
    for (uint32_t j = 0; j < scene.height; ++j) {
        for (uint32_t i = 0; i < scene.width; ++i) {
            // generate primary ray direction
            //计算栅格单元[i, j]中心对应的坐标[x, y]
            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*

            // Don't forget to normalize this direction!
            //假设观察点和屏幕距离为1,观察点位于原点,向z轴负方向观察
            //所以从观察点到栅格中心点的光线z = -1
            Vector3f dir = normalize(Vector3f(x, y, -1));
            //观察点的起始位置在eye_pos,但是观察点和屏幕相对位置不变,观察朝向不变
            //所以,光线方向不变
            Ray ray(eye_pos, dir);
            framebuffer[m++] = scene.castRay(ray, 0);
        }
        UpdateProgress(j / (float)scene.height);
    }

3.2 Triangle::getIntersection in Triangle.hpp

  • 光线-三角形相交函数,并且更新相应相交信息的格式
    在这里插入图片描述
inline Intersection Triangle::getIntersection(Ray ray)
{
    Intersection inter;

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

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

    // TODO find ray triangle intersection
    if (t_tmp < 0) //时间为负,则说明没有交点
        return inter;
    // 更新交点信息
    inter.distance = t_tmp;
    inter.happened = true;
    inter.m = m;
    inter.coords = Vector3f(ray.origin + t_tmp * ray.direction);
    inter.normal = normal;
    inter.obj = this;

    return inter;
}

3.3 IntersectP in the Bounds3.hpp

  • 这个函数的作用是判断包围盒BoundingBox与光线是否相交。
    在这里插入图片描述

    • 第一幅图的红色线段与第二幅图的红色线段求交集,得到最终结果(第三幅图的线段)
      • 第一幅图的红色线段为光线和x0, x1两对面的交点连线
    • tenter = max{tmin}
      • tenter:光线射入包围盒的时间t
      • tmin:光线射入某一个对面的时间t
    • texit = min{tmax}
      • 小于零,表示盒子在光线背后(不满足光线与盒子有交点)
      • (texit >= 0 ) && (texit < 0): 在盒子内,射线可能和盒子由交点
    • if (tenter < texit) && (texit > 0):光线在盒内停留了一段时间

在这里插入图片描述

  • 包围盒是轴对齐的(垂直于某条坐标轴),方便求光线和面的交点t(如上图下方式子所示)
    • 光线方程:r(t) = o + td
      • o:起始点三维坐标;d:光线方向;t:常数
    • 对面垂直于x轴:t * 光线x轴分量 = 包围盒上一点的x分量 - 光源起始点x分量
      • 因为对面是无限大的两个平面,光线暂时看作直线,所以一定有交点;
      • 最后判断光线作为射线时是否与对面有交点
        • if (tenter < texit) && (texit > 0)光线作为射线沿着方向d与对面有交点
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 tx_min = (pMin.x - ray.origin.x) * invDir.x;
    double tx_max = (pMax.x - ray.origin.x) * invDir.x;
    //注意下pMin.x是指包围盒左边的面
    // 假如光线是反向(从右往左),那么光线离包围盒pMin.x距离(tx_min)远
    if (!dirIsNeg[0])
        std::swap(tx_min, tx_max);

    double ty_min = (pMin.y - ray.origin.y) * invDir.y;
    double ty_max = (pMax.y - ray.origin.y) * invDir.y;
    if (!dirIsNeg[1])
        std::swap(ty_min, ty_max);
    
    double tz_min = (pMin.z - ray.origin.z) * invDir.z;
    double tz_max = (pMax.z - ray.origin.z) * invDir.z;
    if (!dirIsNeg[2])
        std::swap(tz_min, tz_max);

    double t_enter = std::max(tx_min, std::max(ty_min, tz_min));
    double t_exit = std::min(tx_max, std::min(ty_max, tz_max));


    return t_enter < t_exit && t_exit >= 0;
}

3.4 getIntersection in BVH.cpp

  • 建立BVH之后,我们可以用它加速求交过程。该过程递归进行,你将在其中调用你实现的Bounds3::IntersectP
  • BVH特点
    在这里插入图片描述
    • 按照物体进行划分,因此一个物体只可能出现在一个包围盒内
      • 比如把三角形分成两部分,然后重新求包围盒
  • getIntersection伪代码
    在这里插入图片描述
Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
{
    // TODO Traverse the BVH to find intersection
    Intersection isect;
    // 光线没有碰到包围盒
    if(!node->bounds.IntersectP(ray, ray.direction_inv, std::array<int, 3>{ray.direction.x>0, ray.direction.y>0, ray.direction.z>0}) )
        return isect;
    // 叶节点
    if(node->left == nullptr && node->right == nullptr)
        return node->object->getIntersection(ray);

    // 中间节点
    Intersection isect_left, isect_right;
    isect_left = getIntersection(node->left, ray);
    isect_right = getIntersection(node->right, ray);

    //返回最近的交点
    return isect_left.distance <= isect_right.distance ? isect_left : isect_right;
}

四、编译

mkdir build
cd ./build
cmake ..
make

./RayTracing

附件

作业6压缩包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值