diff --git a/models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png b/models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png new file mode 100644 index 0000000..67d8040 Binary files /dev/null and b/models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png differ diff --git a/models/CubeImageUriSpaces/2x2 image has spaces.png b/models/CubeImageUriSpaces/2x2 image has spaces.png new file mode 100644 index 0000000..67d8040 Binary files /dev/null and b/models/CubeImageUriSpaces/2x2 image has spaces.png differ diff --git a/models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf b/models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf new file mode 100644 index 0000000..49d32ec --- /dev/null +++ b/models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf @@ -0,0 +1,171 @@ +{ + "asset": { + "version": "2.0" + }, + "scenes": [ + { + "nodes": [ + 0 + ] + } + ], + "scene": 0, + "nodes": [ + { + "mesh": 0 + } + ], + "meshes": [ + { + "primitives": [ + { + "attributes": { + "NORMAL": 2, + "POSITION": 1, + "TEXCOORD_0": 3 + }, + "indices": 0, + "mode": 4, + "material": 0 + } + ] + } + ], + "materials": [ + { + "pbrMetallicRoughness": { + "baseColorTexture": { + "index": 0, + "texCoord": 0 + }, + "baseColorFactor": [ + 1, + 1, + 1, + 1 + ], + "metallicFactor": 1, + "roughnessFactor": 1 + }, + "emissiveFactor": [ + 0, + 0, + 0 + ], + "alphaMode": "OPAQUE" + } + ], + "textures": [ + { + "source": 0, + "sampler": 0 + } + ], + "samplers": [ + { + "wrapS": 33071, + "wrapT": 33071 + } + ], + "images": [ + { + "uri": " 2x2 image has multiple spaces.png " + } + ], + "accessors": [ + { + "bufferView": 0, + "byteOffset": 0, + "componentType": 5121, + "count": 36, + "normalized": false, + "max": [ + 23 + ], + "min": [ + 0 + ], + "type": "SCALAR" + }, + { + "bufferView": 1, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 0.5, + 0.5, + 0.5 + ], + "min": [ + -0.5, + -0.5, + -0.5 + ], + "type": "VEC3" + }, + { + "bufferView": 2, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 1, + 1, + 1 + ], + "min": [ + -1, + -1, + -1 + ], + "type": "VEC3" + }, + { + "bufferView": 3, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 1, + 1 + ], + "min": [ + 0, + 0 + ], + "type": "VEC2" + } + ], + "bufferViews": [ + { + "buffer": 0, + "byteOffset": 0, + "byteLength": 36 + }, + { + "buffer": 0, + "byteOffset": 36, + "byteLength": 288 + }, + { + "buffer": 0, + "byteOffset": 324, + "byteLength": 288 + }, + { + "buffer": 0, + "byteOffset": 612, + "byteLength": 192 + } + ], + "buffers": [ + { + "byteLength": 804, + "uri": "CubeImageUriSpaces.bin" + } + ] +} diff --git a/models/CubeImageUriSpaces/CubeImageUriSpaces.bin b/models/CubeImageUriSpaces/CubeImageUriSpaces.bin new file mode 100644 index 0000000..2059867 Binary files /dev/null and b/models/CubeImageUriSpaces/CubeImageUriSpaces.bin differ diff --git a/models/CubeImageUriSpaces/CubeImageUriSpaces.gltf b/models/CubeImageUriSpaces/CubeImageUriSpaces.gltf new file mode 100644 index 0000000..82c166d --- /dev/null +++ b/models/CubeImageUriSpaces/CubeImageUriSpaces.gltf @@ -0,0 +1,171 @@ +{ + "asset": { + "version": "2.0" + }, + "scenes": [ + { + "nodes": [ + 0 + ] + } + ], + "scene": 0, + "nodes": [ + { + "mesh": 0 + } + ], + "meshes": [ + { + "primitives": [ + { + "attributes": { + "NORMAL": 2, + "POSITION": 1, + "TEXCOORD_0": 3 + }, + "indices": 0, + "mode": 4, + "material": 0 + } + ] + } + ], + "materials": [ + { + "pbrMetallicRoughness": { + "baseColorTexture": { + "index": 0, + "texCoord": 0 + }, + "baseColorFactor": [ + 1, + 1, + 1, + 1 + ], + "metallicFactor": 1, + "roughnessFactor": 1 + }, + "emissiveFactor": [ + 0, + 0, + 0 + ], + "alphaMode": "OPAQUE" + } + ], + "textures": [ + { + "source": 0, + "sampler": 0 + } + ], + "samplers": [ + { + "wrapS": 33071, + "wrapT": 33071 + } + ], + "images": [ + { + "uri": "2x2 image has spaces.png" + } + ], + "accessors": [ + { + "bufferView": 0, + "byteOffset": 0, + "componentType": 5121, + "count": 36, + "normalized": false, + "max": [ + 23 + ], + "min": [ + 0 + ], + "type": "SCALAR" + }, + { + "bufferView": 1, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 0.5, + 0.5, + 0.5 + ], + "min": [ + -0.5, + -0.5, + -0.5 + ], + "type": "VEC3" + }, + { + "bufferView": 2, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 1, + 1, + 1 + ], + "min": [ + -1, + -1, + -1 + ], + "type": "VEC3" + }, + { + "bufferView": 3, + "byteOffset": 0, + "componentType": 5126, + "count": 24, + "normalized": false, + "max": [ + 1, + 1 + ], + "min": [ + 0, + 0 + ], + "type": "VEC2" + } + ], + "bufferViews": [ + { + "buffer": 0, + "byteOffset": 0, + "byteLength": 36 + }, + { + "buffer": 0, + "byteOffset": 36, + "byteLength": 288 + }, + { + "buffer": 0, + "byteOffset": 324, + "byteLength": 288 + }, + { + "buffer": 0, + "byteOffset": 612, + "byteLength": 192 + } + ], + "buffers": [ + { + "byteLength": 804, + "uri": "CubeImageUriSpaces.bin" + } + ] +} diff --git a/tests/tester.cc b/tests/tester.cc index 58ec000..c6d31b0 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -333,3 +333,29 @@ TEST_CASE("pbr-khr-texture-transform", "[material]") { REQUIRE(scale[1] == Approx(-1.0)); } + +TEST_CASE("image-uri-spaces", "[issue-236]") { + + tinygltf::Model model; + tinygltf::TinyGLTF ctx; + std::string err; + std::string warn; + + // Test image file with single spaces. + bool ret = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/CubeImageUriSpaces/CubeImageUriSpaces.gltf"); + if (!err.empty()) { + std::cerr << err << std::endl; + } + + REQUIRE(true == ret); + + // Test image file with a beginning space, trailing space, and greater than + // one consecutive spaces. + ret = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf"); + if (!err.empty()) { + std::cerr << err << std::endl; + } + + REQUIRE(true == ret); +} + diff --git a/tiny_gltf.h b/tiny_gltf.h index c26fe40..91c77b5 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -2458,8 +2458,10 @@ std::string ExpandFilePath(const std::string &filepath, void *) { return ""; } + // Quote the string to keep any spaces in filepath intact. + std::string quoted_path = "\"" + filepath + "\""; // char** w; - int ret = wordexp(filepath.c_str(), &p, 0); + int ret = wordexp(quoted_path.c_str(), &p, 0); if (ret) { // err s = filepath;