mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-20 02:08:14 +00:00
Ensure m3d faces in non-decreasing materialid sequence (#3385)
This modification replaces the expensive qsort protection with an insertion sort that is near-instantaneous in the expected ordered case.
This commit is contained in:

committed by
GitHub

parent
d309b1eaa7
commit
7ab911b9a4
@@ -5614,19 +5614,26 @@ static Model LoadM3D(const char *fileName)
|
|||||||
// We always need a default material, so we add +1
|
// We always need a default material, so we add +1
|
||||||
model.materialCount++;
|
model.materialCount++;
|
||||||
|
|
||||||
// WARNING: Sorting is not needed, valid M3D model files should already be sorted
|
// Faces must be in non-decreasing materialid order
|
||||||
// faces should already by grouped by material
|
// Verify that quickly, sorting them otherwise.
|
||||||
// Just keeping the sorting function for reference (Check PR #3363)
|
for (i = 1; i < m3d->numface; i++)
|
||||||
/*
|
|
||||||
static int m3d_compare_faces(const void *a, const void *b)
|
|
||||||
{
|
{
|
||||||
m3df_t *fa = (m3df_t *)a;
|
if ( m3d->face[i-1].materialid <= m3d->face[i].materialid )
|
||||||
m3df_t *fb = (m3df_t *)b;
|
continue;
|
||||||
return (fa->materialid - fb->materialid);
|
|
||||||
}
|
|
||||||
|
|
||||||
qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces);
|
// face[i-1] > face[i]. slide face[i] lower.
|
||||||
*/
|
m3df_t slider = m3d->face[i];
|
||||||
|
j = i-1;
|
||||||
|
|
||||||
|
do
|
||||||
|
{ // face[j] > slider, face[j+1] is svailable vacant gap.
|
||||||
|
m3d->face[j+1] = m3d->face[j];
|
||||||
|
j = j-1;
|
||||||
|
}
|
||||||
|
while (j >= 0 && m3d->face[j].materialid > slider.materialid);
|
||||||
|
|
||||||
|
m3d->face[j+1] = slider;
|
||||||
|
}
|
||||||
|
|
||||||
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
|
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
|
||||||
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
|
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
|
||||||
|
Reference in New Issue
Block a user