Trouble implementing marching cubes

Posted By: Matt_Aufderheide

Trouble implementing marching cubes - 11/28/14 02:44

i'm trying to implement marching cubes using Paul Bourke's code example:http://paulbourke.net/geometry/polygonise/

I can get everything to compile and actually create a mesh using the TUST dynamic model functions...seems to sort of work except all the faces are connected wrong. Anyone have an idea?

I set up a voxel field like so:
for(x=0;x<voxel_num;x++)
{
for(y=0;y<voxel_num;y++)
{
for(z=0;z<voxel_num;z++)
{
if (y<4)
{
voxel[x][y][z]=1;
}

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


Here is my function to polygonize the voxel field:
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];

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

for (i=0;i<p;i++)
{
for (q=0;q<3;q++)
{
DYNAMIC_FACE FACE;

FACE.v.x=TRI[i].p[q].x;
FACE.v.y=TRI[i].p[q].y;
FACE.v.z=TRI[i].p[q].z;

dmdl_add_face(myModel, FACE);
Posted By: Wjbender

Re: Trouble implementing marching cubes - 11/28/14 07:28

a triangle has 3 vertices , a face has 3 vertices

v [0] , v [1] , v [2]

just saying...
Posted By: Matt_Aufderheide

Re: Trouble implementing marching cubes - 11/28/14 11:02

oops! yes that's a good one... works better now, but still not quite right yet..this is what I have now for the add face routine...see anything ese?

p=Polygonise(CELL,0.5,TRI);

for (i=0;i<p;i++)
{
DYNAMIC_FACE FACE;

for (q=0;q<3;q++)
{
FACE.v[q].x=TRI[i].p[q].x;
FACE.v[q].y=TRI[i].p[q].y;
FACE.v[q].z=TRI[i].p[q].z;
}

dmdl_add_face(myModel, FACE);
}
Posted By: Matt_Aufderheide

Re: Trouble implementing marching cubes - 11/28/14 11:31

ok I figured it out...my setup should be 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];
© 2024 lite-C Forums