GAMES101 作业6——光线追踪2(Ray-Bounding Volume求交与BVH查找)
参考资料:
https://blog.csdn.net/qq_41765657/article/details/121865049
作业描述
在之前的编程练习中,我们实现了基础的光线追踪算法,具体而言是光线传输、光线与三角形求交。我们采用了这样的方法寻找光线与场景的交点:遍历场景中的所有物体,判断光线是否与它相交。在场景中的物体数量不大时,该做法可以取得良好的结果,但当物体数量增多、模型变得更加复杂,该做法将会变得非常低效。因此,我们需要加速结构来加速求交过程。在本次练习中,我们重点关注物体划分算法 Bounding Volume Hierarchy (BVH)。本练习要求你实现 Ray-Bounding Volume 求交与 BVH 查找。
本次代码的流程为:
- 从 main 函数开始,定义了场景的参数。
- 生成物体对象(object),包含顶点,材质以及对应的 bounding box 和加速结构 BVH
- 向场景中添加物体,并构建场景的 BVH
- 调用渲染器对物体进行渲染,其中需要通过 BVH 来加速判断光线与场景中物体的求交
首先,你需要从上一次编程练习中引用以下函数:
Render() in Renderer.cpp
: 将你的光线生成过程粘贴到此处,并且按照新框架更新相应调用的格式。
// The main render function. This where we iterate over all pixels in the image,
// generate primary rays and cast these rays into the scene. The content of the
// framebuffer is saved to a file.
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 - 1)) * scale;
// 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!
// Ray 方向
Vector3f dir = Vector3f(x, y, -1);
dir = normalize(dir);
// Ray
Ray ray(eye_pos, dir);
framebuffer[m++] = scene.castRay(ray, 0);
}
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);
}
Triangle::getIntersection() in Triangle.hpp
: 将你的光线-三角形相交函数粘贴到此处,并且按照新框架更新相应相交信息的格式。
inline Intersection Triangle::getIntersection(Ray ray)
{
Intersection inter;
//如果结果大于0,两向量夹角小于90度
//这里normal是从三角形指向外的,若同方向则不可能穿过三角形
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); //S1*E1
if (fabs(det) < EPSILON) //这里分母太小会导致t特别大,相当于距离很远什么都看不到
return inter;
double det_inv = 1. / det; // 1/(S1*E1)
Vector3f tvec = ray.origin - v0; //S
u = dotProduct(tvec, pvec) * det_inv; // b1 = S1*S/S1*E1
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; //t = S2*E2/S1*E1
//find ray triangle intersection
if (t_tmp < 0) {
return inter;
}
inter.distance = t_tmp;
inter.happened = true;
inter.m = m;
inter.obj = this;
inter.normal = normal;
inter.coords = ray(t_tmp);
return inter;
}
在本次编程练习中,你需要实现以下函数:
IntersectP(const Ray& ray, const Vector3f& invDir, const std::array<int, 3>& dirIsNeg) in the Bounds3.hpp
: 这个函数的作用是判断包围盒 BoundingBox 与光线是否相交,你需要按照课程介绍的算法实现求交过程。
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
// test if ray bound intersects
float min_x = (pMin.x - ray.origin.x) * invDir[0];
float max_x = (pMax.x - ray.origin.x) * invDir[0];
float min_y = (pMin.y - ray.origin.y) * invDir[1];
float max_y = (pMax.y - ray.origin.y) * invDir[1];
float min_z = (pMin.z - ray.origin.z) * invDir[2];
float max_z = (pMax.z - ray.origin.z) * invDir[2];
if(dirIsNeg[0]) {
std::swap(min_x, max_x);
}
if (dirIsNeg[1]) {
std::swap(min_y, max_y);
}
if (dirIsNeg[2]) {
std::swap(min_z, max_z);
}
float enter = std::max(min_x, std::max(min_y, min_z));
float exit = std::max(max_x, std::max(max_y, max_z));
if (enter < exit && exit >= 0) {
return true;
} else {
return false;
}
}
getIntersection(BVHBuildNode* node, const Ray ray)in BVH.cpp
: 建立 BVH 之后,我们可以用它加速求交过程。该过程递归进行,你将在其中调用你实现的 Bounds3::IntersectP
。
Intersection BVHAccel::getIntersection(BVHBuildNode *node, const Ray &ray) const {
//Traverse the BVH to find intersection
Vector3f invDir(1.0 / ray.direction.x, 1.0 / ray.direction.y, 1.0 / ray.direction.z);
std::array<int, 3> dirIsNeg;
dirIsNeg[0] = ray.direction.x > 0 ? 0 : 1;
dirIsNeg[1] = ray.direction.y > 0 ? 0 : 1;
dirIsNeg[2] = ray.direction.z > 0 ? 0 : 1;
//如果光线没有与碰撞盒相交,直接返回一个空值
if (!node->bounds.IntersectP(ray, invDir, dirIsNeg)) {
return {};
}
//如果碰撞盒不再继续细分,测试碰撞盒内的所有物体是否与光纤相交,返回最早相交的
if (node->left == nullptr && node->right == nullptr) {
return node->object->getIntersection(ray);
}
//测试细分的碰撞盒
Intersection leaf1 = BVHAccel::getIntersection(node->left, ray);
Intersection leaf2 = BVHAccel::getIntersection(node->right, ray);
return leaf1.distance < leaf2.distance ? leaf1 : leaf2;
}
- 点赞
- 收藏
- 关注作者
评论(0)