From 0a8594576713b736ba71b2813aeb6f21adf201dc Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Tue, 2 Aug 2016 17:04:00 +0900 Subject: [PATCH] Skip trailing whitespace in mtl. Fixes #92. --- models/issue-92.mtl | 6 ++++++ models/issue-92.obj | 7 +++++++ tests/tester.cc | 16 ++++++++++++++++ tiny_obj_loader.h | 8 ++++++-- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 models/issue-92.mtl create mode 100644 models/issue-92.obj diff --git a/models/issue-92.mtl b/models/issue-92.mtl new file mode 100644 index 0000000..5ebd668 --- /dev/null +++ b/models/issue-92.mtl @@ -0,0 +1,6 @@ +newmtl default +Ka 0 0 0 +Kd 0 0 0 +Ks 0 0 0 +map_Kd tmp.png + diff --git a/models/issue-92.obj b/models/issue-92.obj new file mode 100644 index 0000000..f7be3b6 --- /dev/null +++ b/models/issue-92.obj @@ -0,0 +1,7 @@ +mtllib issue-92.mtl +o Test +v 1.864151 -1.219172 -5.532511 +v 0.575869 -0.666304 5.896140 +v 0.940448 1.000000 -1.971128 +usemtl default +f 1 2 3 diff --git a/tests/tester.cc b/tests/tester.cc index dbfb939..8ca12de 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -351,6 +351,22 @@ TEST_CASE("stream_load", "[Stream]") { REQUIRE(true == TestStreamLoadObj()); } +TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") { + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + + std::string err; + bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath); + + if (!err.empty()) { + std::cerr << err << std::endl; + } + REQUIRE(true == ret); + REQUIRE(1 == materials.size()); + REQUIRE(0 == materials[0].diffuse_texname.compare("tmp.png")); +} + #if 0 int main( diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index a1f4645..0bdbdf8 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -534,8 +534,7 @@ static vertex_index parseTriple(const char **token, int vsize, int vnsize, // Parse raw triples: i, i/j/k, i//k, i/j static vertex_index parseRawTriple(const char **token) { - vertex_index vi( - static_cast(0)); // 0 is an invalid index in OBJ + vertex_index vi(static_cast(0)); // 0 is an invalid index in OBJ vi.v_idx = atoi((*token)); (*token) += strcspn((*token), "/ \t\r"); @@ -679,6 +678,11 @@ void LoadMtl(std::map *material_map, std::string linebuf(&buf[0]); + // Trim trailing whitespace. + if (linebuf.size() > 0) { + linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1); + } + // Trim newline '\r\n' or '\n' if (linebuf.size() > 0) { if (linebuf[linebuf.size() - 1] == '\n')