From 6e579f027f6b3011cfbc17dfef7eca956a86af91 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Wed, 6 Dec 2017 22:37:44 +0200 Subject: [PATCH 01/20] refactoring for new speedup release --- tiny_obj_loader.h | 392 +++++++++++++++++++++++----------------------- 1 file changed, 198 insertions(+), 194 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 65a9d62..18e5bc1 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1092,31 +1092,6 @@ void LoadMtl(std::map *material_map, if (token[0] == '#') continue; // comment line - // new mtl - if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) { - // flush previous material. - if (!material.name.empty()) { - material_map->insert(std::pair( - material.name, static_cast(materials->size()))); - materials->push_back(material); - } - - // initial temporary material - InitMaterial(&material); - - has_d = false; - has_tr = false; - - // set new mtl name - token += 7; - { - std::stringstream sstr; - sstr << token; - material.name = sstr.str(); - } - continue; - } - // ambient if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) { token += 2; @@ -1187,13 +1162,6 @@ void LoadMtl(std::map *material_map, continue; } - // illum model - if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) { - token += 6; - material.illum = parseInt(&token); - continue; - } - // dissolve if ((token[0] == 'd' && IS_SPACE(token[1]))) { token += 1; @@ -1224,33 +1192,62 @@ void LoadMtl(std::map *material_map, continue; } + //tigra: refactoring for new speedup release + if (token[0] == 'P' && IS_SPACE(token[2])) { + token += 2; + // PBR: roughness - if (token[0] == 'P' && token[1] == 'r' && IS_SPACE(token[2])) { - token += 2; - material.roughness = parseReal(&token); - continue; - } - + if(token[1] == 'r') + material.roughness = parseReal(&token); + else // PBR: metallic - if (token[0] == 'P' && token[1] == 'm' && IS_SPACE(token[2])) { - token += 2; - material.metallic = parseReal(&token); - continue; - } - + if(token[1] == 'm') + material.metallic = parseReal(&token); + else // PBR: sheen - if (token[0] == 'P' && token[1] == 's' && IS_SPACE(token[2])) { - token += 2; - material.sheen = parseReal(&token); + if(token[1] == 's') + material.sheen = parseReal(&token); + else + // PBR: clearcoat thickness + if(token[1] == 'c') + material.clearcoat_thickness = parseReal(&token); + } + + + //tigra: refactoring for new speedup release + // new mtl + if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) { + // flush previous material. + if (!material.name.empty()) { + material_map->insert(std::pair( + material.name, static_cast(materials->size()))); + materials->push_back(material); + } + + // initial temporary material + InitMaterial(&material); + + has_d = false; + has_tr = false; + + // set new mtl name + token += 7; + { + std::stringstream sstr; + sstr << token; + material.name = sstr.str(); + } continue; } - // PBR: clearcoat thickness - if (token[0] == 'P' && token[1] == 'c' && IS_SPACE(token[2])) { - token += 2; - material.clearcoat_thickness = parseReal(&token); + // illum model + if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) { + token += 6; + material.illum = parseInt(&token); continue; } + + // PBR: clearcoat roughness if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) { @@ -1638,76 +1635,6 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, continue; } - // use mtl - if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { - token += 7; - std::stringstream ss; - ss << token; - std::string namebuf = ss.str(); - - int newMaterialId = -1; - if (material_map.find(namebuf) != material_map.end()) { - newMaterialId = material_map[namebuf]; - } else { - // { error!! material not found } - } - - if (newMaterialId != material) { - // Create per-face material. Thus we don't add `shape` to `shapes` at - // this time. - // just clear `faceGroup` after `exportFaceGroupToShape()` call. - exportFaceGroupToShape(&shape, faceGroup, tags, material, name, - triangulate); - faceGroup.clear(); - material = newMaterialId; - } - - continue; - } - - // load mtl - if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { - if (readMatFn) { - token += 7; - - std::vector filenames; - SplitString(std::string(token), ' ', filenames); - - if (filenames.empty()) { - if (err) { - (*err) += - "WARN: Looks like empty filename for mtllib. Use default " - "material. \n"; - } - } else { - bool found = false; - for (size_t s = 0; s < filenames.size(); s++) { - std::string err_mtl; - bool ok = (*readMatFn)(filenames[s].c_str(), materials, - &material_map, &err_mtl); - if (err && (!err_mtl.empty())) { - (*err) += err_mtl; // This should be warn message. - } - - if (ok) { - found = true; - break; - } - } - - if (!found) { - if (err) { - (*err) += - "WARN: Failed to load material file(s). Use default " - "material.\n"; - } - } - } - } - - continue; - } - // group name if (token[0] == 'g' && IS_SPACE((token[1]))) { // flush previous face group. @@ -1794,6 +1721,79 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, tags.push_back(tag); } + + + //tigra: compares one more start + // use mtl + if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { + token += 7; + std::stringstream ss; + ss << token; + std::string namebuf = ss.str(); + + int newMaterialId = -1; + if (material_map.find(namebuf) != material_map.end()) { + newMaterialId = material_map[namebuf]; + } else { + // { error!! material not found } + } + + if (newMaterialId != material) { + // Create per-face material. Thus we don't add `shape` to `shapes` at + // this time. + // just clear `faceGroup` after `exportFaceGroupToShape()` call. + exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + triangulate); + faceGroup.clear(); + material = newMaterialId; + } + + continue; + } + + // load mtl + if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + if (readMatFn) { + token += 7; + + std::vector filenames; + SplitString(std::string(token), ' ', filenames); + + if (filenames.empty()) { + if (err) { + (*err) += + "WARN: Looks like empty filename for mtllib. Use default " + "material. \n"; + } + } else { + bool found = false; + for (size_t s = 0; s < filenames.size(); s++) { + std::string err_mtl; + bool ok = (*readMatFn)(filenames[s].c_str(), materials, + &material_map, &err_mtl); + if (err && (!err_mtl.empty())) { + (*err) += err_mtl; // This should be warn message. + } + + if (ok) { + found = true; + break; + } + } + + if (!found) { + if (err) { + (*err) += + "WARN: Failed to load material file(s). Use default " + "material.\n"; + } + } + } + } + + continue; + } + //tigra: compares one more END // Ignore unknown command. } @@ -1927,79 +1927,6 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, continue; } - // use mtl - if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { - token += 7; - std::stringstream ss; - ss << token; - std::string namebuf = ss.str(); - - int newMaterialId = -1; - if (material_map.find(namebuf) != material_map.end()) { - newMaterialId = material_map[namebuf]; - } 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); - } - - continue; - } - - // load mtl - if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { - if (readMatFn) { - token += 7; - - std::vector filenames; - SplitString(std::string(token), ' ', filenames); - - if (filenames.empty()) { - if (err) { - (*err) += - "WARN: Looks like empty filename for mtllib. Use default " - "material. \n"; - } - } else { - bool found = false; - for (size_t s = 0; s < filenames.size(); s++) { - std::string err_mtl; - bool ok = (*readMatFn)(filenames[s].c_str(), &materials, - &material_map, &err_mtl); - if (err && (!err_mtl.empty())) { - (*err) += err_mtl; // This should be warn message. - } - - if (ok) { - found = true; - break; - } - } - - if (!found) { - if (err) { - (*err) += - "WARN: Failed to load material file(s). Use default " - "material.\n"; - } - } else { - if (callback.mtllib_cb) { - callback.mtllib_cb(user_data, &materials.at(0), - static_cast(materials.size())); - } - } - } - } - - continue; - } - // group name if (token[0] == 'g' && IS_SPACE((token[1]))) { names.clear(); @@ -2091,6 +2018,83 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, } #endif + + + //tigra: compares start + // use mtl + if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { + token += 7; + std::stringstream ss; + ss << token; + std::string namebuf = ss.str(); + + int newMaterialId = -1; + if (material_map.find(namebuf) != material_map.end()) { + newMaterialId = material_map[namebuf]; + } 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); + } + + continue; + } + + // load mtl + if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + if (readMatFn) { + token += 7; + + std::vector filenames; + SplitString(std::string(token), ' ', filenames); + + if (filenames.empty()) { + if (err) { + (*err) += + "WARN: Looks like empty filename for mtllib. Use default " + "material. \n"; + } + } else { + bool found = false; + for (size_t s = 0; s < filenames.size(); s++) { + std::string err_mtl; + bool ok = (*readMatFn)(filenames[s].c_str(), &materials, + &material_map, &err_mtl); + if (err && (!err_mtl.empty())) { + (*err) += err_mtl; // This should be warn message. + } + + if (ok) { + found = true; + break; + } + } + + if (!found) { + if (err) { + (*err) += + "WARN: Failed to load material file(s). Use default " + "material.\n"; + } + } else { + if (callback.mtllib_cb) { + callback.mtllib_cb(user_data, &materials.at(0), + static_cast(materials.size())); + } + } + } + } + + continue; + } + //tigra: compares end + // Ignore unknown command. } From ee2c734c15980b94886d545d16908be072597cc3 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Wed, 6 Dec 2017 22:44:04 +0200 Subject: [PATCH 02/20] refactoring for new speedup release1 --- tiny_obj_loader.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 18e5bc1..fe40f7a 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1723,6 +1723,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } + //tigra: refactoring for new speedup release //tigra: compares one more start // use mtl if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { @@ -1793,7 +1794,8 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, continue; } - //tigra: compares one more END + //tigra: compares one more END + //tigra: refactoring for new speedup release // Ignore unknown command. } @@ -2020,6 +2022,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, + //tigra: refactoring for new speedup release //tigra: compares start // use mtl if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { @@ -2094,6 +2097,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, continue; } //tigra: compares end + //tigra: refactoring for new speedup release // Ignore unknown command. } From b2f07d10aaac1c10badf75b537efa96b6a4b8de0 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Thu, 7 Dec 2017 08:38:44 +0200 Subject: [PATCH 03/20] 1.1.2 : new hashed keywords --- tiny_obj_loader.h | 432 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 388 insertions(+), 44 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index fe40f7a..a05064a 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -23,6 +23,7 @@ THE SOFTWARE. */ // +// version 1.1.2 : Used new hashed keywords // version 1.1.0 : Support parsing vertex color(#144) // version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138) // version 1.0.7 : Support multiple tex options(#126) @@ -49,6 +50,9 @@ THE SOFTWARE. #include #include +//tigra: unordered_map for hashed keywords +#include + namespace tinyobj { // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ... @@ -363,7 +367,138 @@ void LoadMtl(std::map *material_map, #include namespace tinyobj { + +//tigra: x31 hash function +typedef uint32_t khint_t; + +inline uint32_t X31_hash_string(const char *s) +{ + khint_t h = *s; + for (++s ; *s; ++s) h = (h << 5) - h + *s; + return h; +} + +inline uint32_t X31_hash_stringSZ(const char *s, int sz) +{ + int i; + khint_t h = *s; + + for (++s, i = sz-1 ; i && *s; ++s, i--) h = (h << 5) - h + *s; + return h; +} + + +//tigra: refactoring2 - add list of keywords +static char * keywords[] = { + //on off + "on","off", + + //TextureType + "cube_top", "cube_bottom", "cube_left", "cube_right", "cube_front", "cube_back", "sphere", + + //TextureNameAndOption + "-blendu", "-blendv", "-clamp", "-boost", "-bm", "-o", "-s", "-t", "-type", "-imfchan", "-mm", + + //newmtl, illum, mtllib, usemtl + "newmtl", "illum", "mtllib", "usemtl", + + //PBR params + "Pcr", "aniso", "anisor", + + //maps + "map_Ka", "map_Kd", "map_Ks", "map_Ns", "map_bump", "map_Bump", "bump", + + // alpha texture + "map_d", + + // displacement texture + "disp", + + // reflection map + "refl", + + // PBR: roughness texture, metallic texture + "map_Pr", "map_Pm", + + // PBR: sheen texture + "map_Ps", + + // PBR: emissive texture + "map_Ke", + + // PBR: normal map texture + "norm" +}; + +//tigra: enum of tokens +enum tokens_enum { + //on off + TOK_on, TOK_off, + + //TextureType + TOK_cube_top, TOK_cube_bottom, TOK_cube_left, TOK_cube_right, TOK_cube_front, TOK_cube_back, TOK_sphere, + + //TextureNameAndOption + TOK_blendu, TOK_blendv, TOK_clamp, TOK_boost, TOK_bm, TOK_o, TOK_s, TOK_t, TOK_type, TOK_imfchan, TOK_mm, + + //newmtl, illum, mtllib, usemtl + TOK_newmtl, TOK_illum, TOK_mtllib, TOK_usemtl, + + //PBR params + TOK_Pcr, TOK_aniso, TOK_anisor, + + //maps + TOK_map_Ka, TOK_map_Kd, TOK_map_Ks, TOK_map_Ns, TOK_map_bump, TOK_map_Bump, TOK_bump, + + // alpha texture + TOK_map_d, + + // displacement texture + TOK_disp, + + // reflection map + TOK_refl, + + // PBR: roughness texture, metallic texture + TOK_map_Pr, TOK_map_Pm, + + // PBR: sheen texture + TOK_map_Ps, + + // PBR: emissive texture + TOK_map_Ke, + + // PBR: normal map texture + TOK_norm +}; + +std::unordered_map hashed_toks; + + + + + +void initHashedTokensMap() +{ + //init hashed tokens map + + uint32_t hhh; + int iii; + + if(hashed_toks.empty()) + { + for(iii=sizeof(keywords)/sizeof(char*);iii;iii--) + { + hhh = X31_hash_string(keywords[iii-1]); + hashed_toks[hhh] = iii-1; + } + } + //init hashed tokens map END +} + + + MaterialReader::~MaterialReader() {} struct vertex_index { @@ -697,20 +832,56 @@ static inline texture_type_t parseTextureType( (*token) += strspn((*token), " \t"); const char *end = (*token) + strcspn((*token), " \t\r"); texture_type_t ty = default_value; + + + uint32_t a_hash; + + int a_tok; + + //init hashed tokens map + initHashedTokensMap(); - if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) { + + a_hash = X31_hash_string(*token); + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + + //if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) + if (a_tok == TOK_cube_top) + { ty = TEXTURE_TYPE_CUBE_TOP; - } else if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) { + } else + //if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) + if (a_tok == TOK_cube_bottom) + { ty = TEXTURE_TYPE_CUBE_BOTTOM; - } else if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) { + } else + //if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) + if (a_tok == TOK_cube_left) + { ty = TEXTURE_TYPE_CUBE_LEFT; - } else if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) { + } else + //if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) + if (a_tok == TOK_cube_right) + { ty = TEXTURE_TYPE_CUBE_RIGHT; - } else if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) { + } else + //if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) + if (a_tok == TOK_cube_front) + { ty = TEXTURE_TYPE_CUBE_FRONT; - } else if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) { + } else + //if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) + if (a_tok == TOK_cube_back) + { ty = TEXTURE_TYPE_CUBE_BACK; - } else if ((0 == strncmp((*token), "sphere", strlen("sphere")))) { + } else + //if ((0 == strncmp((*token), "sphere", strlen("sphere")))) + if (a_tok == TOK_sphere) + { ty = TEXTURE_TYPE_SPHERE; } @@ -862,40 +1033,93 @@ static bool ParseTextureNameAndOption(std::string *texname, texopt->type = TEXTURE_TYPE_NONE; const char *token = linebuf; // Assume line ends with NULL + + + + uint32_t token_sz, a_hash; + + int a_tok; + + //init hashed tokens map + initHashedTokensMap(); + + while (!IS_NEW_LINE((*token))) { token += strspn(token, " \t"); // skip space - if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) { + + + + token_sz = strpbrk(token, " \t\r") - token; // token length + + a_hash = X31_hash_stringSZ(token, token_sz); + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + + + //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendu) + { token += 8; texopt->blendu = parseOnOff(&token, /* default */ true); - } else if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) { + } else + //if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendv) + { token += 8; texopt->blendv = parseOnOff(&token, /* default */ true); - } else if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) { + } else + //if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_clamp) + { token += 7; texopt->clamp = parseOnOff(&token, /* default */ true); - } else if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) { + } else + //if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_boost) + { token += 7; texopt->sharpness = parseReal(&token, 1.0); - } else if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) { + } else + //if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_bm) + { token += 4; texopt->bump_multiplier = parseReal(&token, 1.0); - } else if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) { + } else + //if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_o) + { token += 3; parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), &(texopt->origin_offset[2]), &token); - } else if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) { + } else + //if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_s) + { token += 3; parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), &token, 1.0, 1.0, 1.0); - } else if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) { + } else + //if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_t) + { token += 3; parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), &(texopt->turbulence[2]), &token); - } else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) { + } else + //if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) + if (a_tok == TOK_type) + { token += 5; texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); - } else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) { + } else + //if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) + if (a_tok == TOK_imfchan) + { token += 9; token += strspn(token, " \t"); const char *end = token + strcspn(token, " \t\r"); @@ -903,7 +1127,10 @@ static bool ParseTextureNameAndOption(std::string *texname, texopt->imfchan = (*token); } token = end; - } else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) { + } else + //if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_mm) + { token += 4; parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); } else { @@ -1055,7 +1282,17 @@ void LoadMtl(std::map *material_map, // Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification. bool has_d = false; - bool has_tr = false; + bool has_tr = false; + + + uint32_t token_sz, a_hash; + + int a_tok; + + //init hashed tokens map + initHashedTokensMap(); + + std::stringstream ss; @@ -1213,10 +1450,24 @@ void LoadMtl(std::map *material_map, material.clearcoat_thickness = parseReal(&token); } + + //get token to char array + + token_sz = strpbrk(token, " \t\r") - token; // token length + + a_hash = X31_hash_stringSZ(token, token_sz); + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + //tigra: refactoring for new speedup release - // new mtl - if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) { + // new mtl + //if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_newmtl) + { // flush previous material. if (!material.name.empty()) { material_map->insert(std::pair( @@ -1241,7 +1492,9 @@ void LoadMtl(std::map *material_map, } // illum model - if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) { + //if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) + if (a_tok == TOK_illum) + { token += 6; material.illum = parseInt(&token); continue; @@ -1250,28 +1503,36 @@ void LoadMtl(std::map *material_map, // PBR: clearcoat roughness - if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) { + //if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) + if (a_tok == TOK_Pcr) + { token += 4; material.clearcoat_roughness = parseReal(&token); continue; } // PBR: anisotropy - if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) { + //if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_aniso) + { token += 6; material.anisotropy = parseReal(&token); continue; } // PBR: anisotropy rotation - if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_anisor) + { token += 7; material.anisotropy_rotation = parseReal(&token); continue; } // ambient texture - if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ka) + { token += 7; ParseTextureNameAndOption(&(material.ambient_texname), &(material.ambient_texopt), token, @@ -1280,7 +1541,9 @@ void LoadMtl(std::map *material_map, } // diffuse texture - if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Kd) + { token += 7; ParseTextureNameAndOption(&(material.diffuse_texname), &(material.diffuse_texopt), token, @@ -1289,7 +1552,9 @@ void LoadMtl(std::map *material_map, } // specular texture - if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ks) + { token += 7; ParseTextureNameAndOption(&(material.specular_texname), &(material.specular_texopt), token, @@ -1298,7 +1563,9 @@ void LoadMtl(std::map *material_map, } // specular highlight texture - if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ns) + { token += 7; ParseTextureNameAndOption(&(material.specular_highlight_texname), &(material.specular_highlight_texopt), token, @@ -1307,7 +1574,9 @@ void LoadMtl(std::map *material_map, } // bump texture - if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) { + //if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_bump) + { token += 9; ParseTextureNameAndOption(&(material.bump_texname), &(material.bump_texopt), token, @@ -1316,7 +1585,9 @@ void LoadMtl(std::map *material_map, } // bump texture - if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) { + //if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_Bump) + { token += 9; ParseTextureNameAndOption(&(material.bump_texname), &(material.bump_texopt), token, @@ -1325,7 +1596,9 @@ void LoadMtl(std::map *material_map, } // bump texture - if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) { + //if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_bump) + { token += 5; ParseTextureNameAndOption(&(material.bump_texname), &(material.bump_texopt), token, @@ -1334,7 +1607,9 @@ void LoadMtl(std::map *material_map, } // alpha texture - if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) { + //if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_map_d) + { token += 6; material.alpha_texname = token; ParseTextureNameAndOption(&(material.alpha_texname), @@ -1344,7 +1619,9 @@ void LoadMtl(std::map *material_map, } // displacement texture - if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) { + //if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_disp) + { token += 5; ParseTextureNameAndOption(&(material.displacement_texname), &(material.displacement_texopt), token, @@ -1353,7 +1630,9 @@ void LoadMtl(std::map *material_map, } // reflection map - if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) { + //if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_refl) + { token += 5; ParseTextureNameAndOption(&(material.reflection_texname), &(material.reflection_texopt), token, @@ -1362,7 +1641,9 @@ void LoadMtl(std::map *material_map, } // PBR: roughness texture - if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pr) + { token += 7; ParseTextureNameAndOption(&(material.roughness_texname), &(material.roughness_texopt), token, @@ -1371,7 +1652,9 @@ void LoadMtl(std::map *material_map, } // PBR: metallic texture - if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pm) + { token += 7; ParseTextureNameAndOption(&(material.metallic_texname), &(material.metallic_texopt), token, @@ -1380,7 +1663,9 @@ void LoadMtl(std::map *material_map, } // PBR: sheen texture - if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ps) + { token += 7; ParseTextureNameAndOption(&(material.sheen_texname), &(material.sheen_texopt), token, @@ -1389,7 +1674,9 @@ void LoadMtl(std::map *material_map, } // PBR: emissive texture - if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) { + //if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ke) + { token += 7; ParseTextureNameAndOption(&(material.emissive_texname), &(material.emissive_texopt), token, @@ -1398,7 +1685,9 @@ void LoadMtl(std::map *material_map, } // PBR: normal map texture - if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) { + //if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_norm) + { token += 5; ParseTextureNameAndOption( &(material.normal_texname), &(material.normal_texopt), token, @@ -1441,6 +1730,7 @@ bool MaterialFileReader::operator()(const std::string &matId, filepath = matId; } + //tigra: add buffered stream std::ifstream matIStream(filepath.c_str()); if (!matIStream) { std::stringstream ss; @@ -1500,6 +1790,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::stringstream errss; + //tigra: add buffered stream std::ifstream ifs(filename); if (!ifs) { errss << "Cannot open file [" << filename << "]" << std::endl; @@ -1532,6 +1823,17 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::vector tags; std::vector > faceGroup; std::string name; + + + + uint32_t token_sz, a_hash; + + int a_tok; + + //init hashed tokens map + initHashedTokensMap(); + + // material std::map material_map; @@ -1725,8 +2027,22 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, //tigra: refactoring for new speedup release //tigra: compares one more start + + //get token to char array + + token_sz = strpbrk(token, " \t\r") - token; // token length + + a_hash = X31_hash_stringSZ(token, token_sz); + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + // use mtl - if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { + //if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) + if (a_tok==TOK_usemtl) + { token += 7; std::stringstream ss; ss << token; @@ -1753,7 +2069,9 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } // load mtl - if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + //if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) + if (a_tok==TOK_mtllib) + { if (readMatFn) { token += 7; @@ -1828,6 +2146,16 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, MaterialReader *readMatFn /*= NULL*/, std::string *err /*= NULL*/) { std::stringstream errss; + + + uint32_t token_sz, a_hash; + + int a_tok; + + //init hashed tokens map + initHashedTokensMap(); + + // material std::map material_map; @@ -2024,8 +2352,22 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, //tigra: refactoring for new speedup release //tigra: compares start + + //get token to char array + + token_sz = strpbrk(token, " \t\r") - token; // token length + + a_hash = X31_hash_stringSZ(token, token_sz); + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + // use mtl - if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { + //if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) + if (a_tok==TOK_usemtl) + { token += 7; std::stringstream ss; ss << token; @@ -2050,7 +2392,9 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, } // load mtl - if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + //if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_mtllib) + { if (readMatFn) { token += 7; From baa62f4d891b27e61079b80b6ed92686dcbe69e3 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 08:43:33 +0200 Subject: [PATCH 04/20] small fixes --- tiny_obj_loader.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index a05064a..4bd2f78 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -2357,7 +2357,14 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, token_sz = strpbrk(token, " \t\r") - token; // token length - a_hash = X31_hash_stringSZ(token, token_sz); + if(token_sz<1) //delimiter not found, token_sz = strlen(token) + { + //token_sz=strlen(token); + a_hash = X31_hash_string(token); + } + else + a_hash = X31_hash_stringSZ(token, token_sz); + a_tok = -1; if(hashed_toks.find(a_hash) != hashed_toks.end()) From b818a34f1a7edbab9661132204b59974e59b7e59 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 09:05:42 +0200 Subject: [PATCH 05/20] small fixes2 --- tiny_obj_loader.h | 95 ++++++++++++++++------------------------------- 1 file changed, 33 insertions(+), 62 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 4bd2f78..66fd5f7 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -497,6 +497,33 @@ void initHashedTokensMap() //init hashed tokens map END } + +int token2tok(const char* token) +{ + + uint32_t token_sz, a_hash; + + int a_tok; + + + token_sz = strpbrk(token, " \t\r") - token; // token length + + if(token_sz<1) //delimiter not found, token_sz = strlen(token) + { + //token_sz=strlen(token); + a_hash = X31_hash_string(token); + } + else + a_hash = X31_hash_stringSZ(token, token_sz); + + + a_tok = -1; + if(hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + return a_tok; +} + MaterialReader::~MaterialReader() {} @@ -834,19 +861,13 @@ static inline texture_type_t parseTextureType( texture_type_t ty = default_value; - uint32_t a_hash; int a_tok; //init hashed tokens map initHashedTokensMap(); - - a_hash = X31_hash_string(*token); - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; + a_tok = token2tok(*token); //if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) @@ -1035,9 +1056,6 @@ static bool ParseTextureNameAndOption(std::string *texname, const char *token = linebuf; // Assume line ends with NULL - - uint32_t token_sz, a_hash; - int a_tok; //init hashed tokens map @@ -1048,17 +1066,8 @@ static bool ParseTextureNameAndOption(std::string *texname, while (!IS_NEW_LINE((*token))) { token += strspn(token, " \t"); // skip space - - - token_sz = strpbrk(token, " \t\r") - token; // token length - - a_hash = X31_hash_stringSZ(token, token_sz); - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; - - + a_tok = token2tok(token); + //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) if (a_tok == TOK_blendu) @@ -1284,9 +1293,6 @@ void LoadMtl(std::map *material_map, bool has_d = false; bool has_tr = false; - - uint32_t token_sz, a_hash; - int a_tok; //init hashed tokens map @@ -1451,16 +1457,7 @@ void LoadMtl(std::map *material_map, } - //get token to char array - - token_sz = strpbrk(token, " \t\r") - token; // token length - - a_hash = X31_hash_stringSZ(token, token_sz); - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; - + a_tok = token2tok(token); //tigra: refactoring for new speedup release @@ -1825,9 +1822,6 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::string name; - - uint32_t token_sz, a_hash; - int a_tok; //init hashed tokens map @@ -2028,15 +2022,8 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, //tigra: refactoring for new speedup release //tigra: compares one more start - //get token to char array - token_sz = strpbrk(token, " \t\r") - token; // token length - - a_hash = X31_hash_stringSZ(token, token_sz); - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; + a_tok = token2tok(token); // use mtl @@ -2147,8 +2134,6 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, std::string *err /*= NULL*/) { std::stringstream errss; - - uint32_t token_sz, a_hash; int a_tok; @@ -2353,22 +2338,8 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, //tigra: refactoring for new speedup release //tigra: compares start - //get token to char array - token_sz = strpbrk(token, " \t\r") - token; // token length - - if(token_sz<1) //delimiter not found, token_sz = strlen(token) - { - //token_sz=strlen(token); - a_hash = X31_hash_string(token); - } - else - a_hash = X31_hash_stringSZ(token, token_sz); - - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; + a_tok = token2tok(token); // use mtl From 5d7f6bf539a8952dfd3263ac018b1e3f6bba79dc Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 13:21:45 +0200 Subject: [PATCH 06/20] buffered file read --- deploy.bat | 3 ++ tiny_obj_loader.h | 112 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 deploy.bat diff --git a/deploy.bat b/deploy.bat new file mode 100644 index 0000000..8161f4f --- /dev/null +++ b/deploy.bat @@ -0,0 +1,3 @@ +git add * +git commit -m %1 +git push origin master \ No newline at end of file diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 66fd5f7..463f0c3 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -50,9 +50,6 @@ THE SOFTWARE. #include #include -//tigra: unordered_map for hashed keywords -#include - namespace tinyobj { // https://en.wikipedia.org/wiki/Wavefront_.obj_file says ... @@ -366,8 +363,99 @@ void LoadMtl(std::map *material_map, #include #include +//tigra: unordered_map for hashed keywords +#include + + +#define TINYOBJLOADER_IMPLEMENTATION_BUFREAD + + + +#ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD +#include + #include + #include + #include + + +#define O_LARGEFILE 0100000 +#endif + namespace tinyobj { + +#ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD +//tigra: ImportInBuf - buffered input file +class ImportInBuf: public std::streambuf +{ +public: + +ImportInBuf(const char* filename, size_t buf_sz_=64*1024) +:fd_(open(filename,O_RDONLY | O_LARGEFILE)) +{ + //fprintf(stderr, "ImportInBuf(%s\n", filename); + +if(fd_<0) +{ + fprintf(stderr, "can't open file %s\n",filename); + exit(1); +} + +buf_sz = buf_sz_; + +//fprintf(stderr, "buf_sz=%d\n", buf_sz); + +buffer_ = (char*) malloc(buf_sz); + +setg(buffer_,buffer_,buffer_); +struct stat st; +fstat(fd_,&st); +fsize_=st.st_size; + +//fprintf(stderr, "file size: %ld\n", fsize_); +} + +// you don't have to do it like this if your streams are 64 bit +void seekg(uint64_t pos) +{ +lseek(fd_,pos,SEEK_SET); +pos_=pos; +setg(buffer_,buffer_,buffer_); +} + +uint64_t tellg()const { return pos_; } +uint64_t size()const { return fsize_; } + +~ImportInBuf(){ close(fd_); free(buffer_); } + +private: +ImportInBuf(const ImportInBuf&); +ImportInBuf& operator=(const ImportInBuf&); +virtual int underflow() +{ +if(gptr() *shapes, std::stringstream errss; - //tigra: add buffered stream - std::ifstream ifs(filename); + //tigra: add buffered stream + #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD + ImportInBuf buf(filename); + std::istream ifs(&buf); + #else + std::ifstream ifs(filename); + #endif + if (!ifs) { errss << "Cannot open file [" << filename << "]" << std::endl; if (err) { From c016910317d183dd63b93144fa28bdaa5e8dc830 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 21:53:36 +0200 Subject: [PATCH 07/20] minimize token checks --- tiny_obj_loader.h | 652 +++++++++++++++++++++++----------------------- 1 file changed, 333 insertions(+), 319 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 463f0c3..8ad9b75 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -946,6 +946,7 @@ static inline texture_type_t parseTextureType( const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) { (*token) += strspn((*token), " \t"); const char *end = (*token) + strcspn((*token), " \t\r"); + texture_type_t ty = default_value; @@ -957,42 +958,47 @@ static inline texture_type_t parseTextureType( a_tok = token2tok(*token); + //tigra: dont check if wrong token + if(a_tok>=TOK_cube_top && a_tok<=TOK_sphere) + { - //if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) - if (a_tok == TOK_cube_top) - { - ty = TEXTURE_TYPE_CUBE_TOP; - } else - //if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) - if (a_tok == TOK_cube_bottom) + //if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) + if (a_tok == TOK_cube_top) { - ty = TEXTURE_TYPE_CUBE_BOTTOM; - } else - //if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) - if (a_tok == TOK_cube_left) - { - ty = TEXTURE_TYPE_CUBE_LEFT; - } else - //if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) - if (a_tok == TOK_cube_right) - { - ty = TEXTURE_TYPE_CUBE_RIGHT; - } else - //if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) - if (a_tok == TOK_cube_front) - { - ty = TEXTURE_TYPE_CUBE_FRONT; - } else - //if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) - if (a_tok == TOK_cube_back) - { - ty = TEXTURE_TYPE_CUBE_BACK; - } else - //if ((0 == strncmp((*token), "sphere", strlen("sphere")))) - if (a_tok == TOK_sphere) - { - ty = TEXTURE_TYPE_SPHERE; - } + ty = TEXTURE_TYPE_CUBE_TOP; + } else + //if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) + if (a_tok == TOK_cube_bottom) + { + ty = TEXTURE_TYPE_CUBE_BOTTOM; + } else + //if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) + if (a_tok == TOK_cube_left) + { + ty = TEXTURE_TYPE_CUBE_LEFT; + } else + //if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) + if (a_tok == TOK_cube_right) + { + ty = TEXTURE_TYPE_CUBE_RIGHT; + } else + //if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) + if (a_tok == TOK_cube_front) + { + ty = TEXTURE_TYPE_CUBE_FRONT; + } else + //if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) + if (a_tok == TOK_cube_back) + { + ty = TEXTURE_TYPE_CUBE_BACK; + } else + //if ((0 == strncmp((*token), "sphere", strlen("sphere")))) + if (a_tok == TOK_sphere) + { + ty = TEXTURE_TYPE_SPHERE; + } + + } (*token) = end; return ty; @@ -1155,82 +1161,86 @@ static bool ParseTextureNameAndOption(std::string *texname, token += strspn(token, " \t"); // skip space a_tok = token2tok(token); - - //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) - if (a_tok == TOK_blendu) - { - token += 8; - texopt->blendu = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) - if (a_tok == TOK_blendv) - { - token += 8; - texopt->blendv = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_clamp) - { - token += 7; - texopt->clamp = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_boost) - { - token += 7; - texopt->sharpness = parseReal(&token, 1.0); - } else - //if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) - if (a_tok == TOK_bm) - { - token += 4; - texopt->bump_multiplier = parseReal(&token, 1.0); - } else - //if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_o) - { - token += 3; - parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), - &(texopt->origin_offset[2]), &token); - } else - //if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_s) - { - token += 3; - parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), - &token, 1.0, 1.0, 1.0); - } else - //if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_t) - { - token += 3; - parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), - &(texopt->turbulence[2]), &token); - } else - //if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) - if (a_tok == TOK_type) - { - token += 5; - texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); - } else - //if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) - if (a_tok == TOK_imfchan) - { - token += 9; - token += strspn(token, " \t"); - const char *end = token + strcspn(token, " \t\r"); - if ((end - token) == 1) { // Assume one char for -imfchan - texopt->imfchan = (*token); - } - token = end; - } else - //if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) - if (a_tok == TOK_mm) - { - token += 4; - parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); - } else { + //tigra: minimize checks + if(a_tok>=TOK_blendu && a_tok<=TOK_mm) + { + //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendu) + { + token += 8; + texopt->blendu = parseOnOff(&token, /* default */ true); + } else + //if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendv) + { + token += 8; + texopt->blendv = parseOnOff(&token, /* default */ true); + } else + //if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_clamp) + { + token += 7; + texopt->clamp = parseOnOff(&token, /* default */ true); + } else + //if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_boost) + { + token += 7; + texopt->sharpness = parseReal(&token, 1.0); + } else + //if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_bm) + { + token += 4; + texopt->bump_multiplier = parseReal(&token, 1.0); + } else + //if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_o) + { + token += 3; + parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), + &(texopt->origin_offset[2]), &token); + } else + //if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_s) + { + token += 3; + parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), + &token, 1.0, 1.0, 1.0); + } else + //if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_t) + { + token += 3; + parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), + &(texopt->turbulence[2]), &token); + } else + //if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) + if (a_tok == TOK_type) + { + token += 5; + texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); + } else + //if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) + if (a_tok == TOK_imfchan) + { + token += 9; + token += strspn(token, " \t"); + const char *end = token + strcspn(token, " \t\r"); + if ((end - token) == 1) { // Assume one char for -imfchan + texopt->imfchan = (*token); + } + token = end; + } else + //if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_mm) + { + token += 4; + parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); + } + } + else { // Assume texture filename #if 0 size_t len = strcspn(token, " \t\r"); // untile next space @@ -1545,240 +1555,244 @@ void LoadMtl(std::map *material_map, } - a_tok = token2tok(token); + a_tok = token2tok(token); - - //tigra: refactoring for new speedup release - // new mtl - //if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_newmtl) + //tigra: minimize checks + if(a_tok>=TOK_newmtl && a_tok<=TOK_norm) { - // flush previous material. - if (!material.name.empty()) { - material_map->insert(std::pair( - material.name, static_cast(materials->size()))); - materials->push_back(material); - } + + //tigra: refactoring for new speedup release + // new mtl + //if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_newmtl) + { + // flush previous material. + if (!material.name.empty()) { + material_map->insert(std::pair( + material.name, static_cast(materials->size()))); + materials->push_back(material); + } - // initial temporary material - InitMaterial(&material); + // initial temporary material + InitMaterial(&material); - has_d = false; - has_tr = false; + has_d = false; + has_tr = false; - // set new mtl name - token += 7; - { - std::stringstream sstr; - sstr << token; - material.name = sstr.str(); - } - continue; - } + // set new mtl name + token += 7; + { + std::stringstream sstr; + sstr << token; + material.name = sstr.str(); + } + continue; + } - // illum model - //if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) - if (a_tok == TOK_illum) - { - token += 6; - material.illum = parseInt(&token); - continue; - } - - + // illum model + //if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) + if (a_tok == TOK_illum) + { + token += 6; + material.illum = parseInt(&token); + continue; + } + + - // PBR: clearcoat roughness - //if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) - if (a_tok == TOK_Pcr) - { - token += 4; - material.clearcoat_roughness = parseReal(&token); - continue; - } + // PBR: clearcoat roughness + //if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) + if (a_tok == TOK_Pcr) + { + token += 4; + material.clearcoat_roughness = parseReal(&token); + continue; + } - // PBR: anisotropy - //if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) - if (a_tok == TOK_aniso) - { - token += 6; - material.anisotropy = parseReal(&token); - continue; - } + // PBR: anisotropy + //if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_aniso) + { + token += 6; + material.anisotropy = parseReal(&token); + continue; + } - // PBR: anisotropy rotation - //if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_anisor) - { - token += 7; - material.anisotropy_rotation = parseReal(&token); - continue; - } + // PBR: anisotropy rotation + //if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_anisor) + { + token += 7; + material.anisotropy_rotation = parseReal(&token); + continue; + } - // ambient texture - //if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ka) - { - token += 7; - ParseTextureNameAndOption(&(material.ambient_texname), - &(material.ambient_texopt), token, - /* is_bump */ false); - continue; - } + // ambient texture + //if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ka) + { + token += 7; + ParseTextureNameAndOption(&(material.ambient_texname), + &(material.ambient_texopt), token, + /* is_bump */ false); + continue; + } - // diffuse texture - //if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Kd) - { - token += 7; - ParseTextureNameAndOption(&(material.diffuse_texname), - &(material.diffuse_texopt), token, - /* is_bump */ false); - continue; - } + // diffuse texture + //if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Kd) + { + token += 7; + ParseTextureNameAndOption(&(material.diffuse_texname), + &(material.diffuse_texopt), token, + /* is_bump */ false); + continue; + } - // specular texture - //if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ks) - { - token += 7; - ParseTextureNameAndOption(&(material.specular_texname), - &(material.specular_texopt), token, - /* is_bump */ false); - continue; - } + // specular texture + //if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ks) + { + token += 7; + ParseTextureNameAndOption(&(material.specular_texname), + &(material.specular_texopt), token, + /* is_bump */ false); + continue; + } - // specular highlight texture - //if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ns) - { - token += 7; - ParseTextureNameAndOption(&(material.specular_highlight_texname), - &(material.specular_highlight_texopt), token, - /* is_bump */ false); - continue; - } + // specular highlight texture + //if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ns) + { + token += 7; + ParseTextureNameAndOption(&(material.specular_highlight_texname), + &(material.specular_highlight_texopt), token, + /* is_bump */ false); + continue; + } - // bump texture - //if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) - if (a_tok == TOK_map_bump) - { - token += 9; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // bump texture + //if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_bump) + { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // bump texture - //if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) - if (a_tok == TOK_map_Bump) - { - token += 9; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // bump texture + //if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_Bump) + { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // bump texture - //if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_bump) - { - token += 5; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // bump texture + //if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_bump) + { + token += 5; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // alpha texture - //if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) - if (a_tok == TOK_map_d) - { - token += 6; - material.alpha_texname = token; - ParseTextureNameAndOption(&(material.alpha_texname), - &(material.alpha_texopt), token, - /* is_bump */ false); - continue; - } + // alpha texture + //if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_map_d) + { + token += 6; + material.alpha_texname = token; + ParseTextureNameAndOption(&(material.alpha_texname), + &(material.alpha_texopt), token, + /* is_bump */ false); + continue; + } - // displacement texture - //if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_disp) - { - token += 5; - ParseTextureNameAndOption(&(material.displacement_texname), - &(material.displacement_texopt), token, - /* is_bump */ false); - continue; - } + // displacement texture + //if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_disp) + { + token += 5; + ParseTextureNameAndOption(&(material.displacement_texname), + &(material.displacement_texopt), token, + /* is_bump */ false); + continue; + } - // reflection map - //if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_refl) - { - token += 5; - ParseTextureNameAndOption(&(material.reflection_texname), - &(material.reflection_texopt), token, - /* is_bump */ false); - continue; - } + // reflection map + //if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_refl) + { + token += 5; + ParseTextureNameAndOption(&(material.reflection_texname), + &(material.reflection_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: roughness texture - //if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Pr) - { - token += 7; - ParseTextureNameAndOption(&(material.roughness_texname), - &(material.roughness_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: roughness texture + //if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pr) + { + token += 7; + ParseTextureNameAndOption(&(material.roughness_texname), + &(material.roughness_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: metallic texture - //if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Pm) - { - token += 7; - ParseTextureNameAndOption(&(material.metallic_texname), - &(material.metallic_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: metallic texture + //if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pm) + { + token += 7; + ParseTextureNameAndOption(&(material.metallic_texname), + &(material.metallic_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: sheen texture - //if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ps) - { - token += 7; - ParseTextureNameAndOption(&(material.sheen_texname), - &(material.sheen_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: sheen texture + //if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ps) + { + token += 7; + ParseTextureNameAndOption(&(material.sheen_texname), + &(material.sheen_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: emissive texture - //if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ke) - { - token += 7; - ParseTextureNameAndOption(&(material.emissive_texname), - &(material.emissive_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: emissive texture + //if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ke) + { + token += 7; + ParseTextureNameAndOption(&(material.emissive_texname), + &(material.emissive_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: normal map texture - //if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_norm) - { - token += 5; - ParseTextureNameAndOption( - &(material.normal_texname), &(material.normal_texopt), token, - /* is_bump */ false); // @fixme { is_bump will be true? } - continue; - } + // PBR: normal map texture + //if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_norm) + { + token += 5; + ParseTextureNameAndOption( + &(material.normal_texname), &(material.normal_texopt), token, + /* is_bump */ false); // @fixme { is_bump will be true? } + continue; + } + } // unknown parameter const char *_space = strchr(token, ' '); From aeb0f05c0e782d1c1c411afc50492c1517054edf Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 23:19:31 +0200 Subject: [PATCH 08/20] remove stringstream for simple string copy --- tiny_obj_loader.h | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 8ad9b75..1487fed 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1582,9 +1582,13 @@ void LoadMtl(std::map *material_map, // set new mtl name token += 7; { + /* std::stringstream sstr; sstr << token; material.name = sstr.str(); + */ + + material.name = std::string(token); } continue; } @@ -2097,9 +2101,14 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // @todo { multiple object name? } token += 2; + + /* std::stringstream ss; ss << token; name = ss.str(); + */ + + name = std::string(token); continue; } @@ -2145,9 +2154,13 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (a_tok==TOK_usemtl) { token += 7; + /* std::stringstream ss; ss << token; std::string namebuf = ss.str(); + */ + + std::string namebuf = std::string(token); int newMaterialId = -1; if (material_map.find(namebuf) != material_map.end()) { @@ -2398,6 +2411,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, // @todo { multiple object name? } token += 2; + /* std::stringstream ss; ss << token; std::string object_name = ss.str(); @@ -2405,6 +2419,11 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, if (callback.object_cb) { callback.object_cb(user_data, object_name.c_str()); } + */ + + if (callback.object_cb) { + callback.object_cb(user_data, token); + } continue; } @@ -2414,9 +2433,13 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, tag_t tag; token += 2; + /* std::stringstream ss; ss << token; tag.name = ss.str(); + */ + + tag.name = std::string(token); token += tag.name.size() + 1; @@ -2437,9 +2460,15 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, tag.stringValues.resize(static_cast(ts.num_strings)); for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { - std::stringstream ss; + + /* + std::stringstream ss; ss << token; tag.stringValues[i] = ss.str(); + */ + + tag.stringValues[i] = std::string(token); + token += tag.stringValues[i].size() + 1; } @@ -2461,9 +2490,14 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, if (a_tok==TOK_usemtl) { token += 7; + /* std::stringstream ss; ss << token; std::string namebuf = ss.str(); + */ + + std::string namebuf = std::string(token); + int newMaterialId = -1; if (material_map.find(namebuf) != material_map.end()) { From dc4c970262a14e970ae784499876085b5f1903bd Mon Sep 17 00:00:00 2001 From: tigrazone Date: Fri, 8 Dec 2017 23:39:07 +0200 Subject: [PATCH 09/20] small speedups - up to 1-3% --- tiny_obj_loader.h | 167 ++++++++++++++++++++++++++++------------------ 1 file changed, 101 insertions(+), 66 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 1487fed..a87c8c1 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1432,75 +1432,109 @@ void LoadMtl(std::map *material_map, if (token[0] == '\0') continue; // empty line if (token[0] == '#') continue; // comment line + + //group size==2 + if(IS_SPACE((token[2]))) + { + //group K + if (token[0] == 'K') + { + switch(token[1]) + { + case 'a': + // ambient + //if (token[1] == 'a') + { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.ambient[0] = r; + material.ambient[1] = g; + material.ambient[2] = b; + continue; + } + + // diffuse + case 'd': + //if (token[1] == 'd') + { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.diffuse[0] = r; + material.diffuse[1] = g; + material.diffuse[2] = b; + continue; + } + + // specular + case 's': + //if (token[1] == 's') + { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.specular[0] = r; + material.specular[1] = g; + material.specular[2] = b; + continue; + } - // ambient - if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.ambient[0] = r; - material.ambient[1] = g; - material.ambient[2] = b; - continue; - } + // transmittance + case 't': + //if (token[1] == 't') + { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.transmittance[0] = r; + material.transmittance[1] = g; + material.transmittance[2] = b; + continue; + } + + // emission + case 'e': + //if (token[1] == 'e') + { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.emission[0] = r; + material.emission[1] = g; + material.emission[2] = b; + continue; + } + } + + } - // diffuse - if (token[0] == 'K' && token[1] == 'd' && IS_SPACE((token[2]))) { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.diffuse[0] = r; - material.diffuse[1] = g; - material.diffuse[2] = b; - continue; - } + // transmittance + if ( //(token[0] == 'K' && token[1] == 't') || + token[0] == 'T' && token[1] == 'f') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.transmittance[0] = r; + material.transmittance[1] = g; + material.transmittance[2] = b; + continue; + } - // specular - if (token[0] == 'K' && token[1] == 's' && IS_SPACE((token[2]))) { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.specular[0] = r; - material.specular[1] = g; - material.specular[2] = b; - continue; - } + // ior(index of refraction) + if (token[0] == 'N' && token[1] == 'i') { + token += 2; + material.ior = parseReal(&token); + continue; + } - // transmittance - if ((token[0] == 'K' && token[1] == 't' && IS_SPACE((token[2]))) || - (token[0] == 'T' && token[1] == 'f' && IS_SPACE((token[2])))) { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.transmittance[0] = r; - material.transmittance[1] = g; - material.transmittance[2] = b; - continue; - } - - // ior(index of refraction) - if (token[0] == 'N' && token[1] == 'i' && IS_SPACE((token[2]))) { - token += 2; - material.ior = parseReal(&token); - continue; - } - - // emission - if (token[0] == 'K' && token[1] == 'e' && IS_SPACE(token[2])) { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.emission[0] = r; - material.emission[1] = g; - material.emission[2] = b; - continue; - } - - // shininess - if (token[0] == 'N' && token[1] == 's' && IS_SPACE(token[2])) { - token += 2; - material.shininess = parseReal(&token); - continue; + // shininess + if (token[0] == 'N' && token[1] == 's') { + token += 2; + material.shininess = parseReal(&token); + continue; + } + } // dissolve @@ -1569,7 +1603,8 @@ void LoadMtl(std::map *material_map, // flush previous material. if (!material.name.empty()) { material_map->insert(std::pair( - material.name, static_cast(materials->size()))); + material.name, static_cast(materials->size())) + ); materials->push_back(material); } From 3c0196bfb7344e7698fb824d592ef7d6812a3507 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sat, 9 Dec 2017 11:59:33 +0200 Subject: [PATCH 10/20] map vs unordered_map remake --- tiny_obj_loader.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index a87c8c1..666e5e9 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -363,9 +363,6 @@ void LoadMtl(std::map *material_map, #include #include -//tigra: unordered_map for hashed keywords -#include - #define TINYOBJLOADER_IMPLEMENTATION_BUFREAD @@ -561,7 +558,7 @@ enum tokens_enum { TOK_norm }; -std::unordered_map hashed_toks; +std::map hashed_toks; From 05f06d09d80bc3302d39281f8be9d9cf90c1e250 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sat, 9 Dec 2017 13:17:04 +0200 Subject: [PATCH 11/20] hashed tokens as keys of map. 5% speedup --- tiny_obj_loader.h | 82 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 13 deletions(-) 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; From af1bcf1e11fd7141b340cf30ad0a211ea2b99960 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 00:43:50 +0200 Subject: [PATCH 12/20] errors fix --- tests/tester.cc | 3 +- tiny_obj_loader.h | 382 +++++++++++++++++++++++----------------------- 2 files changed, 197 insertions(+), 188 deletions(-) diff --git a/tests/tester.cc b/tests/tester.cc index cd972f8..ca2bc3e 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -303,7 +303,8 @@ std::string matStream( virtual bool operator() ( const std::string& matId, std::vector* materials, - std::map* matMap, + //std::map* matMap, + std::map* matMap, std::string* err) { (void)matId; diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index e170c1d..fa461c2 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1165,10 +1165,7 @@ static bool ParseTextureNameAndOption(std::string *texname, token += strspn(token, " \t"); // skip space a_tok = token2tok(token); - - //tigra: minimize checks - if(a_tok>=TOK_blendu && a_tok<=TOK_mm) - { + //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) if (a_tok == TOK_blendu) { @@ -1243,7 +1240,6 @@ static bool ParseTextureNameAndOption(std::string *texname, token += 4; parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); } - } else { // Assume texture filename #if 0 @@ -1445,11 +1441,8 @@ std::map *material_map, //group K if (token[0] == 'K') { - switch(token[1]) - { - case 'a': // ambient - //if (token[1] == 'a') + if (token[1] == 'a') { token += 2; real_t r, g, b; @@ -1461,8 +1454,7 @@ std::map *material_map, } // diffuse - case 'd': - //if (token[1] == 'd') + if (token[1] == 'd') { token += 2; real_t r, g, b; @@ -1474,8 +1466,7 @@ std::map *material_map, } // specular - case 's': - //if (token[1] == 's') + if (token[1] == 's') { token += 2; real_t r, g, b; @@ -1487,8 +1478,7 @@ std::map *material_map, } // transmittance - case 't': - //if (token[1] == 't') + if (token[1] == 't') { token += 2; real_t r, g, b; @@ -1500,8 +1490,7 @@ std::map *material_map, } // emission - case 'e': - //if (token[1] == 'e') + if (token[1] == 'e') { token += 2; real_t r, g, b; @@ -1512,8 +1501,6 @@ std::map *material_map, continue; } } - - } // transmittance if ( //(token[0] == 'K' && token[1] == 't') || @@ -1540,7 +1527,60 @@ std::map *material_map, material.shininess = parseReal(&token); continue; } - + + + if (token[0] == 'T' && token[1] == 'r') { + token += 2; + if (has_d) { + // `d` wins. Ignore `Tr` value. + ss << "WARN: Both `d` and `Tr` parameters defined for \"" + << material.name << "\". Use the value of `d` for dissolve." + << std::endl; + } else { + // We invert value of Tr(assume Tr is in range [0, 1]) + // NOTE: Interpretation of Tr is application(exporter) dependent. For + // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43) + material.dissolve = 1.0f - parseReal(&token); + } + has_tr = true; + continue; + } + + //tigra: refactoring for new speedup release + if (token[0] == 'P') { + + // PBR: roughness + if(token[1] == 'r') + { + token += 2; + material.roughness = parseReal(&token); + continue; + } + else + // PBR: metallic + if(token[1] == 'm') + { + token += 2; + material.metallic = parseReal(&token); + continue; + } + else + // PBR: sheen + if(token[1] == 's') + { + token += 2; + material.sheen = parseReal(&token); + continue; + } + else + // PBR: clearcoat thickness + if(token[1] == 'c') + { + token += 2; + material.clearcoat_thickness = parseReal(&token); + continue; + } + } } // dissolve @@ -1555,43 +1595,6 @@ std::map *material_map, } has_d = true; continue; - } - if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) { - token += 2; - if (has_d) { - // `d` wins. Ignore `Tr` value. - ss << "WARN: Both `d` and `Tr` parameters defined for \"" - << material.name << "\". Use the value of `d` for dissolve." - << std::endl; - } else { - // We invert value of Tr(assume Tr is in range [0, 1]) - // NOTE: Interpretation of Tr is application(exporter) dependent. For - // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43) - material.dissolve = 1.0f - parseReal(&token); - } - has_tr = true; - continue; - } - - //tigra: refactoring for new speedup release - if (token[0] == 'P' && IS_SPACE(token[2])) { - token += 2; - - // PBR: roughness - if(token[1] == 'r') - material.roughness = parseReal(&token); - else - // PBR: metallic - if(token[1] == 'm') - material.metallic = parseReal(&token); - else - // PBR: sheen - if(token[1] == 's') - material.sheen = parseReal(&token); - else - // PBR: clearcoat thickness - if(token[1] == 'c') - material.clearcoat_thickness = parseReal(&token); } @@ -2043,166 +2046,171 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (token[0] == '#') continue; // comment line - // vertex - if (token[0] == 'v' && IS_SPACE((token[1]))) { - token += 2; - real_t x, y, z; - real_t r, g, b; - parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); - v.push_back(x); - v.push_back(y); - v.push_back(z); + if (IS_SPACE((token[1]))) { + // vertex + if (token[0] == 'v') { + token += 2; + real_t x, y, z; + real_t r, g, b; + parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); + v.push_back(x); + v.push_back(y); + v.push_back(z); - vc.push_back(r); - vc.push_back(g); - vc.push_back(b); - continue; - } + vc.push_back(r); + vc.push_back(g); + vc.push_back(b); + continue; + } - // normal - if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) { - token += 3; - real_t x, y, z; - parseReal3(&x, &y, &z, &token); - vn.push_back(x); - vn.push_back(y); - vn.push_back(z); - continue; - } + // face + if (token[0] == 'f') { + token += 2; + token += strspn(token, " \t"); - // texcoord - if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) { - token += 3; - real_t x, y; - parseReal2(&x, &y, &token); - vt.push_back(x); - vt.push_back(y); - continue; - } + std::vector face; + face.reserve(3); - // face - if (token[0] == 'f' && IS_SPACE((token[1]))) { - token += 2; - token += strspn(token, " \t"); + while (!IS_NEW_LINE(token[0])) { + vertex_index vi; + if (!parseTriple(&token, static_cast(v.size() / 3), + static_cast(vn.size() / 3), + static_cast(vt.size() / 2), &vi)) { + if (err) { + (*err) = "Failed parse `f' line(e.g. zero value for face index).\n"; + } + return false; + } - std::vector face; - face.reserve(3); + face.push_back(vi); + size_t n = strspn(token, " \t\r"); + token += n; + } - while (!IS_NEW_LINE(token[0])) { - vertex_index vi; - if (!parseTriple(&token, static_cast(v.size() / 3), - static_cast(vn.size() / 3), - static_cast(vt.size() / 2), &vi)) { - if (err) { - (*err) = "Failed parse `f' line(e.g. zero value for face index).\n"; - } - return false; - } + // replace with emplace_back + std::move on C++11 + faceGroup.push_back(std::vector()); + faceGroup[faceGroup.size() - 1].swap(face); - face.push_back(vi); - size_t n = strspn(token, " \t\r"); - token += n; - } + continue; + } - // replace with emplace_back + std::move on C++11 - faceGroup.push_back(std::vector()); - faceGroup[faceGroup.size() - 1].swap(face); + // group name + if (token[0] == 'g') { + // flush previous face group. + bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + triangulate); + (void)ret; // return value not used. - continue; - } + if (shape.mesh.indices.size() > 0) { + shapes->push_back(shape); + } - // group name - if (token[0] == 'g' && IS_SPACE((token[1]))) { - // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, - triangulate); - (void)ret; // return value not used. + shape = shape_t(); - if (shape.mesh.indices.size() > 0) { - shapes->push_back(shape); - } + // material = -1; + faceGroup.clear(); - shape = shape_t(); + std::vector names; + names.reserve(2); - // material = -1; - faceGroup.clear(); + while (!IS_NEW_LINE(token[0])) { + std::string str = parseString(&token); + names.push_back(str); + token += strspn(token, " \t\r"); // skip tag + } - std::vector names; - names.reserve(2); + assert(names.size() > 0); - while (!IS_NEW_LINE(token[0])) { - std::string str = parseString(&token); - names.push_back(str); - token += strspn(token, " \t\r"); // skip tag - } + // names[0] must be 'g', so skip the 0th element. + if (names.size() > 1) { + name = names[1]; + } else { + name = ""; + } - assert(names.size() > 0); + continue; + } - // names[0] must be 'g', so skip the 0th element. - if (names.size() > 1) { - name = names[1]; - } else { - name = ""; - } + // object name + if (token[0] == 'o') { + // flush previous face group. + bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, + triangulate); + if (ret) { + shapes->push_back(shape); + } - continue; - } + // material = -1; + faceGroup.clear(); + shape = shape_t(); - // object name - if (token[0] == 'o' && IS_SPACE((token[1]))) { - // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, - triangulate); - if (ret) { - shapes->push_back(shape); - } + // @todo { multiple object name? } + token += 2; + + /* + std::stringstream ss; + ss << token; + name = ss.str(); + */ + + name = std::string(token); - // material = -1; - faceGroup.clear(); - shape = shape_t(); + continue; + } - // @todo { multiple object name? } - token += 2; - - /* - std::stringstream ss; - ss << token; - name = ss.str(); - */ - - name = std::string(token); + if (token[0] == 't') { + tag_t tag; - continue; - } + token += 2; - if (token[0] == 't' && IS_SPACE(token[1])) { - tag_t tag; + tag.name = parseString(&token); - token += 2; + tag_sizes ts = parseTagTriple(&token); - tag.name = parseString(&token); + tag.intValues.resize(static_cast(ts.num_ints)); - tag_sizes ts = parseTagTriple(&token); + for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { + tag.intValues[i] = parseInt(&token); + } - tag.intValues.resize(static_cast(ts.num_ints)); + tag.floatValues.resize(static_cast(ts.num_reals)); + for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { + tag.floatValues[i] = parseReal(&token); + } - for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { - tag.intValues[i] = parseInt(&token); - } + tag.stringValues.resize(static_cast(ts.num_strings)); + for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { + tag.stringValues[i] = parseString(&token); + } - tag.floatValues.resize(static_cast(ts.num_reals)); - for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { - tag.floatValues[i] = parseReal(&token); - } - - tag.stringValues.resize(static_cast(ts.num_strings)); - for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { - tag.stringValues[i] = parseString(&token); - } - - tags.push_back(tag); - } + tags.push_back(tag); + } + } + + + if (token[0] == 'v' && IS_SPACE((token[2]))) { + // normal + if (token[1] == 'n') { + token += 3; + real_t x, y, z; + parseReal3(&x, &y, &z, &token); + vn.push_back(x); + vn.push_back(y); + vn.push_back(z); + continue; + } + + // texcoord + if (token[1] == 't') { + token += 3; + real_t x, y; + parseReal2(&x, &y, &token); + vt.push_back(x); + vt.push_back(y); + continue; + } + } //tigra: refactoring for new speedup release //tigra: compares one more start From 4fe479453bad2c0053ee0507529f0b4934c59be7 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 09:34:10 +0200 Subject: [PATCH 13/20] smallish optimize --- tiny_obj_loader.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index fa461c2..7f52577 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -1165,7 +1165,11 @@ static bool ParseTextureNameAndOption(std::string *texname, token += strspn(token, " \t"); // skip space a_tok = token2tok(token); - + + + //tigra: minimize checks + if(a_tok>=TOK_blendu && a_tok<=TOK_mm) + { //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) if (a_tok == TOK_blendu) { @@ -1240,6 +1244,8 @@ static bool ParseTextureNameAndOption(std::string *texname, token += 4; parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); } + + } else { // Assume texture filename #if 0 From a6c1d07560f6bf5e1fe9036bbdf340d6b795dce3 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 10:43:24 +0200 Subject: [PATCH 14/20] comment for export data and functions block with hashed keywords to other projects --- tiny_obj_loader.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 7f52577..d2ec096 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -457,8 +457,12 @@ int fd_; uint64_t fsize_,pos_; size_t buf_sz; }; -#endif - +#endif + + + +//*************************************************** +//*************** tigro keywords hash functions begin //tigra: x31 hash function @@ -616,6 +620,9 @@ int token2tok(const char* token) return a_tok; } +//*************** tigro keywords hash functions END +//************************************************* + MaterialReader::~MaterialReader() {} From 54851f8ac79912530c155c72e9c5453b67050b9b Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 10:46:08 +0200 Subject: [PATCH 15/20] comment for export data and functions block with hashed keywords to other projects --- tiny_obj_loader.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index d2ec096..2c9e472 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -569,12 +569,11 @@ enum tokens_enum { TOK_norm }; + std::map hashed_toks; - - - +//functions! void initHashedTokensMap() { //init hashed tokens map @@ -594,6 +593,7 @@ void initHashedTokensMap() } +//search token in keywords hash map int token2tok(const char* token) { From 0511658e86b23977478b77b9584e299a15977d38 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 10:46:35 +0200 Subject: [PATCH 16/20] comment for export data and functions block with hashed keywords to other projects --- deploy.bat | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deploy.bat b/deploy.bat index 8161f4f..62e58a1 100644 --- a/deploy.bat +++ b/deploy.bat @@ -1,3 +1,5 @@ +copy tiny_obj_loader.h ../tinyobjloader/ +cd ../tinyobjloader/ git add * git commit -m %1 -git push origin master \ No newline at end of file +git push origin master -u tigrazone \ No newline at end of file From c5976f931b34de8621d944c31aabb1731e6cf032 Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 10:47:32 +0200 Subject: [PATCH 17/20] comment for export data and functions block with hashed keywords to other projects --- deploy.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.bat b/deploy.bat index 62e58a1..76d7ed0 100644 --- a/deploy.bat +++ b/deploy.bat @@ -2,4 +2,4 @@ copy tiny_obj_loader.h ../tinyobjloader/ cd ../tinyobjloader/ git add * git commit -m %1 -git push origin master -u tigrazone \ No newline at end of file +git push -u tigrazone origin master \ No newline at end of file From dc542d663873c99447ff5321bd7ab4264bba7eeb Mon Sep 17 00:00:00 2001 From: tigrazone Date: Sun, 10 Dec 2017 10:48:05 +0200 Subject: [PATCH 18/20] comment for export data and functions block with hashed keywords to other projects --- deploy.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deploy.bat b/deploy.bat index 76d7ed0..793d002 100644 --- a/deploy.bat +++ b/deploy.bat @@ -2,4 +2,4 @@ copy tiny_obj_loader.h ../tinyobjloader/ cd ../tinyobjloader/ git add * git commit -m %1 -git push -u tigrazone origin master \ No newline at end of file +git push origin master \ No newline at end of file From 583590767ec720bef775407f0acaceeb67252f8b Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Tue, 12 Dec 2017 17:30:59 +0900 Subject: [PATCH 19/20] Fix compilation. Remove unused file. --- deploy.bat | 5 --- loader_example.cc | 2 +- tiny_obj_loader.h | 79 +++++++++++++++++++++++++++-------------------- 3 files changed, 47 insertions(+), 39 deletions(-) delete mode 100644 deploy.bat diff --git a/deploy.bat b/deploy.bat deleted file mode 100644 index 793d002..0000000 --- a/deploy.bat +++ /dev/null @@ -1,5 +0,0 @@ -copy tiny_obj_loader.h ../tinyobjloader/ -cd ../tinyobjloader/ -git add * -git commit -m %1 -git push origin master \ No newline at end of file diff --git a/loader_example.cc b/loader_example.cc index 203fbf8..d92be18 100644 --- a/loader_example.cc +++ b/loader_example.cc @@ -361,7 +361,7 @@ static bool TestStreamLoadObj() { virtual ~MaterialStringStreamReader() {} virtual bool operator()(const std::string& matId, std::vector* materials, - std::map* matMap, + std::map* matMap, std::string* err) { (void)matId; std::string warning; diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 2c9e472..d091bd4 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -278,7 +278,7 @@ 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; }; @@ -290,7 +290,7 @@ class MaterialFileReader : public MaterialReader { virtual bool operator()(const std::string &matId, std::vector *materials, //std::map *matMap, - std::map *matMap, + std::map *matMap, std::string *err); private: @@ -305,7 +305,7 @@ class MaterialStreamReader : public MaterialReader { virtual bool operator()(const std::string &matId, std::vector *materials, //std::map *matMap, - std::map *matMap, + std::map *matMap, std::string *err); private: @@ -350,7 +350,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, /// Loads materials into std::map void LoadMtl( //std::map *material_map, -std::map *material_map, +std::map *material_map, std::vector *materials, std::istream *inStream, std::string *warning); @@ -371,7 +371,7 @@ std::map *material_map, #include -#define TINYOBJLOADER_IMPLEMENTATION_BUFREAD +//#define TINYOBJLOADER_IMPLEMENTATION_BUFREAD @@ -466,27 +466,27 @@ size_t buf_sz; //tigra: x31 hash function -typedef uint32_t khint_t; +typedef unsigned int khint_t; -inline uint32_t X31_hash_string(const char *s) +inline unsigned int X31_hash_string(const char *s) { - khint_t h = *s; - for (++s ; *s; ++s) h = (h << 5) - h + *s; + khint_t h = static_cast(*s); + for (++s ; *s; ++s) h = (h << 5) - h + static_cast(*s); return h; } -inline uint32_t X31_hash_stringSZ(const char *s, int sz) +inline unsigned int X31_hash_stringSZ(const char *s, int sz) { int i; - khint_t h = *s; + khint_t h = static_cast(*s); - for (++s, i = sz-1 ; i && *s; ++s, i--) h = (h << 5) - h + *s; + for (++s, i = sz-1 ; i && *s; ++s, i--) h = (h << 5) - h + static_cast(*s); return h; } //tigra: refactoring2 - add list of keywords -static char * keywords[] = { +static const char * keywords[] = { //on off "on","off", @@ -570,15 +570,27 @@ enum tokens_enum { }; -std::map hashed_toks; +// TODO(syoyo): Do not define in global scope. + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wglobal-constructors" +#pragma clang diagnostic ignored "-Wexit-time-destructors" +#endif + +static std::map hashed_toks; + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif //functions! -void initHashedTokensMap() +static void initHashedTokensMap() { //init hashed tokens map - uint32_t hhh; + unsigned int hhh; int iii; if(hashed_toks.empty()) @@ -594,23 +606,24 @@ void initHashedTokensMap() //search token in keywords hash map -int token2tok(const char* token) +static int token2tok(const char* token) { - uint32_t token_sz, a_hash; + unsigned int token_sz, a_hash; int a_tok; - token_sz = strpbrk(token, " \t\r") - token; // token length + token_sz = static_cast(strpbrk(token, " \t\r") - token); // token length if(token_sz<1) //delimiter not found, token_sz = strlen(token) { //token_sz=strlen(token); a_hash = X31_hash_string(token); } - else - a_hash = X31_hash_stringSZ(token, token_sz); + else { + a_hash = X31_hash_stringSZ(token, static_cast(token_sz)); + } a_tok = -1; @@ -1395,7 +1408,7 @@ static void SplitString(const std::string &s, char delim, void LoadMtl( //std::map *material_map, -std::map *material_map, +std::map *material_map, std::vector *materials, std::istream *inStream, std::string *warning) { // Create a default material anyway. @@ -1631,9 +1644,9 @@ std::map *material_map, */ - uint32_t hsh1 = X31_hash_string(material.name.c_str()); + unsigned int hsh1 = X31_hash_string(material.name.c_str()); - material_map->insert(std::pair( + material_map->insert(std::pair( hsh1, static_cast(materials->size())) ); materials->push_back(material); @@ -1878,7 +1891,7 @@ std::map *material_map, } } - uint32_t hsh1 = X31_hash_string(material.name.c_str()); + unsigned int hsh1 = X31_hash_string(material.name.c_str()); // flush last material. /* @@ -1886,7 +1899,7 @@ std::map *material_map, material.name, static_cast(materials->size()))); */ - material_map->insert(std::pair( + material_map->insert(std::pair( hsh1, static_cast(materials->size()))); materials->push_back(material); @@ -1898,7 +1911,7 @@ 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; @@ -1940,7 +1953,7 @@ 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) { @@ -2024,9 +2037,9 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, // material //std::map material_map; - //tigra: key of material_map is uint32_t now + //tigra: key of material_map is unsigned int now //because std::map is an red-black trees it is better to store key as int - std::map material_map; + std::map material_map; int material = -1; shape_t shape; @@ -2256,7 +2269,7 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, - uint32_t hsh = X31_hash_string(token); + unsigned int hsh = X31_hash_string(token); int newMaterialId = -1; if (material_map.find(hsh) != material_map.end()) { @@ -2367,7 +2380,7 @@ 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; @@ -2605,7 +2618,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, */ //make a hash from token - uint32_t hsh = X31_hash_string(token); + unsigned int hsh = X31_hash_string(token); int newMaterialId = -1; if (material_map.find(hsh) != material_map.end()) { From 160d6be10ffbc2468a505f8b39cd7deb33df8180 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Tue, 12 Dec 2017 17:31:22 +0900 Subject: [PATCH 20/20] Format code. --- tiny_obj_loader.h | 1941 ++++++++++++++++++++++----------------------- 1 file changed, 928 insertions(+), 1013 deletions(-) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index d091bd4..955e0b3 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -277,7 +277,7 @@ 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; }; @@ -289,9 +289,9 @@ class MaterialFileReader : public MaterialReader { virtual ~MaterialFileReader() {} virtual bool operator()(const std::string &matId, std::vector *materials, - //std::map *matMap, + // std::map *matMap, std::map *matMap, - std::string *err); + std::string *err); private: std::string m_mtlBaseDir; @@ -304,9 +304,9 @@ class MaterialStreamReader : public MaterialReader { virtual ~MaterialStreamReader() {} virtual bool operator()(const std::string &matId, std::vector *materials, - //std::map *matMap, - std::map *matMap, - std::string *err); + // std::map *matMap, + std::map *matMap, + std::string *err); private: std::istream &m_inStream; @@ -349,10 +349,10 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, /// Loads materials into std::map void LoadMtl( -//std::map *material_map, -std::map *material_map, - std::vector *materials, std::istream *inStream, - std::string *warning); + // std::map *material_map, + std::map *material_map, + std::vector *materials, std::istream *inStream, + std::string *warning); } // namespace tinyobj @@ -370,206 +370,221 @@ std::map *material_map, #include #include - //#define TINYOBJLOADER_IMPLEMENTATION_BUFREAD - - #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD #include - #include - #include - #include - - +#include +#include +#include + #define O_LARGEFILE 0100000 #endif namespace tinyobj { - - + #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD -//tigra: ImportInBuf - buffered input file -class ImportInBuf: public std::streambuf -{ -public: +// tigra: ImportInBuf - buffered input file +class ImportInBuf : public std::streambuf { + public: + ImportInBuf(const char *filename, size_t buf_sz_ = 64 * 1024) + : fd_(open(filename, O_RDONLY | O_LARGEFILE)) { + // fprintf(stderr, "ImportInBuf(%s\n", filename); -ImportInBuf(const char* filename, size_t buf_sz_=64*1024) -:fd_(open(filename,O_RDONLY | O_LARGEFILE)) -{ - //fprintf(stderr, "ImportInBuf(%s\n", filename); - -if(fd_<0) -{ - fprintf(stderr, "can't open file %s\n",filename); - exit(1); -} + if (fd_ < 0) { + fprintf(stderr, "can't open file %s\n", filename); + exit(1); + } -buf_sz = buf_sz_; + buf_sz = buf_sz_; -//fprintf(stderr, "buf_sz=%d\n", buf_sz); + // fprintf(stderr, "buf_sz=%d\n", buf_sz); -buffer_ = (char*) malloc(buf_sz); + buffer_ = (char *)malloc(buf_sz); -setg(buffer_,buffer_,buffer_); -struct stat st; -fstat(fd_,&st); -fsize_=st.st_size; + setg(buffer_, buffer_, buffer_); + struct stat st; + fstat(fd_, &st); + fsize_ = st.st_size; -//fprintf(stderr, "file size: %ld\n", fsize_); -} + // fprintf(stderr, "file size: %ld\n", fsize_); + } -// you don't have to do it like this if your streams are 64 bit -void seekg(uint64_t pos) -{ -lseek(fd_,pos,SEEK_SET); -pos_=pos; -setg(buffer_,buffer_,buffer_); -} + // you don't have to do it like this if your streams are 64 bit + void seekg(uint64_t pos) { + lseek(fd_, pos, SEEK_SET); + pos_ = pos; + setg(buffer_, buffer_, buffer_); + } -uint64_t tellg()const { return pos_; } -uint64_t size()const { return fsize_; } + uint64_t tellg() const { return pos_; } + uint64_t size() const { return fsize_; } -~ImportInBuf(){ close(fd_); free(buffer_); } + ~ImportInBuf() { + close(fd_); + free(buffer_); + } -private: -ImportInBuf(const ImportInBuf&); -ImportInBuf& operator=(const ImportInBuf&); -virtual int underflow() -{ -if(gptr()(*s); - for (++s ; *s; ++s) h = (h << 5) - h + static_cast(*s); - return h; +inline unsigned int X31_hash_string(const char *s) { + khint_t h = static_cast(*s); + for (++s; *s; ++s) h = (h << 5) - h + static_cast(*s); + return h; } -inline unsigned int X31_hash_stringSZ(const char *s, int sz) -{ - int i; - khint_t h = static_cast(*s); - - for (++s, i = sz-1 ; i && *s; ++s, i--) h = (h << 5) - h + static_cast(*s); - return h; +inline unsigned int X31_hash_stringSZ(const char *s, int sz) { + int i; + khint_t h = static_cast(*s); + + for (++s, i = sz - 1; i && *s; ++s, i--) + h = (h << 5) - h + static_cast(*s); + return h; } - -//tigra: refactoring2 - add list of keywords -static const char * keywords[] = { - //on off - "on","off", - - //TextureType - "cube_top", "cube_bottom", "cube_left", "cube_right", "cube_front", "cube_back", "sphere", - - //TextureNameAndOption - "-blendu", "-blendv", "-clamp", "-boost", "-bm", "-o", "-s", "-t", "-type", "-imfchan", "-mm", - - //newmtl, illum, mtllib, usemtl - "newmtl", "illum", "mtllib", "usemtl", +// tigra: refactoring2 - add list of keywords +static const char *keywords[] = { + // on off + "on", "off", - //PBR params - "Pcr", "aniso", "anisor", - - //maps - "map_Ka", "map_Kd", "map_Ks", "map_Ns", "map_bump", "map_Bump", "bump", - - // alpha texture - "map_d", + // TextureType + "cube_top", "cube_bottom", "cube_left", "cube_right", "cube_front", + "cube_back", "sphere", + + // TextureNameAndOption + "-blendu", "-blendv", "-clamp", "-boost", "-bm", "-o", "-s", "-t", "-type", + "-imfchan", "-mm", + + // newmtl, illum, mtllib, usemtl + "newmtl", "illum", "mtllib", "usemtl", + + // PBR params + "Pcr", "aniso", "anisor", + + // maps + "map_Ka", "map_Kd", "map_Ks", "map_Ns", "map_bump", "map_Bump", "bump", + + // alpha texture + "map_d", // displacement texture - "disp", + "disp", // reflection map - "refl", + "refl", // PBR: roughness texture, metallic texture - "map_Pr", "map_Pm", + "map_Pr", "map_Pm", // PBR: sheen texture "map_Ps", // PBR: emissive texture - "map_Ke", + "map_Ke", // PBR: normal map texture - "norm" -}; + "norm"}; -//tigra: enum of tokens +// tigra: enum of tokens enum tokens_enum { - //on off - TOK_on, TOK_off, - - //TextureType - TOK_cube_top, TOK_cube_bottom, TOK_cube_left, TOK_cube_right, TOK_cube_front, TOK_cube_back, TOK_sphere, - - //TextureNameAndOption - TOK_blendu, TOK_blendv, TOK_clamp, TOK_boost, TOK_bm, TOK_o, TOK_s, TOK_t, TOK_type, TOK_imfchan, TOK_mm, - - //newmtl, illum, mtllib, usemtl - TOK_newmtl, TOK_illum, TOK_mtllib, TOK_usemtl, + // on off + TOK_on, + TOK_off, - //PBR params - TOK_Pcr, TOK_aniso, TOK_anisor, - - //maps - TOK_map_Ka, TOK_map_Kd, TOK_map_Ks, TOK_map_Ns, TOK_map_bump, TOK_map_Bump, TOK_bump, - - // alpha texture - TOK_map_d, + // TextureType + TOK_cube_top, + TOK_cube_bottom, + TOK_cube_left, + TOK_cube_right, + TOK_cube_front, + TOK_cube_back, + TOK_sphere, - // displacement texture - TOK_disp, + // TextureNameAndOption + TOK_blendu, + TOK_blendv, + TOK_clamp, + TOK_boost, + TOK_bm, + TOK_o, + TOK_s, + TOK_t, + TOK_type, + TOK_imfchan, + TOK_mm, - // reflection map - TOK_refl, + // newmtl, illum, mtllib, usemtl + TOK_newmtl, + TOK_illum, + TOK_mtllib, + TOK_usemtl, - // PBR: roughness texture, metallic texture - TOK_map_Pr, TOK_map_Pm, + // PBR params + TOK_Pcr, + TOK_aniso, + TOK_anisor, - // PBR: sheen texture - TOK_map_Ps, + // maps + TOK_map_Ka, + TOK_map_Kd, + TOK_map_Ks, + TOK_map_Ns, + TOK_map_bump, + TOK_map_Bump, + TOK_bump, - // PBR: emissive texture - TOK_map_Ke, + // alpha texture + TOK_map_d, - // PBR: normal map texture - TOK_norm + // displacement texture + TOK_disp, + + // reflection map + TOK_refl, + + // PBR: roughness texture, metallic texture + TOK_map_Pr, + TOK_map_Pm, + + // PBR: sheen texture + TOK_map_Ps, + + // PBR: emissive texture + TOK_map_Ke, + + // PBR: normal map texture + TOK_norm }; - // TODO(syoyo): Do not define in global scope. #ifdef __clang__ @@ -578,66 +593,55 @@ enum tokens_enum { #pragma clang diagnostic ignored "-Wexit-time-destructors" #endif -static std::map hashed_toks; +static std::map hashed_toks; #ifdef __clang__ #pragma clang diagnostic pop #endif +// functions! +static void initHashedTokensMap() { + // init hashed tokens map -//functions! -static void initHashedTokensMap() -{ - //init hashed tokens map - unsigned int hhh; int iii; - - if(hashed_toks.empty()) - { - for(iii=sizeof(keywords)/sizeof(char*);iii;iii--) - { - hhh = X31_hash_string(keywords[iii-1]); - hashed_toks[hhh] = iii-1; - } + + if (hashed_toks.empty()) { + for (iii = sizeof(keywords) / sizeof(char *); iii; iii--) { + hhh = X31_hash_string(keywords[iii - 1]); + hashed_toks[hhh] = iii - 1; + } } - //init hashed tokens map END -} - - -//search token in keywords hash map -static int token2tok(const char* token) -{ + // init hashed tokens map END +} +// search token in keywords hash map +static int token2tok(const char *token) { unsigned int token_sz, a_hash; - - int a_tok; - - token_sz = static_cast(strpbrk(token, " \t\r") - token); // token length - - if(token_sz<1) //delimiter not found, token_sz = strlen(token) - { - //token_sz=strlen(token); - a_hash = X31_hash_string(token); - } - else { - a_hash = X31_hash_stringSZ(token, static_cast(token_sz)); + int a_tok; + + token_sz = static_cast(strpbrk(token, " \t\r") - + token); // token length + + if (token_sz < 1) // delimiter not found, token_sz = strlen(token) + { + // token_sz=strlen(token); + a_hash = X31_hash_string(token); + } else { + a_hash = X31_hash_stringSZ(token, static_cast(token_sz)); } - - - a_tok = -1; - if(hashed_toks.find(a_hash) != hashed_toks.end()) - a_tok = hashed_toks[a_hash]; - - return a_tok; + + a_tok = -1; + if (hashed_toks.find(a_hash) != hashed_toks.end()) + a_tok = hashed_toks[a_hash]; + + return a_tok; } //*************** tigro keywords hash functions END //************************************************* - - MaterialReader::~MaterialReader() {} struct vertex_index { @@ -935,11 +939,12 @@ static inline void parseV(real_t *x, real_t *y, real_t *z, real_t *w, } // Extension: parse vertex with colors(6 items) -static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z, real_t *r, - real_t *g, real_t *b, - const char **token, const double default_x = 0.0, - const double default_y = 0.0, - const double default_z = 0.0) { +static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z, + real_t *r, real_t *g, real_t *b, + const char **token, + const double default_x = 0.0, + const double default_y = 0.0, + const double default_z = 0.0) { (*x) = parseReal(token, default_x); (*y) = parseReal(token, default_y); (*z) = parseReal(token, default_z); @@ -970,59 +975,47 @@ static inline texture_type_t parseTextureType( const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) { (*token) += strspn((*token), " \t"); const char *end = (*token) + strcspn((*token), " \t\r"); - - texture_type_t ty = default_value; - - - - int a_tok; - - //init hashed tokens map - initHashedTokensMap(); - - a_tok = token2tok(*token); - - //tigra: dont check if wrong token - if(a_tok>=TOK_cube_top && a_tok<=TOK_sphere) - { - //if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) - if (a_tok == TOK_cube_top) - { - ty = TEXTURE_TYPE_CUBE_TOP; - } else - //if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) - if (a_tok == TOK_cube_bottom) - { - ty = TEXTURE_TYPE_CUBE_BOTTOM; - } else - //if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) - if (a_tok == TOK_cube_left) - { - ty = TEXTURE_TYPE_CUBE_LEFT; - } else - //if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) - if (a_tok == TOK_cube_right) - { - ty = TEXTURE_TYPE_CUBE_RIGHT; - } else - //if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) - if (a_tok == TOK_cube_front) - { - ty = TEXTURE_TYPE_CUBE_FRONT; - } else - //if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) - if (a_tok == TOK_cube_back) - { - ty = TEXTURE_TYPE_CUBE_BACK; - } else - //if ((0 == strncmp((*token), "sphere", strlen("sphere")))) - if (a_tok == TOK_sphere) - { - ty = TEXTURE_TYPE_SPHERE; - } - - } + texture_type_t ty = default_value; + + int a_tok; + + // init hashed tokens map + initHashedTokensMap(); + + a_tok = token2tok(*token); + + // tigra: dont check if wrong token + if (a_tok >= TOK_cube_top && a_tok <= TOK_sphere) { + // if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) + if (a_tok == TOK_cube_top) { + ty = TEXTURE_TYPE_CUBE_TOP; + } else + // if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) + if (a_tok == TOK_cube_bottom) { + ty = TEXTURE_TYPE_CUBE_BOTTOM; + } else + // if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) + if (a_tok == TOK_cube_left) { + ty = TEXTURE_TYPE_CUBE_LEFT; + } else + // if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) + if (a_tok == TOK_cube_right) { + ty = TEXTURE_TYPE_CUBE_RIGHT; + } else + // if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) + if (a_tok == TOK_cube_front) { + ty = TEXTURE_TYPE_CUBE_FRONT; + } else + // if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) + if (a_tok == TOK_cube_back) { + ty = TEXTURE_TYPE_CUBE_BACK; + } else + // if ((0 == strncmp((*token), "sphere", strlen("sphere")))) + if (a_tok == TOK_sphere) { + ty = TEXTURE_TYPE_SPHERE; + } + } (*token) = end; return ty; @@ -1038,7 +1031,7 @@ static tag_sizes parseTagTriple(const char **token) { return ts; } - (*token)++; // Skip '/' + (*token)++; // Skip '/' (*token) += strspn((*token), " \t"); ts.num_reals = atoi((*token)); @@ -1046,7 +1039,7 @@ static tag_sizes parseTagTriple(const char **token) { if ((*token)[0] != '/') { return ts; } - (*token)++; // Skip '/' + (*token)++; // Skip '/' ts.num_strings = parseInt(token); @@ -1172,102 +1165,86 @@ static bool ParseTextureNameAndOption(std::string *texname, texopt->type = TEXTURE_TYPE_NONE; const char *token = linebuf; // Assume line ends with NULL - - + int a_tok; - - //init hashed tokens map + + // init hashed tokens map initHashedTokensMap(); - - while (!IS_NEW_LINE((*token))) { token += strspn(token, " \t"); // skip space - - a_tok = token2tok(token); - - - //tigra: minimize checks - if(a_tok>=TOK_blendu && a_tok<=TOK_mm) - { - //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) - if (a_tok == TOK_blendu) - { - token += 8; - texopt->blendu = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) - if (a_tok == TOK_blendv) - { - token += 8; - texopt->blendv = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_clamp) - { - token += 7; - texopt->clamp = parseOnOff(&token, /* default */ true); - } else - //if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_boost) - { - token += 7; - texopt->sharpness = parseReal(&token, 1.0); - } else - //if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) - if (a_tok == TOK_bm) - { - token += 4; - texopt->bump_multiplier = parseReal(&token, 1.0); - } else - //if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_o) - { - token += 3; - parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), - &(texopt->origin_offset[2]), &token); - } else - //if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_s) - { - token += 3; - parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), - &token, 1.0, 1.0, 1.0); - } else - //if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) - if (a_tok == TOK_t) - { - token += 3; - parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), - &(texopt->turbulence[2]), &token); - } else - //if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) - if (a_tok == TOK_type) - { - token += 5; - texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); - } else - //if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) - if (a_tok == TOK_imfchan) - { - token += 9; - token += strspn(token, " \t"); - const char *end = token + strcspn(token, " \t\r"); - if ((end - token) == 1) { // Assume one char for -imfchan - texopt->imfchan = (*token); - } - token = end; - } else - //if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) - if (a_tok == TOK_mm) - { - token += 4; - parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); - } - - } - else { - // Assume texture filename + + a_tok = token2tok(token); + + // tigra: minimize checks + if (a_tok >= TOK_blendu && a_tok <= TOK_mm) { + // if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendu) { + token += 8; + texopt->blendu = parseOnOff(&token, /* default */ true); + } else + // if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) + if (a_tok == TOK_blendv) { + token += 8; + texopt->blendv = parseOnOff(&token, /* default */ true); + } else + // if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_clamp) { + token += 7; + texopt->clamp = parseOnOff(&token, /* default */ true); + } else + // if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_boost) { + token += 7; + texopt->sharpness = parseReal(&token, 1.0); + } else + // if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_bm) { + token += 4; + texopt->bump_multiplier = parseReal(&token, 1.0); + } else + // if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_o) { + token += 3; + parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), + &(texopt->origin_offset[2]), &token); + } else + // if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_s) { + token += 3; + parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), + &(texopt->scale[2]), &token, 1.0, 1.0, 1.0); + } else + // if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) + if (a_tok == TOK_t) { + token += 3; + parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), + &(texopt->turbulence[2]), &token); + } else + // if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) + if (a_tok == TOK_type) { + token += 5; + texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); + } else + // if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) + if (a_tok == TOK_imfchan) { + token += 9; + token += strspn(token, " \t"); + const char *end = token + strcspn(token, " \t\r"); + if ((end - token) == 1) { // Assume one char for -imfchan + texopt->imfchan = (*token); + } + token = end; + } else + // if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) + if (a_tok == TOK_mm) { + token += 4; + parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, + 1.0); + } + + } else { +// Assume texture filename #if 0 size_t len = strcspn(token, " \t\r"); // untile next space texture_name = std::string(token, token + len); @@ -1407,24 +1384,22 @@ static void SplitString(const std::string &s, char delim, } void LoadMtl( -//std::map *material_map, -std::map *material_map, - std::vector *materials, std::istream *inStream, - std::string *warning) { + // std::map *material_map, + std::map *material_map, + std::vector *materials, std::istream *inStream, + std::string *warning) { // Create a default material anyway. material_t material; InitMaterial(&material); // Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification. bool has_d = false; - bool has_tr = false; - + bool has_tr = false; + int a_tok; - - //init hashed tokens map + + // init hashed tokens map initHashedTokensMap(); - - std::stringstream ss; @@ -1460,153 +1435,137 @@ std::map *material_map, if (token[0] == '\0') continue; // empty line if (token[0] == '#') continue; // comment line - - //group size==2 - if(IS_SPACE((token[2]))) - { - //group K - if (token[0] == 'K') - { - // ambient - if (token[1] == 'a') - { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.ambient[0] = r; - material.ambient[1] = g; - material.ambient[2] = b; - continue; - } - - // diffuse - if (token[1] == 'd') - { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.diffuse[0] = r; - material.diffuse[1] = g; - material.diffuse[2] = b; - continue; - } - - // specular - if (token[1] == 's') - { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.specular[0] = r; - material.specular[1] = g; - material.specular[2] = b; - continue; - } - // transmittance - if (token[1] == 't') - { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.transmittance[0] = r; - material.transmittance[1] = g; - material.transmittance[2] = b; - continue; - } - - // emission - if (token[1] == 'e') - { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.emission[0] = r; - material.emission[1] = g; - material.emission[2] = b; - continue; - } - } + // group size==2 + if (IS_SPACE((token[2]))) { + // group K + if (token[0] == 'K') { + // ambient + if (token[1] == 'a') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.ambient[0] = r; + material.ambient[1] = g; + material.ambient[2] = b; + continue; + } - // transmittance - if ( //(token[0] == 'K' && token[1] == 't') || - token[0] == 'T' && token[1] == 'f') { - token += 2; - real_t r, g, b; - parseReal3(&r, &g, &b, &token); - material.transmittance[0] = r; - material.transmittance[1] = g; - material.transmittance[2] = b; - continue; - } + // diffuse + if (token[1] == 'd') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.diffuse[0] = r; + material.diffuse[1] = g; + material.diffuse[2] = b; + continue; + } - // ior(index of refraction) - if (token[0] == 'N' && token[1] == 'i') { - token += 2; - material.ior = parseReal(&token); - continue; - } + // specular + if (token[1] == 's') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.specular[0] = r; + material.specular[1] = g; + material.specular[2] = b; + continue; + } - // shininess - if (token[0] == 'N' && token[1] == 's') { - token += 2; - material.shininess = parseReal(&token); - continue; - } - - - if (token[0] == 'T' && token[1] == 'r') { - token += 2; - if (has_d) { - // `d` wins. Ignore `Tr` value. - ss << "WARN: Both `d` and `Tr` parameters defined for \"" - << material.name << "\". Use the value of `d` for dissolve." - << std::endl; - } else { - // We invert value of Tr(assume Tr is in range [0, 1]) - // NOTE: Interpretation of Tr is application(exporter) dependent. For - // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43) - material.dissolve = 1.0f - parseReal(&token); - } - has_tr = true; - continue; - } + // transmittance + if (token[1] == 't') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.transmittance[0] = r; + material.transmittance[1] = g; + material.transmittance[2] = b; + continue; + } - //tigra: refactoring for new speedup release - if (token[0] == 'P') { - - // PBR: roughness - if(token[1] == 'r') - { - token += 2; - material.roughness = parseReal(&token); - continue; - } - else - // PBR: metallic - if(token[1] == 'm') - { - token += 2; - material.metallic = parseReal(&token); - continue; - } - else - // PBR: sheen - if(token[1] == 's') - { - token += 2; - material.sheen = parseReal(&token); - continue; - } - else - // PBR: clearcoat thickness - if(token[1] == 'c') - { - token += 2; - material.clearcoat_thickness = parseReal(&token); - continue; - } - } + // emission + if (token[1] == 'e') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.emission[0] = r; + material.emission[1] = g; + material.emission[2] = b; + continue; + } + } + + // transmittance + if ( //(token[0] == 'K' && token[1] == 't') || + token[0] == 'T' && token[1] == 'f') { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.transmittance[0] = r; + material.transmittance[1] = g; + material.transmittance[2] = b; + continue; + } + + // ior(index of refraction) + if (token[0] == 'N' && token[1] == 'i') { + token += 2; + material.ior = parseReal(&token); + continue; + } + + // shininess + if (token[0] == 'N' && token[1] == 's') { + token += 2; + material.shininess = parseReal(&token); + continue; + } + + if (token[0] == 'T' && token[1] == 'r') { + token += 2; + if (has_d) { + // `d` wins. Ignore `Tr` value. + ss << "WARN: Both `d` and `Tr` parameters defined for \"" + << material.name << "\". Use the value of `d` for dissolve." + << std::endl; + } else { + // We invert value of Tr(assume Tr is in range [0, 1]) + // NOTE: Interpretation of Tr is application(exporter) dependent. For + // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43) + material.dissolve = 1.0f - parseReal(&token); + } + has_tr = true; + continue; + } + + // tigra: refactoring for new speedup release + if (token[0] == 'P') { + // PBR: roughness + if (token[1] == 'r') { + token += 2; + material.roughness = parseReal(&token); + continue; + } else + // PBR: metallic + if (token[1] == 'm') { + token += 2; + material.metallic = parseReal(&token); + continue; + } else + // PBR: sheen + if (token[1] == 's') { + token += 2; + material.sheen = parseReal(&token); + continue; + } else + // PBR: clearcoat thickness + if (token[1] == 'c') { + token += 2; + material.clearcoat_thickness = parseReal(&token); + continue; + } + } } // dissolve @@ -1621,261 +1580,234 @@ std::map *material_map, } has_d = true; continue; - } - - - a_tok = token2tok(token); - - //tigra: minimize checks - if(a_tok>=TOK_newmtl && a_tok<=TOK_norm) - { - - //tigra: refactoring for new speedup release - // new mtl - //if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_newmtl) - { - // flush previous material. - if (!material.name.empty()) { - /* - material_map->insert(std::pair( - material.name, static_cast(materials->size())) - ); - */ - - - unsigned int hsh1 = X31_hash_string(material.name.c_str()); - - material_map->insert(std::pair( - hsh1, static_cast(materials->size())) - ); - materials->push_back(material); - } + } - // initial temporary material - InitMaterial(&material); + a_tok = token2tok(token); - has_d = false; - has_tr = false; + // tigra: minimize checks + if (a_tok >= TOK_newmtl && a_tok <= TOK_norm) { + // tigra: refactoring for new speedup release + // new mtl + // if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_newmtl) { + // flush previous material. + if (!material.name.empty()) { + /* + material_map->insert(std::pair( + material.name, static_cast(materials->size())) + ); + */ - // set new mtl name - token += 7; - { - /* - std::stringstream sstr; - sstr << token; - material.name = sstr.str(); - */ - - material.name = std::string(token); - } - continue; - } + unsigned int hsh1 = X31_hash_string(material.name.c_str()); - // illum model - //if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) - if (a_tok == TOK_illum) - { - token += 6; - material.illum = parseInt(&token); - continue; - } - - + material_map->insert(std::pair( + hsh1, static_cast(materials->size()))); + materials->push_back(material); + } - // PBR: clearcoat roughness - //if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) - if (a_tok == TOK_Pcr) - { - token += 4; - material.clearcoat_roughness = parseReal(&token); - continue; - } + // initial temporary material + InitMaterial(&material); - // PBR: anisotropy - //if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) - if (a_tok == TOK_aniso) - { - token += 6; - material.anisotropy = parseReal(&token); - continue; - } + has_d = false; + has_tr = false; - // PBR: anisotropy rotation - //if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_anisor) - { - token += 7; - material.anisotropy_rotation = parseReal(&token); - continue; - } + // set new mtl name + token += 7; + { + /* + std::stringstream sstr; + sstr << token; + material.name = sstr.str(); + */ - // ambient texture - //if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ka) - { - token += 7; - ParseTextureNameAndOption(&(material.ambient_texname), - &(material.ambient_texopt), token, - /* is_bump */ false); - continue; - } + material.name = std::string(token); + } + continue; + } - // diffuse texture - //if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Kd) - { - token += 7; - ParseTextureNameAndOption(&(material.diffuse_texname), - &(material.diffuse_texopt), token, - /* is_bump */ false); - continue; - } + // illum model + // if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) + if (a_tok == TOK_illum) { + token += 6; + material.illum = parseInt(&token); + continue; + } - // specular texture - //if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ks) - { - token += 7; - ParseTextureNameAndOption(&(material.specular_texname), - &(material.specular_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: clearcoat roughness + // if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) + if (a_tok == TOK_Pcr) { + token += 4; + material.clearcoat_roughness = parseReal(&token); + continue; + } - // specular highlight texture - //if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ns) - { - token += 7; - ParseTextureNameAndOption(&(material.specular_highlight_texname), - &(material.specular_highlight_texopt), token, - /* is_bump */ false); - continue; - } + // PBR: anisotropy + // if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_aniso) { + token += 6; + material.anisotropy = parseReal(&token); + continue; + } - // bump texture - //if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) - if (a_tok == TOK_map_bump) - { - token += 9; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // PBR: anisotropy rotation + // if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_anisor) { + token += 7; + material.anisotropy_rotation = parseReal(&token); + continue; + } - // bump texture - //if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) - if (a_tok == TOK_map_Bump) - { - token += 9; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // ambient texture + // if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ka) { + token += 7; + ParseTextureNameAndOption(&(material.ambient_texname), + &(material.ambient_texopt), token, + /* is_bump */ false); + continue; + } - // bump texture - //if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_bump) - { - token += 5; - ParseTextureNameAndOption(&(material.bump_texname), - &(material.bump_texopt), token, - /* is_bump */ true); - continue; - } + // diffuse texture + // if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Kd) { + token += 7; + ParseTextureNameAndOption(&(material.diffuse_texname), + &(material.diffuse_texopt), token, + /* is_bump */ false); + continue; + } - // alpha texture - //if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) - if (a_tok == TOK_map_d) - { - token += 6; - material.alpha_texname = token; - ParseTextureNameAndOption(&(material.alpha_texname), - &(material.alpha_texopt), token, - /* is_bump */ false); - continue; - } + // specular texture + // if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ks) { + token += 7; + ParseTextureNameAndOption(&(material.specular_texname), + &(material.specular_texopt), token, + /* is_bump */ false); + continue; + } - // displacement texture - //if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_disp) - { - token += 5; - ParseTextureNameAndOption(&(material.displacement_texname), - &(material.displacement_texopt), token, - /* is_bump */ false); - continue; - } + // specular highlight texture + // if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ns) { + token += 7; + ParseTextureNameAndOption(&(material.specular_highlight_texname), + &(material.specular_highlight_texopt), token, + /* is_bump */ false); + continue; + } - // reflection map - //if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_refl) - { - token += 5; - ParseTextureNameAndOption(&(material.reflection_texname), - &(material.reflection_texopt), token, - /* is_bump */ false); - continue; - } + // bump texture + // if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_bump) { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // PBR: roughness texture - //if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Pr) - { - token += 7; - ParseTextureNameAndOption(&(material.roughness_texname), - &(material.roughness_texopt), token, - /* is_bump */ false); - continue; - } + // bump texture + // if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) + if (a_tok == TOK_map_Bump) { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // PBR: metallic texture - //if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Pm) - { - token += 7; - ParseTextureNameAndOption(&(material.metallic_texname), - &(material.metallic_texopt), token, - /* is_bump */ false); - continue; - } + // bump texture + // if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_bump) { + token += 5; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token, + /* is_bump */ true); + continue; + } - // PBR: sheen texture - //if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ps) - { - token += 7; - ParseTextureNameAndOption(&(material.sheen_texname), - &(material.sheen_texopt), token, - /* is_bump */ false); - continue; - } + // alpha texture + // if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) + if (a_tok == TOK_map_d) { + token += 6; + material.alpha_texname = token; + ParseTextureNameAndOption(&(material.alpha_texname), + &(material.alpha_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: emissive texture - //if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) - if (a_tok == TOK_map_Ke) - { - token += 7; - ParseTextureNameAndOption(&(material.emissive_texname), - &(material.emissive_texopt), token, - /* is_bump */ false); - continue; - } + // displacement texture + // if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_disp) { + token += 5; + ParseTextureNameAndOption(&(material.displacement_texname), + &(material.displacement_texopt), token, + /* is_bump */ false); + continue; + } - // PBR: normal map texture - //if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) - if (a_tok == TOK_norm) - { - token += 5; - ParseTextureNameAndOption( - &(material.normal_texname), &(material.normal_texopt), token, - /* is_bump */ false); // @fixme { is_bump will be true? } - continue; - } - } + // reflection map + // if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_refl) { + token += 5; + ParseTextureNameAndOption(&(material.reflection_texname), + &(material.reflection_texopt), token, + /* is_bump */ false); + continue; + } + + // PBR: roughness texture + // if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pr) { + token += 7; + ParseTextureNameAndOption(&(material.roughness_texname), + &(material.roughness_texopt), token, + /* is_bump */ false); + continue; + } + + // PBR: metallic texture + // if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Pm) { + token += 7; + ParseTextureNameAndOption(&(material.metallic_texname), + &(material.metallic_texopt), token, + /* is_bump */ false); + continue; + } + + // PBR: sheen texture + // if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ps) { + token += 7; + ParseTextureNameAndOption(&(material.sheen_texname), + &(material.sheen_texopt), token, + /* is_bump */ false); + continue; + } + + // PBR: emissive texture + // if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) + if (a_tok == TOK_map_Ke) { + token += 7; + ParseTextureNameAndOption(&(material.emissive_texname), + &(material.emissive_texopt), token, + /* is_bump */ false); + continue; + } + + // PBR: normal map texture + // if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) + if (a_tok == TOK_norm) { + token += 5; + ParseTextureNameAndOption( + &(material.normal_texname), &(material.normal_texopt), token, + /* is_bump */ false); // @fixme { is_bump will be true? } + continue; + } + } // unknown parameter const char *_space = strchr(token, ' '); @@ -1890,17 +1822,17 @@ std::map *material_map, std::pair(key, value)); } } - + unsigned int 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()))); + */ + + material_map->insert( + std::pair(hsh1, static_cast(materials->size()))); materials->push_back(material); if (warning) { @@ -1910,7 +1842,7 @@ 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; @@ -1921,14 +1853,14 @@ bool MaterialFileReader::operator()(const std::string &matId, filepath = matId; } - //tigra: add buffered stream - #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD - ImportInBuf buf(filepath.c_str()); +// tigra: add buffered stream +#ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD + ImportInBuf buf(filepath.c_str()); std::istream matIStream(&buf); - #else - std::ifstream matIStream(filepath.c_str()); - #endif - +#else + std::ifstream matIStream(filepath.c_str()); +#endif + if (!matIStream) { std::stringstream ss; ss << "WARN: Material file [ " << filepath << " ] not found." << std::endl; @@ -1952,7 +1884,7 @@ 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; @@ -1988,14 +1920,14 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::stringstream errss; - //tigra: add buffered stream - #ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD - ImportInBuf buf(filename); +// tigra: add buffered stream +#ifdef TINYOBJLOADER_IMPLEMENTATION_BUFREAD + ImportInBuf buf(filename); std::istream ifs(&buf); - #else - std::ifstream ifs(filename); - #endif - +#else + std::ifstream ifs(filename); +#endif + if (!ifs) { errss << "Cannot open file [" << filename << "]" << std::endl; if (err) { @@ -2027,18 +1959,17 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, std::vector tags; std::vector > faceGroup; std::string name; - - + int a_tok; - - //init hashed tokens map + + // init hashed tokens map initHashedTokensMap(); - + // material - //std::map material_map; - - //tigra: key of material_map is unsigned int now - //because std::map is an red-black trees it is better to store key as int + // std::map material_map; + + // tigra: key of material_map is unsigned int now + // because std::map is an red-black trees it is better to store key as int std::map material_map; int material = -1; @@ -2073,204 +2004,198 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, if (token[0] == '#') continue; // comment line if (IS_SPACE((token[1]))) { - // vertex - if (token[0] == 'v') { - token += 2; - real_t x, y, z; - real_t r, g, b; - parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); - v.push_back(x); - v.push_back(y); - v.push_back(z); + // vertex + if (token[0] == 'v') { + token += 2; + real_t x, y, z; + real_t r, g, b; + parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); + v.push_back(x); + v.push_back(y); + v.push_back(z); - vc.push_back(r); - vc.push_back(g); - vc.push_back(b); - continue; - } - - // face - if (token[0] == 'f') { - token += 2; - token += strspn(token, " \t"); - - std::vector face; - face.reserve(3); - - while (!IS_NEW_LINE(token[0])) { - vertex_index vi; - if (!parseTriple(&token, static_cast(v.size() / 3), - static_cast(vn.size() / 3), - static_cast(vt.size() / 2), &vi)) { - if (err) { - (*err) = "Failed parse `f' line(e.g. zero value for face index).\n"; - } - return false; - } - - face.push_back(vi); - size_t n = strspn(token, " \t\r"); - token += n; - } - - // replace with emplace_back + std::move on C++11 - faceGroup.push_back(std::vector()); - faceGroup[faceGroup.size() - 1].swap(face); - - continue; - } - - // group name - if (token[0] == 'g') { - // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, - triangulate); - (void)ret; // return value not used. - - if (shape.mesh.indices.size() > 0) { - shapes->push_back(shape); - } - - shape = shape_t(); - - // material = -1; - faceGroup.clear(); - - std::vector names; - names.reserve(2); - - while (!IS_NEW_LINE(token[0])) { - std::string str = parseString(&token); - names.push_back(str); - token += strspn(token, " \t\r"); // skip tag - } - - assert(names.size() > 0); - - // names[0] must be 'g', so skip the 0th element. - if (names.size() > 1) { - name = names[1]; - } else { - name = ""; - } - - continue; - } - - // object name - if (token[0] == 'o') { - // flush previous face group. - bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, name, - triangulate); - if (ret) { - shapes->push_back(shape); - } - - // material = -1; - faceGroup.clear(); - shape = shape_t(); - - // @todo { multiple object name? } - token += 2; - - /* - std::stringstream ss; - ss << token; - name = ss.str(); - */ - - name = std::string(token); - - continue; - } - - if (token[0] == 't') { - tag_t tag; - - token += 2; - - tag.name = parseString(&token); - - tag_sizes ts = parseTagTriple(&token); - - tag.intValues.resize(static_cast(ts.num_ints)); - - for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { - tag.intValues[i] = parseInt(&token); - } - - tag.floatValues.resize(static_cast(ts.num_reals)); - for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { - tag.floatValues[i] = parseReal(&token); - } - - tag.stringValues.resize(static_cast(ts.num_strings)); - for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { - tag.stringValues[i] = parseString(&token); - } - - tags.push_back(tag); - } - } - - - - if (token[0] == 'v' && IS_SPACE((token[2]))) { - // normal - if (token[1] == 'n') { - token += 3; - real_t x, y, z; - parseReal3(&x, &y, &z, &token); - vn.push_back(x); - vn.push_back(y); - vn.push_back(z); - continue; - } - - // texcoord - if (token[1] == 't') { - token += 3; - real_t x, y; - parseReal2(&x, &y, &token); - vt.push_back(x); - vt.push_back(y); - continue; - } - } - - //tigra: refactoring for new speedup release - //tigra: compares one more start - - - a_tok = token2tok(token); - - - // use mtl - //if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) - if (a_tok==TOK_usemtl) - { - token += 7; - /* - std::stringstream ss; - ss << token; - 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 } + vc.push_back(r); + vc.push_back(g); + vc.push_back(b); + continue; } - */ - - - - unsigned int hsh = X31_hash_string(token); - + + // face + if (token[0] == 'f') { + token += 2; + token += strspn(token, " \t"); + + std::vector face; + face.reserve(3); + + while (!IS_NEW_LINE(token[0])) { + vertex_index vi; + if (!parseTriple(&token, static_cast(v.size() / 3), + static_cast(vn.size() / 3), + static_cast(vt.size() / 2), &vi)) { + if (err) { + (*err) = + "Failed parse `f' line(e.g. zero value for face index).\n"; + } + return false; + } + + face.push_back(vi); + size_t n = strspn(token, " \t\r"); + token += n; + } + + // replace with emplace_back + std::move on C++11 + faceGroup.push_back(std::vector()); + faceGroup[faceGroup.size() - 1].swap(face); + + continue; + } + + // group name + if (token[0] == 'g') { + // flush previous face group. + bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, + name, triangulate); + (void)ret; // return value not used. + + if (shape.mesh.indices.size() > 0) { + shapes->push_back(shape); + } + + shape = shape_t(); + + // material = -1; + faceGroup.clear(); + + std::vector names; + names.reserve(2); + + while (!IS_NEW_LINE(token[0])) { + std::string str = parseString(&token); + names.push_back(str); + token += strspn(token, " \t\r"); // skip tag + } + + assert(names.size() > 0); + + // names[0] must be 'g', so skip the 0th element. + if (names.size() > 1) { + name = names[1]; + } else { + name = ""; + } + + continue; + } + + // object name + if (token[0] == 'o') { + // flush previous face group. + bool ret = exportFaceGroupToShape(&shape, faceGroup, tags, material, + name, triangulate); + if (ret) { + shapes->push_back(shape); + } + + // material = -1; + faceGroup.clear(); + shape = shape_t(); + + // @todo { multiple object name? } + token += 2; + + /* + std::stringstream ss; + ss << token; + name = ss.str(); + */ + + name = std::string(token); + + continue; + } + + if (token[0] == 't') { + tag_t tag; + + token += 2; + + tag.name = parseString(&token); + + tag_sizes ts = parseTagTriple(&token); + + tag.intValues.resize(static_cast(ts.num_ints)); + + for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { + tag.intValues[i] = parseInt(&token); + } + + tag.floatValues.resize(static_cast(ts.num_reals)); + for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { + tag.floatValues[i] = parseReal(&token); + } + + tag.stringValues.resize(static_cast(ts.num_strings)); + for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { + tag.stringValues[i] = parseString(&token); + } + + tags.push_back(tag); + } + } + + if (token[0] == 'v' && IS_SPACE((token[2]))) { + // normal + if (token[1] == 'n') { + token += 3; + real_t x, y, z; + parseReal3(&x, &y, &z, &token); + vn.push_back(x); + vn.push_back(y); + vn.push_back(z); + continue; + } + + // texcoord + if (token[1] == 't') { + token += 3; + real_t x, y; + parseReal2(&x, &y, &token); + vt.push_back(x); + vt.push_back(y); + continue; + } + } + + // tigra: refactoring for new speedup release + // tigra: compares one more start + + a_tok = token2tok(token); + + // use mtl + // if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_usemtl) { + token += 7; + /* + std::stringstream ss; + ss << token; + 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 } + } + */ + + unsigned int hsh = X31_hash_string(token); + int newMaterialId = -1; if (material_map.find(hsh) != material_map.end()) { newMaterialId = material_map[hsh]; @@ -2292,9 +2217,8 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, } // load mtl - //if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) - if (a_tok==TOK_mtllib) - { + // if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_mtllib) { if (readMatFn) { token += 7; @@ -2335,8 +2259,8 @@ bool LoadObj(attrib_t *attrib, std::vector *shapes, continue; } - //tigra: compares one more END - //tigra: refactoring for new speedup release + // tigra: compares one more END + // tigra: refactoring for new speedup release // Ignore unknown command. } @@ -2369,17 +2293,14 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, MaterialReader *readMatFn /*= NULL*/, std::string *err /*= NULL*/) { std::stringstream errss; - - + int a_tok; - - //init hashed tokens map + + // init hashed tokens map initHashedTokensMap(); - - // material - //std::map material_map; + // std::map material_map; std::map material_map; int material_id = -1; // -1 = invalid @@ -2521,16 +2442,16 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, // @todo { multiple object name? } token += 2; - /* - std::stringstream ss; - ss << token; - std::string object_name = ss.str(); + /* + std::stringstream ss; + ss << token; + std::string object_name = ss.str(); + + if (callback.object_cb) { + callback.object_cb(user_data, object_name.c_str()); + } + */ - if (callback.object_cb) { - callback.object_cb(user_data, object_name.c_str()); - } - */ - if (callback.object_cb) { callback.object_cb(user_data, token); } @@ -2586,40 +2507,35 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, } #endif + // tigra: refactoring for new speedup release + // tigra: compares start + a_tok = token2tok(token); - //tigra: refactoring for new speedup release - //tigra: compares start - - - a_tok = token2tok(token); - - // use mtl - //if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) - if (a_tok==TOK_usemtl) - { + // if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_usemtl) { token += 7; - /* - std::stringstream ss; - ss << token; - std::string namebuf = ss.str(); + /* + std::stringstream ss; + ss << token; + 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 } + } */ - - /* - 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 - unsigned int hsh = X31_hash_string(token); - + + // make a hash from token + unsigned int hsh = X31_hash_string(token); + int newMaterialId = -1; if (material_map.find(hsh) != material_map.end()) { newMaterialId = material_map[hsh]; @@ -2632,7 +2548,7 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, } 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); } @@ -2640,9 +2556,8 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, } // load mtl - //if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) - if (a_tok == TOK_mtllib) - { + // if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) + if (a_tok == TOK_mtllib) { if (readMatFn) { token += 7; @@ -2688,8 +2603,8 @@ bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback, continue; } - //tigra: compares end - //tigra: refactoring for new speedup release + // tigra: compares end + // tigra: refactoring for new speedup release // Ignore unknown command. }