Could anyone provide a short & sweet explanation (or suggest a good tutorial) on how to cast a ray against a voxel octree without recursion?
I have a complex model baked into an octree, and I need to find the best/closest leaf that intersects a ray. A standard drill-down iterative tree walk:
- Grab the root node
- Check for intersection
- No? Exit
- Yes? Find child that intersects the ray that is closest to the ray’s origin
- Loop until I reach a leaf or exit the tree
Always returns a leaf, but in instances where the tree stores, say, terrain, the closest node to the ray’s origin doesn’t necessarily contain the leaf that’s the best match. This isn’t suprising – taller objects in farther nodes won’t get tested using this approach.
I can do this recursively by finding all of the intersecting leaves in the tree, sorting by distance and picking the closest one to the ray’s position. However, this is slow and requires recursion.
I’ve read a little about using the Bresenham line algorithm to walk the tree, which seems to require that each node contain pointers to adjacent neighbors, but I’m unclear on how to implement this in a useful way.
Any suggestions? I can fake a stack in HLSL using a fixed-length array or a struct with an element for each potential stack entry, but the memory requirements for that can become crippling with a sufficiently large tree.
Help.
I’ve managed to get this mostly working using a 3D DDA algorithm and neighbor node pointers.
I’m still working out a few bugs, but here’s a C# version that appears to work. This one stops when it reaches the first leaf, but that’s not entirely necessary.
Feel free to point out any bugs. I’ll post the HLSL version if there’s any demand.
Here’s another version that just steps through the tree in leaf-sized steps without intersection checking. This is useful as a 3D DDA demonstration:
And an HLSL version that just stores the tree in a Texture3D, without neighbors or any “sparseness” to the tree.
This is still buggy. The first test with
hitbox()works correctly, but the ray winds up getting refracted within the tree. This looks very cool, but isn’t correct.Here’s what it looks like when I stop at the root bounds, without using the DDA to traverse the tree:
update
Found a very good volume rendering tutorial!
http://graphicsrunner.blogspot.com/search?updated-max=2009-08-27T02%3A45%3A00-04%3A00&max-results=10