Skip to content

Instantly share code, notes, and snippets.

@nervoussystem
Created June 29, 2010 19:39
Show Gist options
  • Save nervoussystem/457700 to your computer and use it in GitHub Desktop.
Save nervoussystem/457700 to your computer and use it in GitHub Desktop.
//simple marching cubes with some abstraction
float resolution = .1;
int cubeGrid[][][] = new int[int(worldX/resolution)][int(worldY/resolution)][int(worldZ/resolution)];
PVector pt = new PVector();
//for each grid point compute inside or outside
for(int i=0;i<cubeGrid.length;++i) {
for(int j=0;j<cubeGrid[0].length;++j) {
for(int k=0;k<cubeGrid[0][0].length;++k) {
pt.set(i*resolution,j*resolution,k*resolution); //current position
float val = colorFunction(pt); //evaluation color function at pt
if(val>threshold) cubeGrid[i][j][k] = 1; //inside
else cubeGrid[i][j][k] = 0; //outside
}
}
}
//get surface for each cell
ArrayList surfaces = new ArrayList();
int[] currCube = new int[8];
for(int i=0;i<cubeGrid.length-1;++i) {
for(int j=0;j<cubeGrid[0].length-1;++j) {
for(int k=0;k<cubeGrid[0][0].length-1;++k) {
pt.set(i*resolution,j*resolution,k*resolution);
//get current cube values
currCube[0] = cubeGrid[i][j][k];currCube[1] = cubeGrid[i][j][k+1];currCube[2] = cubeGrid[i][j+1][k];
currCube[3] = cubeGrid[i][j+1][k+1];currCube[4] = cubeGrid[i+1][j][k];currCube[5] = cubeGrid[i+1][j][k+1];
currCube[6] = cubeGrid[i+1][j+1][k];currCube[7] = cubeGrid[i+1][j+1][k+1];
//retrieve surface for current cube from a lookup table
Surface s = getCubeSurface(currCube);
if(s != Surface.NULL) { //check for empty surface
surfaces.add(s);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment