Skip to content

Commit da5407b

Browse files
authored
Optimize m3d mesh creation (#3363)
* Optimize m3d mesh creation * Avoid qsort() in rmodels.c * Revert "Avoid qsort() in rmodels.c" This reverts commit dc1bd55. * Add comment
1 parent bc15c19 commit da5407b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/rmodels.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5567,6 +5567,15 @@ static Model LoadVOX(const char *fileName)
55675567
unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); }
55685568
void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); }
55695569

5570+
// Comparison function for qsort
5571+
static int m3d_compare_faces(const void *a, const void *b)
5572+
{
5573+
m3df_t *fa = (m3df_t *)a;
5574+
m3df_t *fb = (m3df_t *)b;
5575+
5576+
return (fa->materialid - fb->materialid);
5577+
}
5578+
55705579
// Load M3D mesh data
55715580
static Model LoadM3D(const char *fileName)
55725581
{
@@ -5614,6 +5623,9 @@ static Model LoadM3D(const char *fileName)
56145623
// We always need a default material, so we add +1
56155624
model.materialCount++;
56165625

5626+
// failsafe, model should already have faces grouped by material
5627+
qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces);
5628+
56175629
model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh));
56185630
model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int));
56195631
model.materials = (Material *)RL_CALLOC(model.materialCount + 1, sizeof(Material));

0 commit comments

Comments
 (0)