Personal project

Marching Cubes

Reconstructing a fluid-like surface from particle positions, with a custom marching-cubes lookup-table generator, shared mesh vertices and environment reflections.

  • Rendering
  • Geometry
  • C++
  • OpenGL
  • Marching Cubes
View source

From particles to a surface

I had already worked on a 3D fluid simulation. This project focuses on the rendering side: turning particle positions into a continuous surface instead of drawing the particles individually. I implemented a Marching Cubes pipeline in C++ to reconstruct a triangle mesh from particle data and render it with OpenGL.

The application reads successive frames of particle positions, rebuilds the surface on the CPU and renders each frame. It handles surface reconstruction and rendering, not the particle simulation itself.

Surface reconstruction and rendering from an input particle sequence
Surface reconstruction and rendering from an input particle sequence

Building an isosurface

The first step is to convert a set of particles into a scalar field. For each particle, I add a contribution to nearby samples of a 128 × 128 × 128 grid, with the contribution decreasing quadratically with distance and becoming zero outside a fixed radius. After accumulating the contributions and applying a square root, the renderer extracts an isosurface at a fixed threshold.

Marching Cubes processes the grid one cell at a time. Each cube has eight corners, which are classified as inside or outside the surface. Their eight binary states give 256 possible configurations. A lookup table identifies which cube edges the surface crosses and how to connect those crossings into triangles. I interpolate the crossing position along each edge using the field values, instead of always placing it at the midpoint.

Generating the lookup table

Rather than copying a complete 256-entry table, I wanted to generate one myself. I defined a small set of representative configurations and wrote a Python script to derive the remaining cases using cube rotations, reflections and inside/outside inversion.

Diagram labeling the corners and edges of a cube for marching-cubes configurations.
Diagram illustrating a cube rotation used to transform one case into another.

Each transformation permutes the cube corners and edges. Once a transformed configuration matches a representative case, the script maps its triangle edges back to the original orientation. Reflections and inside/outside inversions also require adjusting triangle winding so the surface orientation stays consistent with the chosen convention.

At runtime, the C++ renderer uses a precomputed 256-case table, so each cube only requires a direct lookup. The Python generator is available as Python source code.

Mesh construction and smooth normals

Extracting triangles is only part of the job: the resulting mesh also needs useful vertex connectivity for shading. If every triangle creates three independent vertices, neighboring faces cannot directly share a smoothed vertex normal.

I use a map keyed by the two grid endpoints of an edge. Whenever another cube produces a surface vertex on that same edge, the mesh reuses the existing vertex index. After removing triangles with repeated indices, the program computes vertex normals by accumulating adjacent face normals weighted by their areas and normalizing the result.

These images from earlier experiments with a procedural field illustrate the progression from midpoint vertices to interpolated positions and smoother shading:

An early marching-cubes surface with blocky triangle placement.
A reconstructed surface with vertex positions interpolated along grid edges.
A reconstructed surface after applying smoothed vertex normals.

From left to right: initial surface, edge interpolation, and smooth vertex normals.

Rendering the fluid-like surface

The final mesh is uploaded to OpenGL and drawn using indexed triangles. For its appearance, I added a skybox and a shader that samples a static environment cubemap in the reflected view direction, combined with diffuse lighting and a specular highlight. This gives the surface a reflective, water-like look, although it is not a physically accurate water shader: there is no refraction or dynamic reflection of nearby objects.

The application processes a numbered sequence of input frames and can save the rendered results as PNG images, which makes it possible to assemble animations from the output.

Another particle-derived surface rendered with environment reflections
Another particle-derived surface rendered with environment reflections