Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8322e00

Browse files
authored
Fix mtllib reloading: load an mtl file only once (tinyobjloader#327)
1 parent dec0ff0 commit 8322e00

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

tiny_obj_loader.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ bool ParseTextureNameAndOption(std::string *texname, texture_option_t *texopt,
657657
#include <cstring>
658658
#include <fstream>
659659
#include <limits>
660+
#include <set>
660661
#include <sstream>
661662
#include <utility>
662663

@@ -2445,6 +2446,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
24452446
std::string name;
24462447

24472448
// material
2449+
std::set<std::string> material_filenames;
24482450
std::map<std::string, int> material_map;
24492451
int material = -1;
24502452

@@ -2735,6 +2737,11 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
27352737
} else {
27362738
bool found = false;
27372739
for (size_t s = 0; s < filenames.size(); s++) {
2740+
if (material_filenames.count(filenames[s]) > 0) {
2741+
found = true;
2742+
continue;
2743+
}
2744+
27382745
std::string warn_mtl;
27392746
std::string err_mtl;
27402747
bool ok = (*readMatFn)(filenames[s].c_str(), materials,
@@ -2749,6 +2756,7 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
27492756

27502757
if (ok) {
27512758
found = true;
2759+
material_filenames.insert(filenames[s]);
27522760
break;
27532761
}
27542762
}
@@ -2993,6 +3001,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
29933001
std::stringstream errss;
29943002

29953003
// material
3004+
std::set<std::string> material_filenames;
29963005
std::map<std::string, int> material_map;
29973006
int material_id = -1; // -1 = invalid
29983007

@@ -3138,6 +3147,11 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
31383147
} else {
31393148
bool found = false;
31403149
for (size_t s = 0; s < filenames.size(); s++) {
3150+
if (material_filenames.count(filenames[s]) > 0) {
3151+
found = true;
3152+
continue;
3153+
}
3154+
31413155
std::string warn_mtl;
31423156
std::string err_mtl;
31433157
bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
@@ -3153,6 +3167,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
31533167

31543168
if (ok) {
31553169
found = true;
3170+
material_filenames.insert(filenames[s]);
31563171
break;
31573172
}
31583173
}

0 commit comments

Comments
 (0)