Marching cubes is essentially a series of tables and algorithms that turn chunky Minecraft-esque voxel based models into smooth organic models. I recently re-re-re-re-restarted a voxel terrain generation engine in GameMaker, this time using the powerful YoYo Compiler addon to give an extra performance boost since such an engine is very mathematical and involves hundreds of thousands of calculations to be done as fast as possible. Ever since I started this ongoing project around two years ago I’ve wanted to get a fully detailed 3D terrain using voxels, but avoid the blocky shapes. After many failed attempts to do this I found Marching Cubes, which is a beautiful example of how powerful working with binary numbers can be (more on that later)
Here are shots of the current generation (raw noise for the most part) as it goes from voxels, to marching cubes, to weighted marching cubes:
[Pardon the frequent changes in contrast]
Now, as nice as the cubes look I think everyone can agree that seeing complex caves. mountains, and cliff walls with smooth surfaces allows for a much more visually pleasing game in the end. At least this is what I hope to achieve.
Now, on to Marching Cubes themselves. I won’t go too in depth, but basically this is how Marching Cubes works. Imagine a cube that has a flat surface cutting through it at some obscure angle. Now, remove the half of the cube that is in front of this surface. What’s left is the marching cube. Each cube consists of 8 vertices. Each of these vertices are either 1 or 0 to represent ground or air. This creates an 8-bit binary integer. Now, as mentioned at the top Marching Cubes relies on a set of predefined tables. These tables contain every possible combination (256) of 8 vertices. So, after the 8-bit binary is generated it is fed into the Edge Lookup table to find which of the 12 edges are being intersected by the surface (known as an isosurface). This generates a 12-bit integer which is fed into the Tri Lookup table. This generates the coordinates of each vertex in the series of triangles needed to create the actual marching cube. It’s all very clever in that no if’s and else’s are involved. It’s just pure math and tables. Here’s the source code I used to reference during my experiment if you would like to learn more: http://paulbourke.net/geometry/polygonise/