Weird results when trying to extract data from index buffer...

I’m trying to create a list of vertices from a model I load. I first load the model, then loop through meshes and then mesh parts and finally check if it’s using the index buffer.

When I check if it’s using the index buffer, I use this:

if (MeshPart.IndexBuffer != null)
{
        if (MeshPart.IndexBuffer.IndexCount <= 0)
        {
            throw new Exception(); // This may be in error, I'm not sure how monogame handles this...
        }
        //Use the indices here
}

When I use this method, I then extract the indices like so:

int[] Indices = new int[MeshPart.IndexBuffer.IndexCount];
MeshPart.IndexBuffer.GetData(Indices);
foreach (var b in Indices)
{
    Vertices.Add(VertexArr[b]);
}

Unfortunately I get illogically large numbers as “indices” like:


This happens even though my vertex buffer’s # of vertices is 108.

How would I properly use the index buffer and check if I should use it?
Thanks!

Check the IndexElementSize on the indexbuffer.

If it’s 16bits then you need be passing ushort[] instead int[]. It generally doesn’t matter for 32-bit indices unless you’re hitting that last bit but indices are unsigned, negative int32s or int16s will be interpreted as being enormous.

Beware that -1 / ushort.max_value / uint.max_value are magic values when strips (triangle/line) are used, they’re the default restart code. DX10/11 have some additional magic index codes but they’re only used inside of pure (vertex-id only, manual load) vertex-shaders, so you don’t have to worry about that.

Wow thanks for the quick response! Will do!

Was that the problem?

Yeah it was, as soon as I can I’ll post a gif of the finished product!