Koiby25
July 17th, 2007, 04:36 AM
I'll just throw this out here first:
struct VERT3D
{
// Position
D3DXVECTOR3 pos;
// Normal
D3DXVECTOR3 normal;
// Texture coordinates
D3DXVECTOR2 tex;
// Default color
D3DCOLOR col;
};
struct POLY3D
{
// Vertex indicies
WORD verts[3];
};
...
VERT3D vertList[OBJ_MAX_VERTS];
POLY3D polyList[OBJ_MAX_POLYS];
VERT3D vertListTemp[OBJ_MAX_VERTS];
POLY3D polyListTemp[OBJ_MAX_POLYS];
Vertices/indices are loaded from a file into vertListTemp and polyListTemp respectively. The file contains a ton of duplicated vertices, so I want to remove them and store the results in vertList and polyList, which I'll load into vertex and index buffers. This is the code I wrote to try to do that, but the program crashes for some reason I can't figure out:
bool match = false;
int matchInd,
currInd;
numVerts = 0;
for (int x = 0; x < numPolys; x++) // loop through every poly
for (int y = 0; y < 3; y++) // loop through each vert index in current poly
{
currInd = polyListTemp[x].verts[y];
// Loop through new vert list to check for duplicates
for (int z = numVerts-1; z >= 0; z--)
if ( (ABSV(vertListTemp[currInd].pos.x - vertList[z].pos.x) < EPS1000) &&
(ABSV(vertListTemp[currInd].pos.y - vertList[z].pos.y) < EPS1000) &&
(ABSV(vertListTemp[currInd].pos.z - vertList[z].pos.z) < EPS1000) )
{
// Found match, so it's a duplicate
match = true;
matchInd = z;
break;
}
// Check match test var
if (match)
{
// Duplicate, so set current poly index to matchInd
polyList[x].verts[y] = matchInd;
}
else
{
// No other match, so add to list
polyList[x].verts[y] = numVerts;
vertList[numVerts] = vertListTemp[currInd];
numVerts++;
}
// Reset test var
match = false;
}
When I replace that last code listing with this...
for (int x = 0; x < numVerts; x++)
vertList[x] = vertListTemp[x];
for (int x = 0; x < numPolys; x++)
polyList[x] = polyListTemp[x];
...it compiles and runs just fine, but obviously the duplicated vertices are still there. I can't find anything wrong with the code to remove them, so I was hoping somebody here could :)
By the way, ABSV returns the absolute value of its argument, and EPS1000 = .001.
struct VERT3D
{
// Position
D3DXVECTOR3 pos;
// Normal
D3DXVECTOR3 normal;
// Texture coordinates
D3DXVECTOR2 tex;
// Default color
D3DCOLOR col;
};
struct POLY3D
{
// Vertex indicies
WORD verts[3];
};
...
VERT3D vertList[OBJ_MAX_VERTS];
POLY3D polyList[OBJ_MAX_POLYS];
VERT3D vertListTemp[OBJ_MAX_VERTS];
POLY3D polyListTemp[OBJ_MAX_POLYS];
Vertices/indices are loaded from a file into vertListTemp and polyListTemp respectively. The file contains a ton of duplicated vertices, so I want to remove them and store the results in vertList and polyList, which I'll load into vertex and index buffers. This is the code I wrote to try to do that, but the program crashes for some reason I can't figure out:
bool match = false;
int matchInd,
currInd;
numVerts = 0;
for (int x = 0; x < numPolys; x++) // loop through every poly
for (int y = 0; y < 3; y++) // loop through each vert index in current poly
{
currInd = polyListTemp[x].verts[y];
// Loop through new vert list to check for duplicates
for (int z = numVerts-1; z >= 0; z--)
if ( (ABSV(vertListTemp[currInd].pos.x - vertList[z].pos.x) < EPS1000) &&
(ABSV(vertListTemp[currInd].pos.y - vertList[z].pos.y) < EPS1000) &&
(ABSV(vertListTemp[currInd].pos.z - vertList[z].pos.z) < EPS1000) )
{
// Found match, so it's a duplicate
match = true;
matchInd = z;
break;
}
// Check match test var
if (match)
{
// Duplicate, so set current poly index to matchInd
polyList[x].verts[y] = matchInd;
}
else
{
// No other match, so add to list
polyList[x].verts[y] = numVerts;
vertList[numVerts] = vertListTemp[currInd];
numVerts++;
}
// Reset test var
match = false;
}
When I replace that last code listing with this...
for (int x = 0; x < numVerts; x++)
vertList[x] = vertListTemp[x];
for (int x = 0; x < numPolys; x++)
polyList[x] = polyListTemp[x];
...it compiles and runs just fine, but obviously the duplicated vertices are still there. I can't find anything wrong with the code to remove them, so I was hoping somebody here could :)
By the way, ABSV returns the absolute value of its argument, and EPS1000 = .001.