Sitemap

Marching Cubes Algorithm

4 min readAug 20, 2025
Press enter or click to view image in full size

The Marching Cubes algorithm is a cornerstone technique in volumetric visualization. It is designed to extract a high-resolution polygonal mesh from a 3D scalar field. It transforms raw voxel data into a tangible surface by “marching” through the grid of scalar values and identifying where the field crosses a specified isolevel. This is a great option for rendering organic shapes like anatomical structures, fluid boundaries, or procedurally generated terrain.

This algorithm uses two precomputed lookup tables. The edge table maps each of the 256 possible cube configurations to a bitmask indicating which of the cube’s 12 edges are intersected by the isosurface. The triangle table then uses that configuration to specify how the interpolated edge vertices should be connected into triangles. These tables eliminate the need for complex conditional logic and enable fast, consistent surface extraction.

In this article, I will walk through my implementation of this algorithm.

To start, create a function that will populate the scalar field. This function systematically traverses a 3D grid, normalizing each voxel’s coordinates to ensure consistent spatial sampling. At each point, it samples Perlin noise to generate smooth, organic scalar values. These raw noise values, originally in the range [-1, 1], are remapped to [0, 1] to align with the expected input range for isosurface detection.

Press enter or click to view image in full size

Next, we create a header file for the marching cubes functionality. This will include are own Vec3 struct and Triangle struct. This header will also contain a function to generate the mesh and another to calculate the normal of the triangle.

We then create the C++ file and implement the struct functionality for Vec3.

Press enter or click to view image in full size

Next, we implement a function to precisely determine the point along a voxel cube’s edge where the isosurface intersects. The function takes two endpoints of a cube edge and its scalar value and computes the interpolated vertex where the scalar field crosses the specified isolevel. By applying linear interpolation, we calculate the exact position between the two points that best approximates the surface intersection.

We also implement a function to calculate the surface normal of a triangle by computing the cross product of two of its edge vectors. This yields a vector perpendicular to the triangle’s surface, which we then normalize to unit length.

Now it is time to create the function that implements the marching cubes algorithm. We begin by iterating through the scalar field, examining each voxel in the grid. For every voxel, we define the eight corners of the corresponding cube, capturing both their spatial positions and scalar values. Next, we determine the cube’s configuration by evaluating which corners fall below the specified isolevel, encoding this information into an 8-bit index. This index serves as a key to the edge table, which reveals which edges of the cube are intersected by the isosurface. For each intersected edge, we compute the exact point of intersection using linear interpolation between the corner values. Finally, we use the triangle table to assemble the interpolated vertices into one or more triangles, which collectively approximate the surface geometry within the scalar field.

That is the heavy lifting done. Now we go into the main file and initialize the mesh and get the vertices. After that, you set up your VAO and VBO and render the mesh.

Press enter or click to view image in full size

Here are some links if you would like to learn more about this algorithm.

Text

Original Paper On Algorithm

Polygonising A Scalar Field

Generating Complex Procedural Terrains Using the GPU

Video

Marching Cubes — video about marching cubes

2D Marching Squares — a very similar algorithm for 2D

--

--

No responses yet