# Marching Cubes Terrain [closed]

For the past week I've been researching the Marching Cubes algorithm. I hope to use this for Terrain as it would allow the creation of destruction of terrain. After looking at source code and reading many articles I decided to try compiling some of it myself and playing around with it to attempt to make terrain. However, It's giving me a headache and I can't seem to find any good resources on how to do it.

http://i.stack.imgur.com/BbvJR.png

This is from the example found here - http://www.angelfire.com/linux/myp/MC/

The sourcecode relevant can be found on the website.

I added the source code to my project and converted all the code to be used with OpenGL 3.2. However, I don't want to be using this code in my final project. (This is just so I can understand it). I'm having trouble understanding the code. I feel I understand the algorithm now but I can't seem to understand how the object is actually formed.

How can I change this object to create terrain? Are there any better resources on the internet? Thanks in advance.

Marching cubes is an algorithm for polygonizing an implicit surface - i.e. one defined by an equation of the form f(x, y, z) = 0. You can evaluate any function f(x, y, z) you like, and the algorithm tries to make a polygonal model of the surface formed by the points where the function is zero.

I haven't looked at the source code, but from the screenshot, the test object looks like a metaball, which is a common application of marching cubes. You could perhaps sculpt your terrain out of metaballs.

Another approach, perhaps better suited for terrain, would be to use a noise function like Perlin noise. 3D noise by itself will just make 3D blobs, but to make it more terrain-like, you could use 2D noise and set f(x, y, z) = noise(x, y) - z.

You can combine noise-based terrain with metaballs, too, just by adding their functions together. Therefore you could use metaballs to add additional terrain, or negative-weight metaballs to hollow out caves or destroy terrain.

• Thanks, However, I'm having trouble understanding how to manipulate the balls to make terrain. The code responsible for making the balls basically creates a vector - (glm::vec3 dp1 = glm::vec3( 0.0, -2.0, 0.0)-p;) This vector is the location of the ball in respect to the grid. Then it returns a Field Value - return 1/sqrt(dp1.x*dp1.x + dp1.y * dp1.y + dp1.z * dp1.z). Like this. This is the value which is sent to the algorithm. How does this value relate to the f(x,y,z) = 0? – Vangoule Aug 19 '13 at 2:27
• (0, -2, 0) is the center of the metaball, and the field value (the f(x,y,z)) being returned is one over the distance from the center. By itself this makes a spherical surface, but when multiple metaballs are added together their fields combine and make the "melted", blobby shape characteristic of metaballs. – Nathan Reed Aug 19 '13 at 5:03
• As an addition to this answer here is a good article aboutmarching cubes terrain, that might help you understand it and manipulate it. GPU Gems 3-1 – akaltar Aug 19 '13 at 8:41
• GPU Gems uses something they call a 'Density' function. Is this relatable to my metaballs or is it something completely different? – Vangoule Aug 19 '13 at 15:47
• Yes, the density function is the f(x, y, z) we've been talking about. – Nathan Reed Aug 19 '13 at 16:53