Dynamic Voxel Based Terrain Generation on the GPU

My final project for 572 was implementing texturing of arbitrary volumetric data and a geometry shader implementation of the marching cubes algorithm. This was combined into a small game to demonstrate it's capabilities.

Marching Cubes is an algorithm for skinning volumetric data. It uses lookup tables for each voxel cube in order to quickly build the resulting mesh. However when attempting to perform this while editing the data in real time, the user get momentary freezes as the mesh is rebuilt with each edit. By moving this process to the GPU, we can remove the CPU time that was required, effectively eliminating most of the lag. In order to do this, the grid of data is packed into a 3D texture. The required lookup tables are then packed into 2D integer textures for use on the GPU. The draw call then draws one point per voxel cube, with a geometry shader building the section of the mesh for each voxel.

Texturing is achieved in the fragment shader by looking at the normal for each point, as well as it's position. Three texture lookups are performed using the xy, yz, and xz coordinates. This allows us to find which color is being projected from the z, x, and y directions respectively. Because we want a smooth wrap of the texture, we then need to interpolate between the colors. Out normal is normalized, meaning the sum of the squares of each component adds up to one. Using this, we then scale each value by it's corresponding squared normal component and add them together. This gives a nice wrap the accounts for any resulting geometry from the marching cubes algorithm.


By implementing these systems, we end up with a voxel system that is entirely editable in real time. This particular information uses two material values allowing for a surface that can be build on or dug through, and one that acts as a wall. This allows for a maze style game where the player digs their way to the goal.

Here is a video of the program in action. The framerate of the video is on the low side, but the program runs noticeably smoother in real time.


Information on marching cubes.

Performing marching cubes on the GPU