Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trouble with marching cubes

Started by Matt Aufderheide Nov 27, 2014 at 11:30 PM 2 replies 1.8k views
Original Post
Matt Aufderheide
Matt Aufderheide

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 {
for(y=0;y {
for(z=0;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 {
for(y=0;y {
for(z=0;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.

Matt Aufderheide
Matt Aufderheide

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);
Matt Aufderheide
Matt Aufderheide

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];

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.