Click to See Complete Forum and Search --> : Duplicated Vertices


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.

Mike Harnad
July 17th, 2007, 11:50 AM
Does stepping into the code in debug show anything? If not, try using the DirectX control panel applet and setting the debug level higher.

TheCPUWizard
July 17th, 2007, 11:57 AM
I might be missing something but I do not see where you are creating the actual objects that will end up in the target arrays.. :confused:

Any chance you can upgrade this to a "minimal yet complete" sample (e.g. a small program that we can actually compile and run....)

Koiby25
July 17th, 2007, 04:56 PM
Does stepping into the code in debug show anything? If not, try using the DirectX control panel applet and setting the debug level higher.

I didn't really get anything from debugging... everything appears to be the way it should.

I might be missing something but I do not see where you are creating the actual objects that will end up in the target arrays.. :confused:

Any chance you can upgrade this to a "minimal yet complete" sample (e.g. a small program that we can actually compile and run....)

I just left that part of the code out. I'm not quite sure if this is what you're confused about... but I load the vertex data from .cob files and store them in the temporary arrays.

Also, I could try to make a minimal sample program... but it might be very time consuming.

Mike Harnad
July 17th, 2007, 05:00 PM
Did you use the control panel applet to adjust the debug level?

Koiby25
July 17th, 2007, 06:44 PM
I did... but I don't know how it works, so it didn't really help.

Koiby25
July 18th, 2007, 12:59 AM
OK, I got some output from it, but none of it helps. The Visual C++ debugger doesn't seem to be stepping through my program at all either.

Bornish
July 18th, 2007, 02:04 AM
You should describe your crash as much as you can if you expect us to help. Describe the exception encountered, show callstack, tell us if it is a multi-threaded app. Also, compile with options to generate a map file and cod listings files. That will help finding the exact line of code which produces the crash.
Since your program doesn't step through, you might have some other project seetings wrong. Maybe debugging information isn't produced or you're running the release configuration.

Koiby25
July 18th, 2007, 03:42 AM
OK, it's not crashing anymore, but there's still a problem with my code to remove the extra vertices. I found that it was crashing during rendering right when "DrawIndexedPrimitive" was called. The old code was:
DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, numPolys*3, 0, numPolys);

I replaced "numPolys*3" with "numVerts" and that fixed it.

The problem now is that the models are not showing up properly. I can post screenshots now, so I will. It looks like "good.jpg" when I use the last code listing. It looks like "bad.jpg" when I use the second to last code listing instead. Of course, "good.jpg" has duplicate vertices.

Bornish
July 18th, 2007, 05:07 AM
Does DrawIndexedPrimitive() return D3D_OK? Is your numPolys smaller or equal to MaxPrimitiveCount member of the D3DCAPS9 structure? Is your numVerts equal to numPolys * 3, since you're rendering D3DPT_TRIANGLELIST? Does your vertices buffer contain numPolys * 3 elements?

Mike Harnad
July 18th, 2007, 08:20 AM
Have you tried using a different D3DPT_ primitive type. You may get better results if you organize your data differently and use one of the other triangle flags.

Koiby25
July 18th, 2007, 03:04 PM
Is your numVerts equal to numPolys * 3, since you're rendering D3DPT_TRIANGLELIST? Does your vertices buffer contain numPolys * 3 elements?

numVerts is not equal to numPolys * 3, and my vertex buffer contains less than numPolys * 3 elements. That's what made it crash, and I already fixed it.