From da5407b77651dc3ffb6ff9df466ac0ec57d82e4f Mon Sep 17 00:00:00 2001 From: DaveH355 <101005658+DaveH355@users.noreply.github.com> Date: Sun, 8 Oct 2023 03:02:05 +0800 Subject: [PATCH] Optimize m3d mesh creation (#3363) * Optimize m3d mesh creation * Avoid qsort() in rmodels.c * Revert "Avoid qsort() in rmodels.c" This reverts commit dc1bd559fdda8d338d480dd7d9c3d77bb1ec5ac2. * Add comment --- src/rmodels.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/rmodels.c b/src/rmodels.c index f9ceec1372bf..c25fe3342340 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -5567,6 +5567,15 @@ static Model LoadVOX(const char *fileName) unsigned char *m3d_loaderhook(char *fn, unsigned int *len) { return LoadFileData((const char *)fn, (int *)len); } void m3d_freehook(void *data) { UnloadFileData((unsigned char *)data); } +// Comparison function for qsort +static int m3d_compare_faces(const void *a, const void *b) +{ + m3df_t *fa = (m3df_t *)a; + m3df_t *fb = (m3df_t *)b; + + return (fa->materialid - fb->materialid); +} + // Load M3D mesh data static Model LoadM3D(const char *fileName) { @@ -5614,6 +5623,9 @@ static Model LoadM3D(const char *fileName) // We always need a default material, so we add +1 model.materialCount++; + // failsafe, model should already have faces grouped by material + qsort(m3d->face, m3d->numface, sizeof(m3df_t), m3d_compare_faces); + model.meshes = (Mesh *)RL_CALLOC(model.meshCount, sizeof(Mesh)); model.meshMaterial = (int *)RL_CALLOC(model.meshCount, sizeof(int)); model.materials = (Material *)RL_CALLOC(model.materialCount + 1, sizeof(Material));