Marching Cubes as I understand

Emre SOLAK
Dream Harvesters Team
3 min readDec 24, 2023

--

Marching Cubes is an algorithm to polygonize a scalar field. Although i’m aware of the algorithm for a long time, i couldn’t find time to consume it.

Now that we kickstarted a game for PC(which i’ll share details in future), it’s the time to learn and practice it a bit.

The main idea behind the algorithm is slicing a 3d space into a grid of cubes and run them to create a mesh. Here’s the steps of the algorithm:

  1. Split a 3d space into cubes.
  2. Iterate all the cubes.
  3. Every cube will have 8 vertices in the corners. Every vertice suppose to have a scalar value calculated by a function.
  4. Define a global isoLevel. Scalar values of the vertices will be tested against isoLevel. Algorithm understands if the tested vertex is inside the mesh, outside the mesh or on the polygon of the mesh.
  5. Depending on the test result, we use some precomputed lookup tables to decide the shape of the polygon inside the cube.
  6. Create the mesh inside the cube. Algorithm creates it so it matches with the shape in the neighbour cubes.

I want to explain terms a little more detailed.

  1. Scalar Field: Scalar field is a mathematical concept to associate a scalar value for all the points in a space. This is generally a function getting the point in the space as input and assign it a scalar value. Scalar field must have a value for each vertices of the cubes(corners).
  2. Cube: Pretty obvious :D. The sections in a 3d space to be marched.
  3. Edge Table: Edge table is the precomputed table which shows the intersected edges of the cube with the polygons inside it. This is 255 lenght int array and can be found on the internet.
  4. Triangle Table: Marching cubes is used to create polygons in a scalar field. That’s why we’ll need triangles. Triangle Table is a precomputed array of the

After realizing the approach of the algorithm, i tried to implement it in the most pritive way of course and i couldn’t have the expected result in my first attempt. You all experienced this situation i suppose :)

In this kind of situation, in which you’re trying to learn something new and experiment, there might be several wrong parts in the implementation. This generally confuses me because i’m not sure which part is wrong or not. I try to compare my implementation with the ones out there(generally some open source repos). This wasn’t different at all. Here is a list you might be careful with your implementation:

  1. Be careful how you create the scalar values of vertices
  2. Remember that every vertex(corner of the cubes) must have a scalar value. Some might have the same but all must have one.
  3. I tried to use Perlin Noise for my terrain creation and didn’t take the height of the cubes into account. This caused all vertices in the same x-z coordinate have the same scalar value. You might get a result similar to this one:

This happens because using perlin noise with x-z plane doesn’t take height of the vertex into account.

4. Be careful with the order of the vertices to create the index buffer. The order depends on the api you use. The wrong order might create a polygon in the opposite side of the expected outcome.

Lastly here’re the sources i’ve used:

https://paulbourke.net/geometry/polygonise/
https://www.youtube.com/watch?v=M3iI2l0ltbE

I will share my further experiences with the algorithm as i have progress in the project :)

--

--

Emre SOLAK
Dream Harvesters Team

Co-Founder @Teknodev & @DreamHarvesters / Senior Developer / Expert on Game Development and Project Management