snogglethorpe / snogray

Snogray renderer

Home Page:http://www.nongnu.org/snogray

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimize Octree search order

snogglethorpe opened this issue · comments

Currently Octree searches subnodes in a fixed order.

It would probably be better to search starting with nodes nearer to the search-ray's origin, because it would be more likely to find the "right" answer sooner for "closest surface" searches (where we're looking for the closest intersection rather than any intersection): in "closest surface" mode, we can only prune the search by making the ray shorter from the end.

Even in the "any surface" search mode, searching closer nodes first might be an advantage, as often search are done from an existing surface "into space," and surfaces tend to be clustered. So it's probably more likely that we'll find some intersecting surface closer than we'll find one farther... [So if we do this optimization, it would make sense to look at the places where we call the intersection routines, and make sure that in fact all of our search rays are in fact "surface out." For instance, in the case of shadow-testing, both surface->light or light->surface directions work, and until now there's been no reason to prefer one or the other.]

Now that we're using an array to store subnode references, with geometrically determined indices (x-axis bit 2, y-axis bit 1, z-axis bit 0), it should be fairly simple to do this.

One idea: simply make a bitmask corresponding to the direction of the ray we're searching along, one bit per axis, with the value determined by the sign of the corresponding component of the direction vector. Then we can search in order 0-7, but xor the search index with the bitmask above before indexing.
That will result in subnodes nearer the ray's origin being tried first. E.g., in the case where the direction vector has all negative components, the bitmask would be 0x7, meaning we'd essentially invert the bits of the 0-7 search index searching subnode 7 (x-hi, y-hi, z-hi) first, then subnode 6 (x-hi, y-hi, z-lo), etc.

[Hmm, that search order is good, but not perfect... a perfect order would be 0 - 1 - 2 - 4 - 3 - 5 - 6 - 7... >< ]

Closed by b523c4a