@@ -737,8 +737,9 @@ objzModel *objz_load(const char *_filename) {
737737 }
738738 // Parse the obj file and any material files.
739739 // Faces are triangulated. Other than that, this is straight parsing.
740- Array materials , positions , texcoords , normals , tempObjects , faces ;
740+ Array materialLibs , materials , positions , texcoords , normals , tempObjects , faces ;
741741 Array faceIndices , tempFaceIndices ; // Re-used per face.
742+ arrayInit (& materialLibs , sizeof (char ) * OBJZ_MAX_TOKEN_LENGTH , 1 );
742743 arrayInit (& materials , sizeof (objzMaterial ), 16 );
743744 arrayInit (& positions , sizeof (float ) * 3 , guessArrayInitialSize (file .length , UINT16_MAX , 1 <<21 ));
744745 arrayInit (& texcoords , sizeof (float ) * 2 , guessArrayInitialSize (file .length , UINT16_MAX , UINT16_MAX ));
@@ -843,8 +844,19 @@ objzModel *objz_load(const char *_filename) {
843844 snprintf (s_error , OBJZ_MAX_ERROR_LENGTH , "(%u:%u) Expected name after 'mtllib'" , token .line , token .column );
844845 goto error ;
845846 }
846- if (!loadMaterialFile (_filename , token .text , & materials ))
847- goto error ;
847+ // Don't load the same material library twice.
848+ bool alreadyLoaded = false;
849+ for (uint32_t i = 0 ; i < materialLibs .length ; i ++ ) {
850+ if (OBJZ_STRICMP (token .text , (const char * )OBJZ_ARRAY_ELEMENT (materialLibs , i )) == 0 ) {
851+ alreadyLoaded = true;
852+ break ;
853+ }
854+ }
855+ if (!alreadyLoaded ) {
856+ if (!loadMaterialFile (_filename , token .text , & materials ))
857+ goto error ;
858+ arrayAppend (& materialLibs , token .text );
859+ }
848860 } else if (OBJZ_STRICMP (token .text , "s" ) == 0 ) {
849861 tokenize (& lexer , & token , false);
850862 if (token .text [0 ] == 0 ) {
@@ -888,6 +900,7 @@ objzModel *objz_load(const char *_filename) {
888900 flags |= OBJZ_FLAG_TEXCOORDS ;
889901 }
890902 }
903+ OBJZ_FREE (materialLibs .data );
891904 OBJZ_FREE (faceIndices .data );
892905 OBJZ_FREE (tempFaceIndices .data );
893906 fileClose (& file );
@@ -1042,6 +1055,7 @@ objzModel *objz_load(const char *_filename) {
10421055 return model ;
10431056error :
10441057 fileClose (& file );
1058+ OBJZ_FREE (materialLibs .data );
10451059 OBJZ_FREE (materials .data );
10461060 OBJZ_FREE (positions .data );
10471061 OBJZ_FREE (texcoords .data );
0 commit comments