Voxel terrain rendering with marching cubes
- by JavaJosh94
I was working on making procedurally generated terrain using normal cubish voxels (like minecraft) But then I read about marching cubes and decided to convert to using those. I managed to create a working marching cubes class and cycle through the densities and everything in it seemed to be working so I went on to work on actual terrain generation. I'm using XNA (C#) and a ported libnoise library to generate noise for the terrain generator. But instead of rendering smooth terrain I get a 64x64 chunk (I specified 64 but can change it) of seemingly random marching cubes using different triangles.
This is the code I'm using to generate a "chunk":
public MarchingCube[, ,] getTerrainChunk(int size, float dMultiplyer, int stepsize)
{
MarchingCube[, ,] temp = new MarchingCube[size / stepsize, size / stepsize, size / stepsize];
for (int x = 0; x < size; x += stepsize)
{
for (int y = 0; y <size; y += stepsize)
{
for (int z = 0; z < size; z += stepsize)
{
float[] densities = {(float)terrain.GetValue(x, y, z)*dMultiplyer, (float)terrain.GetValue(x, y+stepsize, z)*dMultiplyer, (float)terrain.GetValue(x+stepsize, y+stepsize, z)*dMultiplyer, (float)terrain.GetValue(x+stepsize, y, z)*dMultiplyer,
(float)terrain.GetValue(x,y,z+stepsize)*dMultiplyer,(float)terrain.GetValue(x,y+stepsize,z+stepsize)*dMultiplyer,(float)terrain.GetValue(x+stepsize,y+stepsize,z+stepsize)*dMultiplyer,(float)terrain.GetValue(x+stepsize,y,z+stepsize)*dMultiplyer };
Vector3[] corners = { new Vector3(x,y,z), new Vector3(x,y+stepsize,z),new Vector3(x+stepsize,y+stepsize,z),new Vector3(x+stepsize,y,z), new Vector3(x,y,z+stepsize), new Vector3(x,y+stepsize,z+stepsize),
new Vector3(x+stepsize,y+stepsize,z+stepsize), new Vector3(x+stepsize,y,z+stepsize)};
if (x == 0 && y == 0 && z == 0)
{
temp[x / stepsize, y / stepsize, z / stepsize] = new MarchingCube(densities, corners, device);
}
temp[x / stepsize, y / stepsize, z / stepsize] = new MarchingCube(densities, corners);
}
}
}
(terrain is a Perlin Noise generated using libnoise)
I'm sure there's probably an easy solution to this but I've been drawing a blank for the past hour.
I'm just wondering if the problem is how I'm reading in the data from the noise or if I may be generating the noise wrong? Or maybe the noise is just not good for this kind of generation? If I'm reading it wrong does anyone know the right way?
the answers on google were somewhat ambiguous but I'm going to keep searching.
Thanks in advance!