Part 2: Gotta Go Fast/Power of Linear Algebra
Part 2: Gotta Go Fast/Power of Linear Algebra
Hello everybody, In this post I will be explaining the details of my second homework of Advanced Ray Tracing.
This Homework consists of two parts. First part is about implementing an acceleration structure for ray tracing. The other is about linear transformations and object instancing.
Acceleration Structures
In the previous post I was just intersecting my rays with every object with my rays then choosing the closest one leaving me with O(N) complexity. Now I am going to implement one of the most popular acceleration structures for ray tracing which being bounding volume hierarchy. If you'd like to learn about other ones you can check out kd-tree's or grid acceleration structure yourself.
Bounding Boxes
The idea of a bounding box is really simple instead of intersecting our ray with a complex object like this camouflage bunny below. (Because intersecting a ray with this bunny means intersecting the ray with all of it's triangles.)
We will put it in a box and check if the box is intersecting with the ray or not. If the ray doesn't intersect with the box we can be sure that ray doesn't intersect with the bunny and move on. Of course if they intersect we still have to test it but this will still save us a lot of time.
Bounding Volume Hierarchy
Let us have these Perlin bunnies chilling in our scene.
We take all the bunnies and computer their common bounding box, then split objects into two groups. Then we compute their bounding box and repeat as you can see at some point we will get a bounding box with only one bunny. Finally we have a tree structure at our hands. Now we can send a ray to the root Bounding box if the ray hits it we check if the same ray hits the children of this bounding box. Be careful here because if a ray hits the left child of node that does not mean it will not hit the right. Depending on the scene it still might hit the right so we check a node's both children.
In this way have approximately reduced our complexity to approximately to O(logN) with a tree structure. Of course there are important things to notice here.I have purposefully selected the bunnies in the above scene If I had split the bunnies such that the yellowish bunny and the purplish/blueish black spotted bunny were in the same group, it would not reduce the size of our bounding box. So In order to solve that I sorted the objects according to their centroids along X axis and split the object list into two. In this way we usually en up reducing bounding boxes if we don't have a weirdly long object. Another good idea is to split the object along different axis' with each split. So if you have sorted them with respect to X axis first sorting them w.r.t Y, then Z, then X again and so on, next time is a better idea.
And voila now the dragon mesh below can be rendered in 8 seconds or so in my laptop with Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz processor. (In a better processor it takes even less. For instance in an Intel(R) Xeon(R) E-2224 CPU @ 3.40GHz cpu it takes like 3.5 seconds) Before implementing BVH I tried to render it and it didn't render in 52 minutes and I have interrupted it for the sake of my cpu.
Transformations and Instancing
In computer graphics we define our objects by their geometry in 3D space. Luckily we have a very powerful tool to help us when dealing with objects: Linear Algebra.
Obviously I don't know for sure but the aliens in the Toy Story
,or any object/character in cinema with copies of them for that matter,
are probably rendered with this method. If not that's a waste of good memory.
Delicate Points about transformations.
One cool result of this inverse transforming ray instead of transforming objects is without defining an ellipsoid equation in our scene we can get an ellipsoid with scaling transforms like below.
One other problem you may run into is an mesh being instanced on an already instanced mesh. This may cause instances to lose transformation information or something else If not handled carefully but that can be handled by writing clean code (which I admit I didn't do quite well at first).










Comments
Post a Comment