Skip to content

Commit 94fc413

Browse files
committed
Fix parsing of crease tags(t)
Support parsing texture filename containing whitespace.
1 parent 1c6dbf9 commit 94fc413

4 files changed

Lines changed: 98 additions & 15 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
newmtl white
2+
Ka 0 0 0
3+
Kd 1 1 1
4+
Ks 0 0 0
5+
# filename with white space.
6+
map_Kd texture 01.png
7+
8+
newmtl red
9+
Ka 0 0 0
10+
Kd 1 0 0
11+
Ks 0 0 0
12+
# texture option + filename with white space.
13+
bump -bm 2 bump 01.png
14+
15+
newmtl green
16+
Ka 0 0 0
17+
Kd 0 1 0
18+
Ks 0 0 0
19+
20+
newmtl blue
21+
Ka 0 0 0
22+
Kd 0 0 1
23+
Ks 0 0 0
24+
25+
newmtl light
26+
Ka 20 20 20
27+
Kd 1 1 1
28+
Ks 0 0 0
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
mtllib texture-filename-with-whitespace.mtl
2+
3+
v 0.000000 2.000000 2.000000
4+
v 0.000000 0.000000 2.000000
5+
v 2.000000 0.000000 2.000000
6+
v 2.000000 2.000000 2.000000
7+
v 0.000000 2.000000 0.000000
8+
v 0.000000 0.000000 0.000000
9+
v 2.000000 0.000000 0.000000
10+
v 2.000000 2.000000 0.000000
11+
# 8 vertices
12+
13+
g front cube
14+
usemtl white
15+
f 1 2 3 4
16+
g back cube
17+
# expects white material
18+
f 8 7 6 5
19+
g right cube
20+
usemtl red
21+
f 4 3 7 8
22+
g top cube
23+
usemtl white
24+
f 5 1 4 8
25+
g left cube
26+
usemtl green
27+
f 5 6 2 1
28+
g bottom cube
29+
usemtl white
30+
f 2 6 7 3
31+
# 6 elements

tests/tester.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,29 @@ TEST_CASE("zero-face-idx-value", "[Issue140]") {
694694

695695
}
696696

697+
TEST_CASE("texture-name-whitespace", "[Issue145]") {
698+
tinyobj::attrib_t attrib;
699+
std::vector<tinyobj::shape_t> shapes;
700+
std::vector<tinyobj::material_t> materials;
701+
702+
std::string err;
703+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/texture-filename-with-whitespace.obj", gMtlBasePath);
704+
705+
706+
if (!err.empty()) {
707+
std::cerr << "[Issue145] " << err << std::endl;
708+
}
709+
710+
REQUIRE(true == ret);
711+
REQUIRE(err.empty());
712+
REQUIRE(2 < materials.size());
713+
714+
REQUIRE(0 == materials[0].diffuse_texname.compare("texture 01.png"));
715+
REQUIRE(0 == materials[1].bump_texname.compare("bump 01.png"));
716+
REQUIRE(2 == Approx(materials[1].bump_texopt.bump_multiplier));
717+
718+
}
719+
697720
#if 0
698721
int
699722
main(

tiny_obj_loader.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -721,22 +721,24 @@ static inline texture_type_t parseTextureType(
721721
static tag_sizes parseTagTriple(const char **token) {
722722
tag_sizes ts;
723723

724+
(*token) += strspn((*token), " \t");
724725
ts.num_ints = atoi((*token));
725726
(*token) += strcspn((*token), "/ \t\r");
726727
if ((*token)[0] != '/') {
727728
return ts;
728729
}
729-
(*token)++;
730730

731+
(*token)++; // Skip '/'
732+
733+
(*token) += strspn((*token), " \t");
731734
ts.num_reals = atoi((*token));
732735
(*token) += strcspn((*token), "/ \t\r");
733736
if ((*token)[0] != '/') {
734737
return ts;
735738
}
736-
(*token)++;
739+
(*token)++; // Skip '/'
737740

738-
ts.num_strings = atoi((*token));
739-
(*token) += strcspn((*token), "/ \t\r") + 1;
741+
ts.num_strings = parseInt(token);
740742

741743
return ts;
742744
}
@@ -906,11 +908,18 @@ static bool ParseTextureNameAndOption(std::string *texname,
906908
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
907909
} else {
908910
// Assume texture filename
911+
#if 0
909912
size_t len = strcspn(token, " \t\r"); // untile next space
910913
texture_name = std::string(token, token + len);
911914
token += len;
912915

913916
token += strspn(token, " \t"); // skip space
917+
#else
918+
// Read filename until line end to parse filename containing whitespace
919+
// TODO(syoyo): Support parsing texture option flag after the filename.
920+
texture_name = std::string(token);
921+
token += texture_name.length();
922+
#endif
914923

915924
found_texname = true;
916925
}
@@ -1762,33 +1771,25 @@ bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
17621771
tag_t tag;
17631772

17641773
token += 2;
1765-
std::stringstream ss;
1766-
ss << token;
1767-
tag.name = ss.str();
17681774

1769-
token += tag.name.size() + 1;
1775+
tag.name = parseString(&token);
17701776

17711777
tag_sizes ts = parseTagTriple(&token);
17721778

17731779
tag.intValues.resize(static_cast<size_t>(ts.num_ints));
17741780

17751781
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
1776-
tag.intValues[i] = atoi(token);
1777-
token += strcspn(token, "/ \t\r") + 1;
1782+
tag.intValues[i] = parseInt(&token);
17781783
}
17791784

17801785
tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
17811786
for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
17821787
tag.floatValues[i] = parseReal(&token);
1783-
token += strcspn(token, "/ \t\r") + 1;
17841788
}
17851789

17861790
tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
17871791
for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
1788-
std::stringstream sstr;
1789-
sstr << token;
1790-
tag.stringValues[i] = sstr.str();
1791-
token += tag.stringValues[i].size() + 1;
1792+
tag.stringValues[i] = parseString(&token);
17921793
}
17931794

17941795
tags.push_back(tag);

0 commit comments

Comments
 (0)