Compare commits

...

74 Commits

Author SHA1 Message Date
Syoyo Fujita
f82e3e7238 Add internal JSON backend option 2025-11-30 21:19:30 +09:00
Syoyo Fujita
19733906ec Harden input limits and add contributor guide 2025-11-30 10:21:17 +09:00
Syoyo Fujita
b956fa3e9d Merge pull request #512 from ctrlaltf2/oob-fix
Add bounds check to images loaded from bufferviews
2025-01-22 22:45:07 +09:00
ctrlaltf2
baa70db5a8 Add bounds check to images loaded from bufferviews 2025-01-20 23:43:07 -05:00
Syoyo Fujita
5f330f3952 Merge pull request #509 from NoirMorilec/fix-no-fs
Added NO_FS definition for std::ofstream usage
2024-12-30 22:11:09 +09:00
Leonid
5d7fa94a4b Added NO_FS definition for std::ofstream usage 2024-12-30 03:12:20 +08:00
Syoyo Fujita
a93facf9b2 Merge pull request #507 from thearchivalone/release
Documentation: Submodule hint added
2024-12-21 19:59:42 +09:00
Brad
0c4b226f1f Documentation: Submodule hint added 2024-12-20 17:49:51 -06:00
Syoyo Fujita
2d31d6fe74 Merge pull request #504 from nim65s/vendor 2024-10-16 23:27:42 +09:00
Syoyo Fujita
50a29a6cf6 Merge pull request #503 from nim65s/release 2024-10-16 23:26:53 +09:00
Guilhem Saurel
bf23ecdfe1 CMake: fix export install dir 2024-10-16 15:53:48 +02:00
Guilhem Saurel
ce27d3cb2b CMake: allow opt-out of installing vendored headers 2024-10-16 14:58:42 +02:00
Syoyo Fujita
14ba27113e Merge pull request #501 from msklywenn/release
Fix Animation extensions being loaded in place of Sampler extensions
2024-08-08 21:47:56 +09:00
Daniel Borges
1b983add49 Merge branch 'syoyo:release' into release 2024-08-08 10:25:11 +02:00
Syoyo Fujita
f132d242aa Merge pull request #496 from ptc-tgamper/bug/issue-495
Allow WriteImageDataFunction() callback to be called with empty images
2024-07-26 21:44:16 +09:00
Thomas Gamper
bde9ad61c1 fixes #495
Fix issues that block custom image loaders and writers to deal with empty images
2024-07-23 11:45:54 +02:00
Daniel Borges
314faac311 fixed ParseAnimation loading animation's extensions into sampler instead of sample's extensions. 2024-07-19 12:34:16 +02:00
Syoyo Fujita
3cb2822512 Merge pull request #493 from ptc-tgamper/bug/model_clear_on_load
Properly clear the model before loading
2024-07-06 02:43:52 +09:00
Thomas Gamper
7c16fd3b43 Properly clear the model before loading 2024-07-05 08:57:36 +02:00
Syoyo Fujita
35d3f5ae0a Update README.md 2024-07-04 03:01:46 +09:00
Syoyo Fujita
5a20fbd7f7 Merge pull request #492 from danwillm/inverse-bind-matrix
Make inverseBindMatrices optional
2024-07-04 02:59:35 +09:00
danwillm
0e28f1ef38 Add test for inverse bind matrices being optional 2024-07-01 22:32:17 +01:00
danwillm
54fd914bc1 Make inverseBindMatrices optional 2024-07-01 18:31:00 +01:00
Syoyo Fujita
43055c0ed1 bump minor version. 2024-06-28 21:30:43 +09:00
Syoyo Fujita
953bf8bd1c Merge pull request #491 from ptc-tgamper/bug/image_saving
Fix images not being saved due to missing filesystem callbacks
2024-06-28 21:24:53 +09:00
Thomas Gamper
9cf25ef930 fixes #487
Support image as_is flag in loading and saving
2024-06-28 12:17:38 +02:00
Thomas Gamper
a440b44ef9 fixes #473
tiny_gltf.h - explicitly pass filesystem callbacks to image related functions
tester.cc - add respective test case, fix image uri test case
2024-06-25 15:12:30 +02:00
Syoyo Fujita
efe2957c4a Merge pull request #489 from SeanCurtis-TRI/PR_callback_as_function
Update typedefs of C-style function pointers to std::function
2024-06-12 04:01:37 +09:00
Sean Curtis
4692f7a714 Remove asserts 2024-06-10 14:18:31 -07:00
Sean Curtis
5d1882cf2f Update typedefs of C-style function pointers to std::function
This allows for the callback to maintain their own state (without recourse
to globals).

In addition, added some incidental clean up:

  - URICallback, passed by pointer, is now asserted to be non-null before
    accessing.
  - FSCallbacks are validated when they are set (in debug builds).
2024-06-06 07:45:01 -07:00
Syoyo Fujita
9aa1c3c393 Merge pull request #486 from pmcgvr/release
Fix stripping of slashes from paths
2024-05-24 02:49:22 +09:00
Syoyo Fujita
394c48c25d Merge pull request #485 from bwrsandman/patch-1
msvc 32bit: Fix C4244 warning
2024-05-21 04:11:31 +09:00
Patrick Mc Gartoll
30457a5373 Fix stripping of slashes from paths 2024-05-16 18:11:26 -07:00
Sandy
73b2d0926f msvc 32bit: Fix C4244 warning
On 32 bit msvc compilers with warnings on, there are C4244 warnings about  from 'std::streamoff' to size_t to vector::size_type
2024-05-10 08:47:23 -04:00
Syoyo Fujita
3a65e2ddb8 Merge pull request #482 from jam3sward/fix-c4018-warnings-msvc-win32
Fix C4018 warnings in MSVC on WIN32
2024-03-27 03:43:42 +09:00
jamesvert
8a61c041d5 Resolve overload ambiguity in VS2015 (version 14.0) 2024-03-26 11:32:43 +00:00
jamesvert
c74040e9f0 Fix C4018 warnings in MSVC on WIN32 2024-03-26 11:06:16 +00:00
Syoyo Fujita
223a51270a Merge pull request #481 from jam3sward/fix-c4267-warnings-vs-win32
Fix C4267 warnings in Visual Studio on WIN32
2024-03-26 05:23:03 +09:00
James Ward
d98da79545 Fix C4267 warnings in Visual Studio on WIN32 2024-03-25 17:50:33 +00:00
Syoyo Fujita
26d0bc2e1a Merge pull request #479 from The0Dev/fix_wopen_ronly
Added the pmode argument to _wopen to fix the access permission on MinGW
2024-03-26 02:36:27 +09:00
Syoyo Fujita
b2c885f9c7 Merge pull request #480 from ptc-tgamper/bug/msft_lod_extension_used
Bug/msft lod extension used
2024-03-26 02:35:51 +09:00
Thomas Gamper
8248a41b9e tester.cc - extend MSFT_lod test 2024-03-25 17:01:08 +01:00
Thomas Gamper
eb5d4ade15 tiny_gltf.h - register MSFT_lod with the model's used extensions 2024-03-25 17:00:41 +01:00
TheDev
9f7af47e45 Added the pmode argument to _wopen to fix the access permission on MinGW 2024-03-25 18:21:20 +03:00
Syoyo Fujita
b7f07dc946 Merge pull request #475 from jam3sward/issue-474
M_PI was not defined by <cmath>
2024-03-20 21:11:56 +09:00
James Ward
e3b6789a7c M_PI was not defined by <cmath> 2024-03-19 19:06:19 +00:00
Syoyo Fujita
d9c2bbe90c Merge pull request #471 from ptc-tgamper/ftr/msft_lods
[Feature] Add basic support for MSFT_lod extension
2024-02-06 23:22:38 +09:00
Thomas Gamper
b8366a1c4b tester.cc - add test case for the crash fix in KHR_audio node serialization 2024-02-06 14:48:58 +01:00
Thomas Gamper
ec027b581b tester.cc - add MSFT_lods test case 2024-02-05 17:03:16 +01:00
Thomas Gamper
6bfcb71a0c tiny_gltf.h - add/remove MSFT_extension as required 2024-02-05 16:50:46 +01:00
Thomas Gamper
90e2995bad tiny_gltf.h - parse node and material lods 2024-02-05 15:42:49 +01:00
Syoyo Fujita
ffa2c665f9 Allow zero-sized BIN chunk. Fixes #440 2024-01-24 05:43:27 +09:00
Syoyo Fujita
29cfe103f8 Deprecate Travis CI and remove Travis CI scripts. Fixes #439 2023-12-11 21:00:35 +09:00
Syoyo Fujita
1f91e9b662 Merge pull request #467 from rhiskey/release
Small security and overflow patch
2023-12-05 06:42:19 +09:00
rhiskey
4df21f6df6 Update stb_image_write.h
Provided `sizeof(buffer)` in `sptintf`
2023-12-05 00:28:18 +03:00
rhiskey
ae7b113cad Revert back stb_image_write.h to original code 2023-12-05 00:21:41 +03:00
rhiskey
359cf50a0f Update stb_image_write.h
Fixed case when  `__STDC_LIB_EXT1__ ` is not defined - for Linux, etc. According to the C99 standart @syoyo
2023-12-05 00:15:50 +03:00
rhiskey
cf55186b0b Update tiny_gltf.h
Removed `#undef` 
and used the @syoyo method:

https://github.com/syoyo/tinygltf/pull/467#issuecomment-1838703699
2023-12-04 17:11:59 +03:00
rhiskey
808ab042e9 Update tiny_gltf.h
Fixed `Windows.h` MINMAX error and reverted to original numeric limits of type `uint32_t`
2023-12-04 16:57:36 +03:00
rhiskey
aa2fb04ab7 Merge branch 'syoyo:release' into release 2023-12-04 16:55:16 +03:00
Syoyo Fujita
354c1146ee Fix possible nullptr dereferencing.
Add missing `return false`
2023-12-04 22:50:49 +09:00
rhiskey
a68be31c9d Update stb_image_write.h
Securing printing output via `sprintf_s` instead `sprintf`.
2023-12-04 14:22:14 +03:00
rhiskey
46fd11cef1 Update tiny_gltf.h
Fix max size of `header_and_json_size` limit.
In case of 4GB will check ` sizeof(uint64_t)` insted deprecated max
2023-12-04 14:21:06 +03:00
Syoyo Fujita
fb3ff961f8 Merge pull request #465 from ptc-tgamper/bug/issue464
Fix Empty scenes are wrongly serialized as null Issue #464
2023-11-23 23:28:39 +09:00
Thomas Gamper
cef28f1791 Fix #464
tinygltf.h - serialize empty scenes as empty json objects; tester.cc - ad respective test case, bring test cases in correct order, tag test case for light index with correct issue number and fix it to compare to deserialozed model
2023-11-23 15:14:46 +01:00
Syoyo Fujita
cb4dcef520 Merge pull request #463 from ptc-tgamper/bug/issue458
Fix Light reference in the node issue #457
2023-11-23 22:19:05 +09:00
Thomas Gamper
46a1e426d1 fix #457
tiny_gltf.h - access correct json object when serializing a light, this fixes an assert and causes us to serialze the light index properly; tester.cc - add respective testcase
2023-11-23 14:13:12 +01:00
Syoyo Fujita
f54bbd847e Merge pull request #462 from ptc-tgamper/bug/issue457
Fix Empty nodes are wrongly serialized as null Issue #457
2023-11-23 21:52:47 +09:00
Thomas Gamper
439d103665 fix #457
tiny_gltf.h - make sure to serialize null node as empty object; tester.cc - add respective test case
2023-11-23 11:59:18 +01:00
Syoyo Fujita
a65d2d6de1 Merge pull request #460 from ptc-tgamper/bug/issue459
Fix Default constructed Material has wrong emissiveFactor #459
2023-11-23 02:21:28 +09:00
Thomas Gamper
d66d9d291b fix #459
tiny_gltf.h - use member initialization
2023-11-22 15:59:13 +01:00
Thomas Gamper
925d783330 fix #459
tiny_gltf.h - properly initialise emissiveFactor; tests/tester.cc - add test case
2023-11-22 14:17:46 +01:00
Syoyo Fujita
817ce1a9d0 Merge pull request #456 from haroonq/patch-1
Allow BufferView indices to be unspecified.
2023-10-10 21:24:51 +09:00
haroonq
8036d6a00f Allow BufferView indices to be unspecified.
Allow BufferView indices for element array buffers to be unspecified to support some extensions.

Note that this is similar to how invalid array buffers are handled in order to support, for example, sparse morph targets.
2023-10-09 10:30:25 +00:00
11 changed files with 2538 additions and 379 deletions

View File

@@ -1,10 +0,0 @@
#!/bin/bash
if [[ "$TRAVIS_OS_NAME" == "osx" ]]
then
brew upgrade
curl -o premake5.tar.gz https://github.com/premake/premake-core/releases/download/v5.0.0-alpha12/premake-5.0.0-alpha12-macosx.tar.gz
else
wget https://github.com/premake/premake-core/releases/download/v5.0.0-alpha12/premake-5.0.0-alpha12-linux.tar.gz -O premake5.tar.gz
fi
tar xzf premake5.tar.gz

View File

@@ -1,63 +0,0 @@
language: cpp
sudo: false
matrix:
include:
- addons: &1
apt:
sources:
- george-edison55-precise-backports
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-3.9
packages:
- g++-4.9
- clang-3.9
compiler: clang
env: COMPILER_VERSION=3.9 BUILD_TYPE=Debug
- addons: *1
compiler: clang
env: COMPILER_VERSION=3.9 BUILD_TYPE=Release
- addons: &2
apt:
sources:
- george-edison55-precise-backports
- ubuntu-toolchain-r-test
packages:
- g++-4.9
compiler: gcc
env: COMPILER_VERSION=4.9 BUILD_TYPE=Debug EXTRA_CXXFLAGS="-fsanitize=address"
- addons: *2
compiler: gcc
env: COMPILER_VERSION=4.9 BUILD_TYPE=Release EXTRA_CXXFLAGS="-fsanitize=address"
- addons: *1
compiler: clang
env: COMPILER_VERSION=3.9 BUILD_TYPE=Debug CFLAGS="-O0" CXXFLAGS="-O0"
- addons: &3
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
compiler: gcc
env: COMPILER_VERSION=4.8 BUILD_TYPE=Debug
- addons: *3
compiler: gcc
env: COMPILER_VERSION=4.8 BUILD_TYPE=Release
before_install:
- ./.travis-before-install.sh
script:
- export CC="${CC}-${COMPILER_VERSION}"
- export CXX="${CXX}-${COMPILER_VERSION}"
- ${CC} -v
- ${CXX} ${EXTRA_CXXFLAGS} -std=c++11 -Wall -g -o loader_example loader_example.cc
- ./loader_example ./models/Cube/Cube.gltf
- cd tests
- clang++ -v
- make
- ./tester
- ./tester_noexcept
- cd ../examples/raytrace
- ../../premake5 gmake
- make

34
AGENTS.md Normal file
View File

@@ -0,0 +1,34 @@
# Repository Guidelines
## Project Structure & Module Organization
- Core library lives in `tiny_gltf.h` (header-only) with `tiny_gltf.cc` provided for the amalgamated implementation flags. Keep public API updates localized and documented.
- Example viewers and utilities sit under `examples/`; use them as references for loading, validation, and WASM builds. Temporary build outputs belong in `build/` (git-ignored).
- Tests reside in `tests/` with sample assets in `data/` and `models/`; avoid committing generated binaries in `build/`, `tmp/`, or `tests/tester*`.
## Build, Test, and Development Commands
- Quick build of the loader example: `make` (uses clang++, C++11, optional `EXTRA_CXXFLAGS` for sanitizers).
- Unit tests: `cd tests && make && ./tester && ./tester_noexcept`.
- Parsing regression run: build `loader_example`, then `python test_runner.py` (requires local glTF-Sample-Models checkout and path update inside the script).
- CMake alternative: `cmake -S . -B build && cmake --build build` for IDE integration or non-clang toolchains.
- Lint header: `python deps/cpplint.py tiny_gltf.h`.
## Coding Style & Naming Conventions
- C++11, two-space indent, braces on the same line; mirror existing spacing and comment style in `tiny_gltf.h`.
- Prefer `std::` facilities and minimal dependencies; keep new symbols in the `tinygltf` namespace.
- Public API names stay PascalCase for types and camelCase for functions; keep enums/macros consistent with existing `TINYGLTF_*` patterns.
- Guard optional features with the established `TINYGLTF_*` defines; avoid introducing new globals without discussion.
## Testing Guidelines
- Framework: Catch2 single-header (`tests/catch.hpp`); add `TEST_CASE` blocks alongside related helpers in `tests/tester.cc`.
- Provide coverage for both exception-enabled and `TINYGLTF_NOEXCEPTION` builds; run both `tester` binaries before submitting.
- For new formats or parsing code, add assets under `tests/` or reference `data/` and note provenance.
## Commit & Pull Request Guidelines
- Commit messages: concise, present-tense imperatives mirroring existing history (e.g., “Add bounds check to images loaded from bufferviews”).
- PRs should describe the change, motivation, and testing (`tester`, `tester_noexcept`, fuzzing if relevant); link related issues.
- Include platform notes if behavior differs (Windows vs. POSIX, filesystem callbacks, WASM). Add before/after metrics when touching performance-sensitive paths.
## Security & Configuration Tips
- Handle external data defensively: validate buffer sizes, offsets, and URI handling; prefer bounded allocations.
- Keep optional callbacks (`fs::`, URI, image) robust against untrusted input; document new failure modes.
- Avoid committing sample assets with unclear licensing; reuse existing test fixtures where possible.

View File

@@ -16,6 +16,7 @@ option(TINYGLTF_BUILD_VALIDATOR_EXAMPLE "Build validator exampe" OFF)
option(TINYGLTF_BUILD_BUILDER_EXAMPLE "Build glTF builder example" OFF)
option(TINYGLTF_HEADER_ONLY "On: header-only mode. Off: create tinygltf library(No TINYGLTF_IMPLEMENTATION required in your project)" OFF)
option(TINYGLTF_INSTALL "Install tinygltf files during install step. Usually set to OFF if you include tinygltf through add_subdirectory()" ON)
option(TINYGLTF_INSTALL_VENDOR "Install vendored nlohmann/json and nothings/stb headers" ON)
if (TINYGLTF_BUILD_LOADER_EXAMPLE)
add_executable(loader_example
@@ -61,19 +62,26 @@ endif (TINYGLTF_HEADER_ONLY)
if (TINYGLTF_INSTALL)
install(TARGETS tinygltf EXPORT tinygltfTargets)
install(EXPORT tinygltfTargets NAMESPACE tinygltf:: FILE TinyGLTFTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
install(EXPORT tinygltfTargets NAMESPACE tinygltf:: FILE TinyGLTFTargets.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinygltf)
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/TinyGLTFConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/TinyGLTFConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/tinygltf)
# Do not install .lib even if !TINYGLTF_HEADER_ONLY
INSTALL ( FILES
json.hpp
stb_image.h
stb_image_write.h
tiny_gltf.h
${TINYGLTF_EXTRA_SOUECES}
DESTINATION
include
)
if(TINYGLTF_INSTALL_VENDOR)
INSTALL ( FILES
json.hpp
stb_image.h
stb_image_write.h
DESTINATION
include
)
endif()
endif(TINYGLTF_INSTALL)

View File

@@ -9,7 +9,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch (b
## Status
Currently TinyGLTF is stable and maintenance mode. No drastic changes and feature additions planned.
- v2.9.0 Various fixes and improvements. Filesystem callback API change.
- v2.8.0 Add URICallbacks for custom URI handling in Buffer and Image. PR#397
- v2.7.0 Change WriteImageDataFunction user callback function signature. PR#393
- v2.6.0 Support serializing sparse accessor(Thanks to @fynv).
@@ -26,8 +26,6 @@ Currently TinyGLTF is stable and maintenance mode. No drastic changes and featur
## Builds
[![Build Status](https://travis-ci.org/syoyo/tinygltf.svg?branch=devel)](https://travis-ci.org/syoyo/tinygltf)
[![Build status](https://ci.appveyor.com/api/projects/status/warngenu9wjjhlm8?svg=true)](https://ci.appveyor.com/project/syoyo/tinygltf)
![C/C++ CI](https://github.com/syoyo/tinygltf/workflows/C/C++%20CI/badge.svg)
@@ -109,7 +107,7 @@ WASI build example is located in [wasm](wasm) .
* [SanityEngine](https://github.com/DethRaid/SanityEngine) - A C++/D3D12 renderer focused on the personal and professional development of its developer
* [Open3D](http://www.open3d.org/) - A Modern Library for 3D Data Processing
* [Supernova Engine](https://github.com/supernovaengine/supernova) - Game engine for 2D and 3D projects with Lua or C++ in data oriented design.
* [Wicked Engine<img src="https://github.com/turanszkij/WickedEngine/blob/master/Content/logo_small.png" width="28px" align="center"/>](https://github.com/turanszkij/WickedEngine) - 3D engine with modern graphics
* [Wicked Engine<img src="https://github.com/turanszkij/WickedEngine/blob/master/Content/logo_small.png" width="28px" align="center"/>](https://github.com/turanszkij/WickedEngine) - 3D engine with modern graphics
* Your projects here! (Please send PR)
## TODOs
@@ -213,6 +211,11 @@ set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)
add_subdirectory(/path/to/tinygltf)
```
NOTE: Using tinygltf as a submodule doesn't automatically add the headers to your include path (as standard for many libraries). To get this functionality, add the following to the CMakeLists.txt file from above:
```
target_include_directories(${PROJECT_NAME} PRIVATE "/path/to/tinygltf")
```
### Saving gltTF 2.0 model

View File

@@ -788,8 +788,10 @@ static void QuatToAngleAxis(const std::vector<double> quaternion,
return;
}
constexpr double pi = 3.14159265358979323846;
double denom = sqrt(1-qw*qw);
outAngleDegrees = angleRadians * 180.0 / M_PI;
outAngleDegrees = angleRadians * 180.0 / pi;
axis[0] = qx / denom;
axis[1] = qy / denom;
axis[2] = qz / denom;

Binary file not shown.

View File

@@ -773,7 +773,7 @@ static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, f
#ifdef __STDC_LIB_EXT1__
len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#else
len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
len = snprintf(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#endif
s->func(s->context, buffer, len);

BIN
tests/issue-492.glb Normal file

Binary file not shown.

View File

@@ -4,7 +4,9 @@
#include "tiny_gltf.h"
// Nlohmann json(include ../json.hpp)
#if !defined(TINYGLTF_USE_INTERNAL_JSON) && !defined(TINYGLTF_USE_RAPIDJSON)
#include "json.hpp"
#endif
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
@@ -474,7 +476,7 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
}
REQUIRE(true == ret);
REQUIRE(err.empty());
REQUIRE(!warn.empty()); // relative image path won't exist in tests/
REQUIRE(warn.empty());
REQUIRE(saved.images.size() == model.images.size());
// The image uri in CubeImageUriMultipleSpaces.gltf is not encoded and
@@ -494,25 +496,23 @@ TEST_CASE("image-uri-spaces", "[issue-236]") {
}
TEST_CASE("serialize-empty-material", "[issue-294]") {
tinygltf::Model m;
tinygltf::Material mat;
mat.pbrMetallicRoughness.baseColorFactor = {1.0f, 1.0f, 1.0f, 1.0f}; // default baseColorFactor
m.materials.push_back(mat);
// Add default constructed material to model
m.materials.push_back({});
// Serialize model to output stream
std::stringstream os;
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
// use nlohmann json
// Parse serialized model
nlohmann::json j = nlohmann::json::parse(os.str());
// Serialized materials shall hold an empty object that
// represents the default constructed material
REQUIRE(j.find("materials") != j.end());
REQUIRE(j["materials"].is_array());
REQUIRE(1 == j["materials"].size());
REQUIRE(j["materials"][0].is_object());
CHECK(j["materials"][0].is_object());
CHECK(j["materials"][0].empty());
}
TEST_CASE("empty-skeleton-id", "[issue-321]") {
@@ -664,10 +664,11 @@ TEST_CASE("serialize-image-callback", "[issue-394]") {
auto writer = [](const std::string *basepath, const std::string *filename,
const tinygltf::Image *image, bool embedImages,
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
void *user_pointer) -> bool {
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
std::string *out_uri, void *user_pointer) -> bool {
(void)basepath;
(void)image;
(void)fs;
(void)uri_cb;
REQUIRE(*filename == "foo");
REQUIRE(embedImages == true);
@@ -701,12 +702,13 @@ TEST_CASE("serialize-image-failure", "[issue-394]") {
auto writer = [](const std::string *basepath, const std::string *filename,
const tinygltf::Image *image, bool embedImages,
const tinygltf::URICallbacks *uri_cb, std::string *out_uri,
void *user_pointer) -> bool {
const tinygltf::FsCallbacks* fs, const tinygltf::URICallbacks *uri_cb,
std::string *out_uri, void *user_pointer) -> bool {
(void)basepath;
(void)filename;
(void)image;
(void)embedImages;
(void)fs;
(void)uri_cb;
(void)out_uri;
(void)user_pointer;
@@ -757,3 +759,681 @@ TEST_CASE("load-issue-416-model", "[issue-416]") {
// external file load fails, but reading glTF itself is ok.
REQUIRE(true == ret);
}
TEST_CASE("reject-unsafe-paths", "[security]") {
tinygltf::TinyGLTF ctx;
SECTION("parent-directory reference is rejected") {
tinygltf::Model model;
std::string err;
std::string warn;
const std::string gltf = R"({
"asset": {"version": "2.0"},
"buffers": [
{"uri": "../secret.bin", "byteLength": 4}
]
})";
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, gltf.c_str(),
static_cast<unsigned int>(gltf.size()),
".");
REQUIRE_FALSE(ret);
REQUIRE_THAT(err, Catch::Contains("Rejected unsafe filename"));
}
SECTION("absolute path is rejected") {
tinygltf::Model model;
std::string err;
std::string warn;
const std::string gltf = R"({
"asset": {"version": "2.0"},
"buffers": [
{"uri": "/tmp/secret.bin", "byteLength": 4}
]
})";
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, gltf.c_str(),
static_cast<unsigned int>(gltf.size()),
".");
REQUIRE_FALSE(ret);
REQUIRE_THAT(err, Catch::Contains("Rejected unsafe filename"));
}
}
TEST_CASE("data-uri-size-limit", "[security]") {
tinygltf::TinyGLTF ctx;
ctx.SetMaxDataURISize(16); // small limit to exercise rejection path.
const std::string payload(24, 'A'); // decodes to 18 bytes.
const std::string data_uri =
"data:application/octet-stream;base64," + payload;
const std::string gltf = R"({
"asset": {"version": "2.0"},
"buffers": [
{"uri": ")" + data_uri + R"(", "byteLength": 18}
]
})";
tinygltf::Model model;
std::string err;
std::string warn;
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, gltf.c_str(),
static_cast<unsigned int>(gltf.size()),
".");
REQUIRE_FALSE(ret);
REQUIRE_THAT(err,
Catch::Contains("Data URI for buffer exceeds maximum allowed"));
SECTION("ceiling estimation rejects near-limit payloads") {
tinygltf::TinyGLTF ctx2;
ctx2.SetMaxDataURISize(4);
// 8 chars base64 -> 6 decoded bytes, should be rejected by max size.
const std::string small_payload(8, 'A');
const std::string small_uri =
"data:application/octet-stream;base64," + small_payload;
const std::string gltf_small = R"({
"asset": {"version": "2.0"},
"buffers": [
{"uri": ")" + small_uri + R"(", "byteLength": 6}
]
})";
tinygltf::Model m2;
std::string err2;
std::string warn2;
bool ok = ctx2.LoadASCIIFromString(
&m2, &err2, &warn2, gltf_small.c_str(),
static_cast<unsigned int>(gltf_small.size()), ".");
REQUIRE_FALSE(ok);
REQUIRE_THAT(err2,
Catch::Contains("Data URI for buffer exceeds maximum allowed"));
}
}
TEST_CASE("max-external-file-size", "[security]") {
tinygltf::TinyGLTF ctx;
ctx.SetMaxExternalFileSize(16); // small limit to force rejection.
const std::string gltf_path = "oversize.gltf";
{
std::ofstream ofs(gltf_path, std::ios::binary);
ofs << R"({"asset":{"version":"2.0"}})" << std::string(64, ' ');
}
tinygltf::Model model;
std::string err;
std::string warn;
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, gltf_path);
REQUIRE_FALSE(ok);
REQUIRE_THAT(err,
Catch::Contains("exceeds maximum allowed file size"));
std::remove(gltf_path.c_str());
const std::string glb_path = "oversize.glb";
{
std::ofstream ofs(glb_path, std::ios::binary);
ofs << std::string(32, '\0');
}
err.clear();
warn.clear();
ok = ctx.LoadBinaryFromFile(&model, &err, &warn, glb_path);
REQUIRE_FALSE(ok);
REQUIRE_THAT(err,
Catch::Contains("exceeds maximum allowed file size"));
std::remove(glb_path.c_str());
}
TEST_CASE("max-size-in-memory", "[security]") {
tinygltf::TinyGLTF ctx;
ctx.SetMaxExternalFileSize(16);
// LoadASCIIFromString should reject oversized input.
{
tinygltf::Model model;
std::string err;
std::string warn;
std::string large_json(20, ' ');
bool ok = ctx.LoadASCIIFromString(
&model, &err, &warn, large_json.c_str(),
static_cast<unsigned int>(large_json.size()), ".");
REQUIRE_FALSE(ok);
REQUIRE_THAT(err, Catch::Contains("Input size exceeds maximum"));
}
// LoadBinaryFromMemory should reject oversized input even before parsing.
{
tinygltf::Model model;
std::string err;
std::string warn;
std::array<unsigned char, 32> bytes{};
// Craft minimal glTF header; size still exceeds limit and should be
// rejected early.
bytes[0] = 'g'; bytes[1] = 'l'; bytes[2] = 'T'; bytes[3] = 'F';
bytes[4] = 2; // version little-endian
bool ok = ctx.LoadBinaryFromMemory(&model, &err, &warn, bytes.data(),
static_cast<unsigned int>(bytes.size()),
".");
REQUIRE_FALSE(ok);
REQUIRE_THAT(err, Catch::Contains("Input size exceeds maximum"));
}
SECTION("GLB length mismatch is rejected in strict mode") {
tinygltf::TinyGLTF ctx_strict;
ctx_strict.SetParseStrictness(tinygltf::ParseStrictness::Strict);
// Construct a minimal GLB with length field smaller than actual buffer.
std::array<unsigned char, 32> glb{};
glb[0] = 'g'; glb[1] = 'l'; glb[2] = 'T'; glb[3] = 'F';
glb[4] = 2; // version
uint32_t length = 24; // claimed length (smaller than actual buffer size)
memcpy(&glb[8], &length, 4);
uint32_t json_len = 4;
memcpy(&glb[12], &json_len, 4);
uint32_t json_fmt = 0x4E4F534A; // "JSON"
memcpy(&glb[16], &json_fmt, 4);
tinygltf::Model model;
std::string err;
std::string warn;
bool ok = ctx_strict.LoadBinaryFromMemory(
&model, &err, &warn, glb.data(),
static_cast<unsigned int>(glb.size()), ".");
REQUIRE_FALSE(ok);
REQUIRE_THAT(err,
Catch::Contains("Length field does not match data size"));
}
}
TEST_CASE("serialize-empty-node", "[issue-457]") {
tinygltf::Model m;
// Add default constructed node to model
m.nodes.push_back({});
// Add scene to model
m.scenes.push_back({});
// The scene's only node is the empty node
m.scenes.front().nodes.push_back(0);
// Serialize model to output stream
std::stringstream os;
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
// Parse serialized model
nlohmann::json j = nlohmann::json::parse(os.str());
// Serialized nodes shall hold an empty object that
// represents the default constructed node
REQUIRE(j.find("nodes") != j.end());
REQUIRE(j["nodes"].is_array());
REQUIRE(1 == j["nodes"].size());
CHECK(j["nodes"][0].is_object());
CHECK(j["nodes"][0].empty());
// We also want to make sure that the serialized scene
// is referencing the empty node.
// There shall be a single serialized scene
auto scenes = j.find("scenes");
REQUIRE(scenes != j.end());
REQUIRE(scenes->is_array());
REQUIRE(1 == scenes->size());
auto scene = scenes->at(0);
REQUIRE(scene.is_object());
// The scene's nodes array shall hold a reference
// to the single node
auto nodes = scene.find("nodes");
REQUIRE(nodes != scene.end());
REQUIRE(nodes->is_array());
REQUIRE(1 == nodes->size());
auto node = nodes->at(0);
CHECK(node.is_number_integer());
int idx = -1;
node.get_to(idx);
CHECK(0 == idx);
}
TEST_CASE("serialize-light-index", "[issue-458]") {
// Create the light
tinygltf::Light light;
light.type = "point";
light.intensity = 0.75;
light.color = std::vector<double>{1.0, 0.8, 0.95};
// Stream to serialize to
std::stringstream os;
{
tinygltf::Model m;
tinygltf::Scene scene;
// Add the light to the model
m.lights.push_back(light);
// Create a node that uses the light
tinygltf::Node node;
node.light = 0;
// Add the node to the model
m.nodes.push_back(node);
// Add the node to the scene
scene.nodes.push_back(0);
// Add the scene to the model
m.scenes.push_back(scene);
// Serialize model to output stream
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
}
{
tinygltf::Model m;
tinygltf::TinyGLTF ctx;
// Parse the serialized model
bool ok = ctx.LoadASCIIFromString(&m, nullptr, nullptr, os.str().c_str(), os.str().size(), "");
REQUIRE(true == ok);
// Check if the light was correctly serialized
REQUIRE(1 == m.lights.size());
CHECK(m.lights[0] == light);
// Check that the node properly references the light
REQUIRE(1 == m.nodes.size());
CHECK(m.nodes[0].light == 0);
}
}
TEST_CASE("default-material", "[issue-459]") {
const std::vector<double> default_emissive_factor{ 0.0, 0.0, 0.0 };
const std::vector<double> default_base_color_factor{ 1.0, 1.0, 1.0, 1.0 };
const std::string default_alpha_mode = "OPAQUE";
const double default_alpha_cutoff = 0.5;
const bool default_double_sided = false;
const double default_metallic_factor = 1.0;
const double default_roughness_factor = 1.0;
// Check that default constructed material
// holds actual default GLTF material properties
tinygltf::Material mat;
CHECK(mat.alphaMode == default_alpha_mode);
CHECK(mat.alphaCutoff == default_alpha_cutoff);
CHECK(mat.doubleSided == default_double_sided);
CHECK(mat.emissiveFactor == default_emissive_factor);
CHECK(mat.pbrMetallicRoughness.baseColorFactor == default_base_color_factor);
CHECK(mat.pbrMetallicRoughness.metallicFactor == default_metallic_factor);
CHECK(mat.pbrMetallicRoughness.roughnessFactor == default_roughness_factor);
// None of the textures should be set
CHECK(mat.normalTexture.index == -1);
CHECK(mat.occlusionTexture.index == -1);
CHECK(mat.emissiveTexture.index == -1);
}
TEST_CASE("serialize-empty-scene", "[issue-464]") {
// Stream to serialize to
std::stringstream os;
{
tinygltf::Model m;
// Add empty scene to the model
m.scenes.push_back({});
// Serialize model to output stream
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
}
{
tinygltf::Model m;
tinygltf::TinyGLTF ctx;
// Parse the serialized model
bool ok = ctx.LoadASCIIFromString(&m, nullptr, nullptr, os.str().c_str(), os.str().size(), "");
REQUIRE(true == ok);
// Make sure the empty scene is there
REQUIRE(1 == m.scenes.size());
tinygltf::Scene scene{};
// Check that the scene is empty
CHECK(m.scenes[0] == scene);
}
}
TEST_CASE("zero-sized-bin-chunk-glb", "[issue-440]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
// Input glb has zero-sized data in bin chunk(8 bytes for BIN chunk, and chunksize == 0)
// The spec https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#binary-buffer says
//
// When the binary buffer is empty or when it is stored by other means, this chunk SHOULD be omitted.
//
// 'SHOULD' mean 'RECOMMENDED', so we'll need to allow such zero-sized bin chunk is NOT omitted.
bool ret = ctx.LoadBinaryFromFile(&model, &err, &warn, "../models/regression/zero-sized-bin-chunk-issue-440.glb");
if (!warn.empty()) {
std::cout << "WARN: " << warn << "\n";
}
if (!err.empty()) {
std::cerr << err << std::endl;
}
REQUIRE(true == ret);
}
TEST_CASE("serialize-node-emitter", "[KHR_audio]") {
// Stream to serialize to
std::stringstream os;
{
tinygltf::Model m;
// Create a default audio emitter
m.audioEmitters.resize(1);
// Create a single node
m.nodes.resize(1);
// The node references the single emitter
m.nodes[0].emitter = 0;
// Create a single scene
m.scenes.resize(1);
// Make the scene reference the single node
m.scenes[0].nodes.push_back(0);
// Serialize model to output stream
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
}
{
tinygltf::Model m;
tinygltf::TinyGLTF ctx;
// Parse the serialized model
bool ok = ctx.LoadASCIIFromString(&m, nullptr, nullptr, os.str().c_str(), os.str().size(), "");
REQUIRE(true == ok);
// Make sure the single scene is there
REQUIRE(1 == m.scenes.size());
// Make sure all three nodes are there
REQUIRE(1 == m.nodes.size());
// Make sure the single root node of the scene is there
REQUIRE(1 == m.scenes[0].nodes.size());
REQUIRE(0 == m.scenes[0].nodes[0]);
// Retrieve the scene root node
const tinygltf::Node& node = m.nodes[m.scenes[0].nodes[0]];
// Make sure the single root node has both lod nodes
REQUIRE(0 == node.emitter);
}
}
TEST_CASE("serialize-lods", "[lods]") {
// Stream to serialize to
std::stringstream os;
{
tinygltf::Model m;
m.nodes.resize(4);
// Add Node 1 and Node 2 as lods to Node 0
m.nodes[0].lods.push_back(1);
m.nodes[0].lods.push_back(2);
// Add Material 1 and Material 2 as lods to Material 0
m.materials.resize(4);
m.materials[0].lods.push_back(1);
m.materials[0].lods.push_back(2);
tinygltf::Scene scene;
// Scene uses Node 0 and 3 as root node
scene.nodes.push_back(0);
scene.nodes.push_back(3);
// Add scene to the model
m.scenes.push_back(scene);
// Serialize model to output stream
tinygltf::TinyGLTF ctx;
bool ret = ctx.WriteGltfSceneToStream(&m, os, false, false);
REQUIRE(true == ret);
}
{
tinygltf::Model m;
tinygltf::TinyGLTF ctx;
// Parse the serialized model
bool ok = ctx.LoadASCIIFromString(&m, nullptr, nullptr, os.str().c_str(), os.str().size(), "");
REQUIRE(true == ok);
// Make sure the model's used extensions hold MSFT_lod
CHECK(m.extensionsUsed.size() == 1);
CHECK(m.extensionsUsed[0].compare("MSFT_lod") == 0);
// MSFT_lod is not a required extension
CHECK(m.extensionsRequired.size() == 0);
// Make sure all four materials are there
REQUIRE(4 == m.materials.size());
// Make sure the first material has both lod materials
REQUIRE(2 == m.materials[0].lods.size());
// Make sure the order is still the same after serialization and deserialization
CHECK(1 == m.materials[0].lods[0]);
CHECK(2 == m.materials[0].lods[1]);
// Make sure the material with lods exposes the MSFT_lod extension
CHECK(m.materials[0].extensions.size() == 1);
CHECK(m.materials[0].extensions.count("MSFT_lod") == 1);
// Make sure the last material has no lod materials
CHECK(0 == m.materials[3].lods.size());
// Make sure the material without lods does not exposes the MSFT_lod extension
CHECK(m.materials[3].extensions.size() == 0);
CHECK(m.materials[3].extensions.count("MSFT_lod") == 0);
// Make sure the single scene is there
REQUIRE(1 == m.scenes.size());
// Make sure all four nodes are there
REQUIRE(4 == m.nodes.size());
// Make sure the two root nodes of the scene are there
REQUIRE(2 == m.scenes[0].nodes.size());
REQUIRE(0 == m.scenes[0].nodes[0]);
REQUIRE(3 == m.scenes[0].nodes[1]);
// Retrieve the node with lods
const tinygltf::Node& nodeWithLods = m.nodes[m.scenes[0].nodes[0]];
// Make sure the node has both lod nodes
REQUIRE(2 == nodeWithLods.lods.size());
// Make sure the order is still the same after serialization and deserialization
CHECK(1 == nodeWithLods.lods[0]);
CHECK(2 == nodeWithLods.lods[1]);
// Make sure the node with lods exposes the MSFT_lod extension
CHECK(nodeWithLods.extensions.size() == 1);
CHECK(nodeWithLods.extensions.count("MSFT_lod") == 1);
// Retrieve the node without lods
const tinygltf::Node& nodeWithoutLods = m.nodes[m.scenes[0].nodes[1]];
// Make sure the node has no lod nodes
CHECK(0 == nodeWithoutLods.lods.size());
// Make sure the node without lods does not exposes the MSFT_lod extension
CHECK(nodeWithoutLods.extensions.size() == 0);
CHECK(nodeWithoutLods.extensions.count("MSFT_lod") == 0);
}
}
TEST_CASE("write-image-issue", "[issue-473]") {
std::string err;
std::string warn;
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
REQUIRE(model.images.size() == 2);
REQUIRE(model.images[0].uri == "Cube_BaseColor.png");
REQUIRE(model.images[1].uri == "Cube_MetallicRoughness.png");
REQUIRE_FALSE(model.images[0].image.empty());
REQUIRE_FALSE(model.images[1].image.empty());
ok = ctx.WriteGltfSceneToFile(&model, "Cube.gltf");
REQUIRE(ok);
for (const auto& image : model.images) {
std::fstream file(image.uri);
CHECK(file.good());
}
}
TEST_CASE("images-as-is", "[issue-487]") {
std::string err;
std::string warn;
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
ctx.SetImagesAsIs(true);
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
for (const auto& image : model.images) {
CHECK(image.as_is == true);
CHECK_FALSE(image.uri.empty());
CHECK_FALSE(image.image.empty());
#ifndef TINYGLTF_NO_STB_IMAGE
// Make sure we can decode the images
int w = -1, h = -1, component = -1;
unsigned char *data = stbi_load_from_memory(image.image.data(), static_cast<int>(image.image.size()), &w, &h, &component, 0);
CHECK(data != nullptr);
CHECK(w == 512);
CHECK(h == 512);
CHECK(component >= 3);
stbi_image_free(data);
#endif
}
// Write glTF model to disk, and images as separate files
{
ok = ctx.WriteGltfSceneToFile(&model, "Cube_with_image_files.gltf");
REQUIRE(ok);
// All the images should have been written to disk with their original data
for (const auto& image : model.images) {
// Make sure the image files exist
std::fstream file(image.uri);
CHECK(file.good());
#ifndef TINYGLTF_NO_STB_IMAGE
// Make sure we can load the images
int w = -1, h = -1, component = -1;
unsigned char *data = stbi_load(image.uri.c_str(), &w, &h, &component, 0);
CHECK(data != nullptr);
CHECK(w == 512);
CHECK(h == 512);
CHECK(component >= 3);
stbi_image_free(data);
#endif
}
}
// Write glTF model to disk, and embed images as data URIs
{
ok = ctx.WriteGltfSceneToFile(&model, "Cube_with_embedded_images.gltf", true, false);
REQUIRE(ok);
// Load above model again, and check if the images are loaded properly
tinygltf::Model embeddedImages;
ctx.SetImagesAsIs(false);
bool ok = ctx.LoadASCIIFromFile(&embeddedImages, &err, &warn, "Cube_with_embedded_images.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
for (const auto& image : embeddedImages.images) {
CHECK(image.as_is == false);
CHECK_FALSE(image.mimeType.empty());
CHECK_FALSE(image.image.empty());
CHECK(image.width == 512);
CHECK(image.height == 512);
CHECK(image.component >= 3);
}
}
// Write glTF model to disk, as GLB
{
ok = ctx.WriteGltfSceneToFile(&model, "Cube.glb", true, true, true, true);
REQUIRE(ok);
// Load above model again, and check if the images are loaded properly
tinygltf::Model glbModel;
ctx.SetImagesAsIs(false);
bool ok = ctx.LoadBinaryFromFile(&glbModel, &err, &warn, "Cube.glb");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
for (const auto& image : glbModel.images) {
CHECK(image.as_is == false);
CHECK_FALSE(image.mimeType.empty());
CHECK_FALSE(image.image.empty());
CHECK(image.width == 512);
CHECK(image.height == 512);
CHECK(image.component >= 3);
}
}
}
TEST_CASE("inverse-bind-matrices-optional", "[issue-492]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
bool ret = ctx.LoadBinaryFromFile(&model, &err, &warn, "issue-492.glb");
if (!warn.empty()) {
std::cout << "WARN:" << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "ERR:" << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(err.empty());
}
bool LoadImageData(tinygltf::Image * /* image */, const int /* image_idx */, std::string * /* err */,
std::string * /* warn */, int /* req_width */, int /* req_height */,
const unsigned char * /* bytes */, int /* size */, void * /*user_data */) {
return true;
}
bool WriteImageData(const std::string * /* basepath */, const std::string * /* filename */,
const tinygltf::Image *image, bool /* embedImages */,
const tinygltf::FsCallbacks * /* fs_cb */, const tinygltf::URICallbacks * /* uri_cb */,
std::string * /* out_uri */, void * user_pointer) {
REQUIRE(user_pointer != nullptr);
auto counter = static_cast<int*>(user_pointer);
*counter = *counter + 1;
return true;
}
TEST_CASE("empty-images-not-written", "[issue-495]") {
std::string err;
std::string warn;
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
ctx.SetImageLoader(LoadImageData, nullptr);
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
CHECK(model.images.size() == 2);
for (const auto& image : model.images) {
// No data loaded or decoded
CHECK(image.image.empty());
// The URI is kept
CHECK_FALSE(image.uri.empty());
// The URI should not be a data URI
CHECK(image.uri.find("data:") != 0);
}
// Now write the loaded model
int counter = 0;
ctx.SetImageWriter(WriteImageData, &counter);
ok = ctx.WriteGltfSceneToFile(&model, "issue-495-external.gltf");
CHECK(ok);
// WriteImageData should be invoked for both images
CHECK(counter == 2);
}

File diff suppressed because it is too large Load Diff