Search code examples
raytracing

BVH trees - ways to determine child nodes intersection order?


just to mention that my math skills are not great and i need to ask for a little help here !

I am now trying to implement a stack less BVH traversal function using this paper: https://graphics.cg.uni-saarland.de/fileadmin/cguds/papers/2011/hapala_sccg2011/hapala_sccg2011.pdf

I am having it working except i am likely not having the both child traversal order correct which results in wrong image.

In point ( 3 Algorithm outline ) they mention a couple of methods to determine the right order:

For the traversal order there are various different alternatives. One often-used option is to store, for each node, the coordinate axis along which the builder split the parent node, and to use the ray’s direction sign in this dimension to determine the two nodes’ traversal order.

Let's say i am having the split axis index as 0, 1, 2 for X, Y, Z and the ray direction... It can determine the split axis by computing the maximum separation axis of both nodes centroids on the fly which i am also having...

So, the question is what would be the way (math) to determine the nodes traversal order by using the ray direction and the split axis ?


Solution

  • use the ray’s direction sign in this dimension to determine the two nodes’ traversal order.

    Suppose your ray has direction (0.707, 0, -0.707).

    For that ray:

    If you determine the parent's split axis to be X (i.e. two children share a boundary on the YZ plane), then your ray's direction sign in X is positive (0.707), and you should traverse the child whose centroid has the higher X centroid coordinate first.

    If you determine the parent's split axis to by Y (i.e. two children share a boundary on the XZ plane), then your ray's direction in Y is unsigned (0) and you'll have to decide what to do. The details of what you do in this case are up to you (pick a direction, move on to the next coordinate, etc) but acting consistently is the key.

    If you determine the parent's split axis to be Z (i.e. to children share a boundary on the XY plane), then your ray's direction in Z is negative (-.707), and you should traverse the child whose centroid has the lower Z coordinate first.