From 1cdfd786d8dabf2ff9896c9c7e634cfd90c664a1 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Fri, 14 Sep 2018 13:42:27 +0900 Subject: [PATCH] Add colorspace extension to texture options. Fixes #184 --- models/colorspace-issue-184.mtl | 9 +++++++++ models/colorspace-issue-184.obj | 7 +++++++ tests/tester.cc | 19 +++++++++++++++++++ tiny_obj_loader.h | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 models/colorspace-issue-184.mtl create mode 100644 models/colorspace-issue-184.obj diff --git a/models/colorspace-issue-184.mtl b/models/colorspace-issue-184.mtl new file mode 100644 index 0000000..7f3f9a4 --- /dev/null +++ b/models/colorspace-issue-184.mtl @@ -0,0 +1,9 @@ +newmtl default +Ka 0 0 0 +Kd 0 0 0 +Ks 0 0 0 +Kt 0.1 0.2 0.3 +map_Kd -colorspace sRGB -o 0.1 diffuse.jpg +map_Ks -s 0.1 0.2 specular.jpg +map_bump -colorspace linear -bm 3 bumpmap.jpg + diff --git a/models/colorspace-issue-184.obj b/models/colorspace-issue-184.obj new file mode 100644 index 0000000..ae589cb --- /dev/null +++ b/models/colorspace-issue-184.obj @@ -0,0 +1,7 @@ +mtllib colorspace-issue-184.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 25b8836..e24d1e2 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -825,6 +825,25 @@ TEST_CASE("multiple-group-names", "[group]") { REQUIRE(0 == shapes[1].name.compare("back cube")); // multiple whitespaces are aggregated as single white space. } +TEST_CASE("colorspace", "[Issue184]") { + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + + std::string err; + bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &err, "../models/colorspace-issue-184.obj", gMtlBasePath); + + if (!err.empty()) { + std::cerr << err << std::endl; + } + REQUIRE(true == ret); + REQUIRE(1 == shapes.size()); + REQUIRE(1 == materials.size()); + REQUIRE(0 == materials[0].diffuse_texopt.colorspace.compare("sRGB")); + REQUIRE(0 == materials[0].specular_texopt.colorspace.size()); + REQUIRE(0 == materials[0].bump_texopt.colorspace.compare("linear")); +} + // Fuzzer test. // Just check if it does not crash. // Disable by default since Windows filesystem can't create filename of afl testdata diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index 460edd0..f627e94 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -23,6 +23,7 @@ THE SOFTWARE. */ // +// version 1.2.3 : Added color space extension('-colorspace') to tex opts. // version 1.2.2 : Parse multiple group names. // version 1.2.1 : Added initial support for line('l') primitive(PR #178) // version 1.2.0 : Hardened implementation(#175) @@ -113,6 +114,11 @@ namespace tinyobj { // cube_front | cube_back | # side of the cube is specified // separately // cube_left | cube_right +// +// TinyObjLoader extension. +// +// -colorspace SPACE # Color space of the texture. e.g. 'sRGB` or 'linear' +// #ifdef TINYOBJLOADER_USE_DOUBLE //#pragma message "using double" @@ -147,6 +153,9 @@ typedef struct { bool blendu; // -blendu (default on) bool blendv; // -blendv (default on) real_t bump_multiplier; // -bm (for bump maps only, default 1.0) + + // extension + std::string colorspace; // Explicitly specify color space of stored value. Usually `sRGB` or `linear` (default empty). } texture_option_t; typedef struct { @@ -950,6 +959,9 @@ static bool ParseTextureNameAndOption(std::string *texname, } else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) { token += 4; parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); + } else if ((0 == strncmp(token, "-colorspace", 11)) && IS_SPACE((token[11]))) { + token += 12; + texopt->colorspace = parseString(&token); } else { // Assume texture filename #if 0