hello 👁
..

Understanding Bvh

I have been reading about BVHs and I think I have a good understanding of them now. I will be implementing them in my raytracer soon as well as updating this post as I go along the implementation and generate more images.

Basically, in our raytracer each object has a hit function to check if one of the millions of rays we send touches any object. We dont wanna call it everytime so we’ll use bounding volumes to optimize this. Its like creating a box around object that perfectly fits them, this box itself could be in a box that fits all the boxes of the objects in the scene. This way if a ray doesnt hit the big box no need to check if it hits smaller boxes and object and so on…. BOOM! we just saved a lot of time.

I’ll use the same example as the book :

BVHGRAPH Click to see the full image

So the “code” will look something like this:

if (hit big box)
{
    for each object in big box, or object in sub-boxes
    if (hit smaller box)
    {
        if (hit object)
        // Object is hit
    }
}

Update! I successfully implemented BVH in my raytracer. Here is the speed comparison:

BVHCOMPDINGBOARDDDD

The left image is the one without BVH and the right one is with BVH. As you can see, the one with BVH is CLEARLY faster. The difference is even more pronounced when the scene has more objects or increased Antialiasing.

now, take a look at this graph, the blue ones are the boxes and the green ones are the spheres:

EXEMPLE Click to see the full image

I’m basically creating this tree by randomly selecting an axis (x, y, or z) to split the objects and chooses a comparator function based on this axis. Then calculate the number of objects in the current segment. If there is only one object, both child nodes are NULL. If there are two objects, each child node points to one of the objects. For more than two objects, the constructor sorts the objects along the chosen axis and splits them into two groups at the midpoint. It then recursively creates child nodes for these groups. Finally, the bounding box for the current node is calculated as the union of the bounding boxes of the child nodes. This hierarchical structure allows for efficient ray-object intersection tests, significantly speeding up the rendering process.

I can still optimize it by dividing the boxes by their longest axis, but I will leave that for another day. I am very happy with the results so far and i’m looking forward to implement more features in my raytracer.

HEEEEHOOO