# Trouble with marching cubes

This topic is 1417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Trying to implement simple marching cubes using Paul Bourke's code example (http://paulbourke.net/geometry/polygonise/)

I first set up a test voxel field like so:

void setup_voxels()
{
int x=0;
int y=0;
int z=0;

for(x=0;x<voxel_num;x++)
{
for(y=0;y<voxel_num;y++)
{
for(z=0;z<voxel_num;z++)
{
if (z<4)
{
voxel[x][y][z]=1;
}

else
{
voxel[x][y][z]=0;
}
}
}
}

This would be a 16*16*16 cube, with only the the first four levels  actually set as "on" .

I then loop through each cell and try to set up the grid cells and call the polygonise function

for(x=0;x<voxel_num;x++)
{
for(y=0;y<voxel_num;y++)
{
for(z=0;z<voxel_num;z++)
{
GRIDCELL CELL;
TRIANGLE TRI[5];
XYZ tempvec;

tempvec.x=x*16;tempvec.y=y*16;tempvec.z=z*16;
vec_set(CELL.p[0],tempvec);

tempvec.x=x*16+16;tempvec.y=y*16;tempvec.z=z*16;
vec_set(CELL.p[1],tempvec);

tempvec.x=x*16+16;tempvec.y=y*16+16;tempvec.z=z*16;
vec_set(CELL.p[2],tempvec);

tempvec.x=x*16;tempvec.y=y*16+16;tempvec.z=z*16;
vec_set(CELL.p[3],tempvec);

tempvec.x=x*16;tempvec.y=y*16;tempvec.z=z*16+16;
vec_set(CELL.p[4],tempvec);

tempvec.x=x*16+16;tempvec.y=y*16;tempvec.z=z*16+16;
vec_set(CELL.p[5],tempvec);

tempvec.x=x*16+16;tempvec.y=y*16+16;tempvec.z=z*16+16;
vec_set(CELL.p[6],tempvec);

tempvec.x=x*16;tempvec.y=y*16+16;tempvec.z=z*16+16;
vec_set(CELL.p[7],tempvec);

CELL.val[0]=voxel[x][y][z];
CELL.val[1]=voxel[x+1][y][z];
CELL.val[2]=voxel[x+1][y+1][z];
CELL.val[3]=voxel[x][y+1][z];

CELL.val[4]=voxel[x][y][z+1];
CELL.val[5]=voxel[x+1][y][z+1];
CELL.val[6]=voxel[x+1][y+1][z+1];
CELL.val[7]=voxel[x][y+1][z+1];

int i=0;
int p=0;

p= Polygonise(CELL,0.5,TRI);

This is written in something called "lite-c" so it might seem a bit weird. Compiles fine and does return triangles, but they all seem messed up as far as positions and so on. I'm assuming I'm not getting something about the  CELL.val and what the values are actually supposed to be , I just made it one or zero depending on if a voxel cell is filled or not.

I hope this makes at least some sense...maybe I am totally misunderstanding how this method works..?? If anyone has any idea what's going on I'd be happy to get some help.

##### Share on other sites

Changed some things , works better now but still not right...this is what I have now :

GRIDCELL CELL;
TRIANGLE TRI[5];

vec_set(CELL.p[0],vector(x,y,z));
vec_set(CELL.p[1],vector(x+1,y,z));
vec_set(CELL.p[2],vector(x,y+1,z));
vec_set(CELL.p[3],vector(x+1,y+1,z));

vec_set(CELL.p[4],vector(x,y,z+1));
vec_set(CELL.p[5],vector(x+1,y,z+1));
vec_set(CELL.p[6],vector(x,y+1,z+1));
vec_set(CELL.p[7],vector(x+1,y+1,z+1));

CELL.val[0]=voxel[x][y][z];
CELL.val[1]=voxel[x+1][y][z];
CELL.val[2]=voxel[x][y+1][z];
CELL.val[3]=voxel[x+1][y+1][z];

CELL.val[4]=voxel[x][y][z+1];
CELL.val[5]=voxel[x+1][y][z+1];
CELL.val[6]=voxel[x][y+1][z+1];
CELL.val[7]=voxel[x+1][y+1][z+1];

int i=0;
int p=0;
int q=0;

p=Polygonise(CELL,0.5,TRI);


##### Share on other sites

ok I got it working :) should have been like this:

vec_set(CELL.p[0],vector(x,y,z));
vec_set(CELL.p[1],vector(x+1,y,z));
vec_set(CELL.p[2],vector(x+1,y,z+1));
vec_set(CELL.p[3],vector(x,y,z+1));

vec_set(CELL.p[4],vector(x,y+1,z));
vec_set(CELL.p[5],vector(x+1,y+1,z));
vec_set(CELL.p[6],vector(x+1,y+1,z+1));
vec_set(CELL.p[7],vector(x,y+1,z+1));

CELL.val[0]=voxel[x][y][z];
CELL.val[1]=voxel[x+1][y][z];
CELL.val[2]=voxel[x+1][y][z+1];
CELL.val[3]=voxel[x][y][z+1];

CELL.val[4]=voxel[x][y+1][z];
CELL.val[5]=voxel[x+1][y+1][z];
CELL.val[6]=voxel[x+1][y+1][z+1];
CELL.val[7]=voxel[x][y+1][z+1];


##### Share on other sites

Isn't Gamedev wonderful.  The forums work even if you only answer yourself.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009199
• ### Who's Online (See full list)

There are no registered users currently online

×