Marching Cubes

Well hello there.

So I managed to get a Marching Cubes implementation working over the past week. I got the implementation from here. It is from a gentleman named Paul Bourke, who graciously allowed me to use his code in my project. It seems that this fellows implementation is very popular, as you'll find various references to him and his implementation all over the web.

In order to get lighting to work, I needed a way to generate normals for each vertex. A simple implementation for getting the normals involves getting the vector perpendicular to the triangle face that 3 vertices describe, and then using it as the normal for each vertex. Unfortunately, this isn't a very attractive option, as it means all the pixels in that triangle have the same 'intensity' value for each light source that hits it, which makes the whole terrain look blocky. The screenshot below illustrates what I mean.

Marching Cubes blocky

To make the lighting more natural, we need to calculate the normal for each vertex. To do that, we calculate the gradient vector at each point in our point field. Then, along each edge (which has 2 gradient vectors), we linearly interpolate the gradient vectors to get the normal for the newly created vertex. I got this idea from here. You can see a screenshot below that demonstrates lighting on normals that have been calculated in this way.

Marching Cubes smooth

Unfortunately, if we reach the boundary of our point field while calculating normals, we are unable to generate normals for the edges at the boundary. In my current implementation, I just default back to the 'simple' implementation in this case, which gives the 'blocky' look. You will notice this in the video below, which has areas that are 'smooth', and areas that are 'blocky'. I'll have to address this at some point.

Here's a video of my terrain smoothed over with Marching Cubes:

That's all for now!