Skip to content

Commit 0a85945

Browse files
committed
Skip trailing whitespace in mtl. Fixes #92.
1 parent 1983e88 commit 0a85945

4 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎models/issue-92.mtl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
newmtl default
2+
Ka 0 0 0
3+
Kd 0 0 0
4+
Ks 0 0 0
5+
map_Kd tmp.png
6+

‎models/issue-92.obj‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mtllib issue-92.mtl
2+
o Test
3+
v 1.864151 -1.219172 -5.532511
4+
v 0.575869 -0.666304 5.896140
5+
v 0.940448 1.000000 -1.971128
6+
usemtl default
7+
f 1 2 3

‎tests/tester.cc‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ TEST_CASE("stream_load", "[Stream]") {
351351
REQUIRE(true == TestStreamLoadObj());
352352
}
353353

354+
TEST_CASE("trailing_whitespace_in_mtl", "[Issue92]") {
355+
tinyobj::attrib_t attrib;
356+
std::vector<tinyobj::shape_t> shapes;
357+
std::vector<tinyobj::material_t> materials;
358+
359+
std::string err;
360+
bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/issue-92.obj", gMtlBasePath);
361+
362+
if (!err.empty()) {
363+
std::cerr << err << std::endl;
364+
}
365+
REQUIRE(true == ret);
366+
REQUIRE(1 == materials.size());
367+
REQUIRE(0 == materials[0].diffuse_texname.compare("tmp.png"));
368+
}
369+
354370
#if 0
355371
int
356372
main(

‎tiny_obj_loader.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,7 @@ static vertex_index parseTriple(const char **token, int vsize, int vnsize,
534534

535535
// Parse raw triples: i, i/j/k, i//k, i/j
536536
static vertex_index parseRawTriple(const char **token) {
537-
vertex_index vi(
538-
static_cast<int>(0)); // 0 is an invalid index in OBJ
537+
vertex_index vi(static_cast<int>(0)); // 0 is an invalid index in OBJ
539538

540539
vi.v_idx = atoi((*token));
541540
(*token) += strcspn((*token), "/ \t\r");
@@ -679,6 +678,11 @@ void LoadMtl(std::map<std::string, int> *material_map,
679678

680679
std::string linebuf(&buf[0]);
681680

681+
// Trim trailing whitespace.
682+
if (linebuf.size() > 0) {
683+
linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1);
684+
}
685+
682686
// Trim newline '\r\n' or '\n'
683687
if (linebuf.size() > 0) {
684688
if (linebuf[linebuf.size() - 1] == '\n')

0 commit comments

Comments
 (0)