diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 666e5e9..e170c1d 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -277,7 +277,8 @@ class MaterialReader { virtual bool operator()(const std::string &matId, std::vector *materials, - std::map *matMap, + //std::map *matMap, + std::map *matMap, std::string *err) = 0; }; @@ -288,7 +289,9 @@ class MaterialFileReader : public MaterialReader { virtual ~MaterialFileReader() {} virtual bool operator()(const std::string &matId, std::vector *materials, - std::map *matMap, std::string *err); + //std::map *matMap, + std::map *matMap, + std::string *err); private: std::string m_mtlBaseDir; @@ -301,7 +304,9 @@ class MaterialStreamReader : public MaterialReader { virtual ~MaterialStreamReader() {} virtual bool operator()(const std::string &matId, std::vector *materials, - std::map *matMap, std::string *err); + //std::map *matMap, + std::map *matMap, + std::string *err); private: std::istream &m_inStream; @@ -343,7 +348,9 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, bool triangulate = true); /// Loads materials into std::map -void LoadMtl(std::map *material_map, +void LoadMtl( +//std::map *material_map, +std::map *material_map, std::vector *materials, std::istream *inStream, std::string *warning); @@ -1377,7 +1384,9 @@ static void SplitString(const std::string &s, char delim, } } -void LoadMtl(std::map *material_map, +void LoadMtl( +//std::map *material_map, +std::map *material_map, std::vector *materials, std::istream *inStream, std::string *warning) { // Create a default material anyway. @@ -1599,9 +1608,18 @@ void LoadMtl(std::map *material_map, { // flush previous material. if (!material.name.empty()) { + /* material_map->insert(std::pair( material.name, static_cast(materials->size())) ); + */ + + + uint32_t hsh1 = X31_hash_string(material.name.c_str()); + + material_map->insert(std::pair( + hsh1, static_cast(materials->size())) + ); materials->push_back(material); } @@ -1843,9 +1861,17 @@ void LoadMtl(std::map *material_map, std::pair(key, value)); } } + + uint32_t hsh1 = X31_hash_string(material.name.c_str()); + // flush last material. + /* material_map->insert(std::pair( material.name, static_cast(materials->size()))); + */ + + material_map->insert(std::pair( + hsh1, static_cast(materials->size()))); materials->push_back(material); if (warning) { @@ -1855,7 +1881,8 @@ void LoadMtl(std::map *material_map, bool MaterialFileReader::operator()(const std::string &matId, std::vector *materials, - std::map *matMap, + //std::map *matMap, + std::map *matMap, std::string *err) { std::string filepath; @@ -1896,7 +1923,8 @@ bool MaterialFileReader::operator()(const std::string &matId, bool MaterialStreamReader::operator()(const std::string &matId, std::vector *materials, - std::map *matMap, + //std::map *matMap, + std::map *matMap, std::string *err) { (void)matId; if (!m_inStream) { @@ -1977,10 +2005,12 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, //init hashed tokens map initHashedTokensMap(); - - // material - std::map material_map; + //std::map material_map; + + //tigra: key of material_map is uint32_t now + //because std::map is an red-black trees it is better to store key as int + std::map material_map; int material = -1; shape_t shape; @@ -2192,6 +2222,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::string namebuf = ss.str(); */ + /* std::string namebuf = std::string(token); int newMaterialId = -1; @@ -2200,6 +2231,18 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } else { // { error!! material not found } } + */ + + + + uint32_t hsh = X31_hash_string(token); + + int newMaterialId = -1; + if (material_map.find(hsh) != material_map.end()) { + newMaterialId = material_map[hsh]; + } else { + // { error!! material not found } + } if (newMaterialId != material) { // Create per-face material. Thus we don't add `shape` to `shapes` at @@ -2302,7 +2345,8 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, // material - std::map material_map; + //std::map material_map; + std::map material_map; int material_id = -1; // -1 = invalid std::vector indices; @@ -2528,22 +2572,34 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, std::string namebuf = ss.str(); */ + /* std::string namebuf = std::string(token); - int newMaterialId = -1; if (material_map.find(namebuf) != material_map.end()) { newMaterialId = material_map[namebuf]; } else { // { error!! material not found } } + */ + + //make a hash from token + uint32_t hsh = X31_hash_string(token); + + int newMaterialId = -1; + if (material_map.find(hsh) != material_map.end()) { + newMaterialId = material_map[hsh]; + } else { + // { error!! material not found } + } if (newMaterialId != material_id) { material_id = newMaterialId; } if (callback.usemtl_cb) { - callback.usemtl_cb(user_data, namebuf.c_str(), material_id); + //callback.usemtl_cb(user_data, namebuf.c_str(), material_id); + callback.usemtl_cb(user_data, token, material_id); } continue;