Media Artist & Graphics Programmer

đź“· Instagram

đź’» Github

đź“ť LinkedIn

đź§Š A graphical program for exploring marching cubes + compute shaders in real-time.

Marching cubes is one of the most widely used algorithms for constructing a polygonal (triangle) mesh from a scalar field. A scalar field can be thought of as a function that, for a given point in 3-space, returns a floating-point (scalar) value. Examples of scalar fields include MRI scan data, signed-distance fields, and other volumes (point clouds, etc.).

The task of marching cubes is to discretize space into a 3D grid and extract an isosurface, which is a surface containing points whose values within the scalar field are a constant (i.e. the â€śisolevelâ€ť - a user-specified float). In particular, each â€śvoxelâ€ť (i.e. cell within the grid) can be in one of 256 configurations, depending on which of its 8 vertices are â€śinsideâ€ť or â€śoutsideâ€ť the isosurface. Paul Bourkeâ€™s method (see the links below) uses a pre-computed edge table and bitwise operators to quickly compute which configuration a voxel is in. From this, we can calculate the necessary vertex information.

The algorithm readily lends itself to parallelization, as the results from any one cell are not dependent on its neighbors. Therefore, we can use GPGPU constructs (in this case, OpenGL compute shaders) to perform the isosurface extraction in real-time.

The volume data is held within a 3D texture that is â€śanimatedâ€ť every frame by the first compute shader pass. By default, a (sculpted) noise field is drawn. The next compute shader pass runs the marching cubes algorithm. Specifically, it examines each cell (voxel) of the 3D volume texture and determines which (and how many) triangles to draw. The resulting triangle data (positions and normals) is written into two write-only buffers, which are subsequently used to draw a triangular mesh with a vertex + fragment shader. For now, the normals are calculated by taking the gradient of the signed-distance function (see Inigo Quilezâ€™s website below for details).

Upon launching the application, you should see two separate UI panels labeled:

**Marching Cubes on the GPU**: contains controls for adjusting the isolevel, mode selection, etc.**Debug**: displays different â€śslicesâ€ť (layers) of the 3D volume texture

You can choose between five basic modes:

- Fractal noise (default)
- Sphere
- Box
- Torus
- Metaballs

Each mode corresponds to a change in the underlying scalar field that will be used to generate triangles.

You can use your mouse to rotate the model in space. You can zoom in or out with your scroll wheel. Finally, you can â€śhomeâ€ť (i.e. reset) the current view by pressing `h`

on your keyboard.

This project was largely inspired by and based on previous work done by Paul Bourke. Many of the functions used for the signed-distance field evaluation are taken from Inigo Quilezâ€™s amazing website. The 3D noise function is taken from Patricio Gonzalez Vivoâ€™s GitHub.