Source Code: CPU Voxel Ray Tracer

This source package is based on a somewhat old experiment on CPU ray tracing. The 3D world consists of voxels: a grand total of 8192x512x8192 of them, each with a 24-bit color, for a grand total of 128GB of raw data. The data is subdivided in chunks of 128^3 voxels, which are then compressed to a compact octree. The result is a much more manageable ~2GB world.

The data for the world is generated procedurally at runtime, and stored to disk once generated. The world generator is based on Dave Hoskin’s work on Shadertoy: https://t.co/G2b8w2akXE .

The first interesting bit in this code is the octree construction in file chunk.cpp. A 128^3 buffer (8MB of data) is first reordered into Morton order, which is essentially already an octree. We then mark, at each level of the octree, identical cells. The unused ‘alpha channel’ of the voxels is used for this: bit i in this channel is set for each voxel that is identical to the 7 other voxels at level i in the octree that have the same parent. The final octree is then obtained by using run-length encoding.

The other interesting bit may be the actual ray tracing code in packet.cpp, which is multithreaded and vectorized and uses ray packets to rapidly intersect the chunks and the compressed octree data directly.

Source code can be downloaded here (Windows / Visual Studio 2017).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.