[Book 2 Chapter 3] BVH "depth testing" #1538
Closed
Williscool13
started this conversation in
Debugging
Replies: 1 comment 1 reply
-
The
Specifically, the clause |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm going through the chapter on creating a BVH to more efficiently sort the scene objects. However, I am lost on how the BVH implementation as of this chapter sorts the object's based on their depth. Maybe I missed some code that I was supposed to write, or perhaps this is addressed further into the book.
As I understand it, the scene is recursively split into BVH nodes on random axes, with the sphere objects as leaf nodes. This allows bounding boxes that aren't in the path of the ray to be discarded early. In the pre-BVH implementation, depth is sorted by comparing the hit data with hit data from previous intersection tests, ensuring that you always sample from the closest surface.
for (const auto& object : objects) {
if (object->hit(r, interval(ray_t.min, closest_so_far), temp_rec)) {
hit_anything = true;
closest_so_far = temp_rec.t;
rec = temp_rec;
}
}
But in post-BVH, we don't do that anymore:
world = hittable_list(make_shared<bvh_node>(world));
This code causes the world to be a single
hittable
. This means that the above for loop only iterates once, and as a result, the previous implementation of depth testing is no longer used.I have verified that this does occasionally give the wrong surface to be sampled (depth-wise) by running the following code:
std::cout << "Hit leaf object dist along ray: (" << root << ") ";
, whenever the spheres write tohit_record& rec
.Which leads to the following log for a single specific ray:
Hit leaf object dist along ray: (25.1717) Hit leaf object dist along ray: (25.1717) Hit leaf object dist along ray: (1) Hit leaf object dist along ray: (8.22024) Hit leaf object dist along ray: (4.03197) Hit leaf object dist along ray: (7.17525) Hit leaf object dist along ray: (4.84666)
.In this particular case, using a debugger shows that the latest ray data (4.84666) is used rather than the more appropriate (1).
Though just from a rough eyeballing, this seems to be fairly rare, and in most cases the appropriate surface is selected for the samples.
I want to know if this is truly how it is meant to be implemented. How does the BVH structure guarantee that traversal samples the nearest surface? I would imagine you would store the distance in the
hit_record
and do a comparison at point of contact. Does the sheer number of traced rays turn this into standard noise? Or did I miss something important. I would be willing to show my code if necessary.Beta Was this translation helpful? Give feedback.
All reactions