Skip to content

Commit

Permalink
Fix #3177 #3109
Browse files Browse the repository at this point in the history
  • Loading branch information
raysan5 committed Aug 10, 2023
1 parent 0959f6e commit f1c31be
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/rmodels.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
#define PAR_SHAPES_IMPLEMENTATION
#include "external/par_shapes.h" // Shapes 3d parametric generation

#if defined(_MSC_VER)
#if defined(_MSC_VER)
#pragma warning(pop) // Disable MSVC warning suppression
#endif
#endif
Expand Down Expand Up @@ -5576,7 +5576,7 @@ static Model LoadM3D(const char *fileName)
m3dp_t *prop = NULL;
unsigned int bytesRead = 0;
unsigned char *fileData = LoadFileData(fileName, &bytesRead);
int i, j, k, l, n, mi = -2;
int i, j, k, l, n, mi = -2, vcolor = 0;

if (fileData != NULL)
{
Expand Down Expand Up @@ -5606,10 +5606,13 @@ static Model LoadM3D(const char *fileName)
}
else
{
model.meshCount = model.materialCount = 1;
model.meshCount = 1; model.materialCount = 0;
TRACELOG(LOG_INFO, "MODEL: No materials, putting all meshes in a default material");
}

// We always need a default material, so we add +1
model.materialCount++;

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 All @@ -5634,17 +5637,24 @@ static Model LoadM3D(const char *fileName)
k++;
mi = m3d->face[i].materialid;

for (j = i, l = 0; (j < (int)m3d->numface) && (mi == m3d->face[j].materialid); j++, l++);
// Only allocate colors VertexBuffer if there's a color vertex in the model for this material batch
// if all colors are fully transparent black for all verteces of this materal, then we assume no vertex colors
for (j = i, l = vcolor = 0; (j < (int)m3d->numface) && (mi == m3d->face[j].materialid); j++, l++)
{
if (!m3d->vertex[m3d->face[j].vertex[0]].color ||
!m3d->vertex[m3d->face[j].vertex[1]].color ||
!m3d->vertex[m3d->face[j].vertex[2]].color) vcolor = 1;
}

model.meshes[k].vertexCount = l*3;
model.meshes[k].triangleCount = l;
model.meshes[k].vertices = (float *)RL_CALLOC(model.meshes[k].vertexCount*3, sizeof(float));
model.meshes[k].texcoords = (float *)RL_CALLOC(model.meshes[k].vertexCount*2, sizeof(float));
model.meshes[k].normals = (float *)RL_CALLOC(model.meshes[k].vertexCount*3, sizeof(float));

// If no map is provided, we allocate storage for vertex colors
// M3D specs only consider vertex colors if no material is provided
if (mi != M3D_UNDEF) model.meshes[k].colors = RL_CALLOC(model.meshes[k].vertexCount*4, sizeof(unsigned char));
// If no map is provided, or we have colors defined, we allocate storage for vertex colors
// M3D specs only consider vertex colors if no material is provided, however raylib uses both and mixes the colors
if ((mi == M3D_UNDEF) || vcolor) model.meshes[k].colors = RL_CALLOC(model.meshes[k].vertexCount*4, sizeof(unsigned char));

if (m3d->numbone && m3d->numskin)
{
Expand Down

0 comments on commit f1c31be

Please sign in to comment.