Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize m3d mesh creation #3363

Merged
merged 4 commits into from
Oct 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
raysan5 marked this conversation as resolved.
Show resolved Hide resolved
{
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)
{
Expand Down Expand Up @@ -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);
raysan5 marked this conversation as resolved.
Show resolved Hide resolved

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));
Expand Down