[SOLVED] FBX negative indices

How to deal with negative indices in FBX files?

I tried using bitwise negation with the ~ operator which should result in the same value as calculating the “real” index like this:

int posIndex = (-1) * negativeIndex - 1

So for example -4 should result in 3: (-1) * (-4) - 1 = 3…

Is this the right way to do it?

There must be a reason they are negative. Find out the reason first. Are they negative in the FBX file? Or are they negative after import?

Googling this, it seems fbx uses negative indices to indicate the end of a polygon.

@Kwyrky are they still negative after processing them, because that shouldn’t be the case. If you’re doing your own processing, bitwise negation should be fine (just make sure the type of the indices is what’s expected short vs int).

The model is loaded into a MonoGame Model object the indices consists of positive and negative values looking into it with the debugger. I also found some mentions googling, that negative values indicate the end of a polygon. And the advice of doing this bitwise negation or using the formula above.

So it is the anvil model I posted lately to the deferred renderer thread together with the blend files packed as rar. I will paste the link here also later, on mobile right now.

I don’t know, if there is something I may have missed when exporting from blender. Or if there is maybe an option to force positive values?

When looping over the indices I just check if they are negative and apply the bitwise negation. So if you mean that by processing they are all positive after that step? I use the Standard model importer. I will check if it is maybe an issue with the data type (short vs int). I try to handle it at the moment by reading the data in to a array of that data type and then converting the result over to an int array.

Edit: here is the link to the model: anvil.rar

I rechecked again, the model has IndexElementSize.SixteenBits
After thinking about it, maybe the bitwise negation should be done only on the short value and not on the int value holding the short value…
But simultaneously should it in principal not work with the other formula / method?

Are you reading them as indexbuffer.GetData<short>()?
Have tried to read them as unsigned shorts, .GetData<ushort>()?

1 Like

Jackpot :smile:

I checked everything. Logged the converted values. Everything seemed to work as it should (regarding the conversion of negative values). The result still looked messed up…

Changed to ushort and everything worked :slight_smile:

So now wondering if dealing with negative indices like this will work in all cases? How does the conversion from short to ushort work internally?

The values are ushort to begin with, no conversion takes place.
the internal buffer is copied directly into the ushort[] array.

oh… that makes sense. So the usage of signed data types messed everything up in the first place.

1 Like