mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-08 11:13:50 +00:00
Compare commits
77 Commits
v2.9.2
...
copilot/im
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8fb6cad78 | ||
|
|
e2e40f58ae | ||
|
|
306c72fce9 | ||
|
|
12affdcc64 | ||
|
|
2c1a8be82d | ||
|
|
df3efc6453 | ||
|
|
99720ea0cc | ||
|
|
f9397d296d | ||
|
|
c4e4155bf7 | ||
|
|
5dfa17d14b | ||
|
|
5b87beb373 | ||
|
|
0ab7e74933 | ||
|
|
247cb388a0 | ||
|
|
eb087e80e7 | ||
|
|
690585fa73 | ||
|
|
73d309ebfa | ||
|
|
4d16d528a5 | ||
|
|
229f2b8c88 | ||
|
|
ad531900cb | ||
|
|
9da2046cba | ||
|
|
ed13b0422a | ||
|
|
1dfcb11442 | ||
|
|
a2b55f008e | ||
|
|
fdf528f9aa | ||
|
|
ebcd8cc4ee | ||
|
|
f6c71cf88b | ||
|
|
5aaa3e4daf | ||
|
|
1117aa7191 | ||
|
|
bdba4dfb4c | ||
|
|
e379d0d60c | ||
|
|
659de95977 | ||
|
|
b1a7736249 | ||
|
|
9ab0d0d5f7 | ||
|
|
fca5da1b37 | ||
|
|
bdc37385f1 | ||
|
|
797bf0e023 | ||
|
|
10ac914244 | ||
|
|
dc6dddac98 | ||
|
|
b548191e41 | ||
|
|
17287c7fcf | ||
|
|
6c948d5bc3 | ||
|
|
d4a4a1b27a | ||
|
|
3d5453ecd0 | ||
|
|
52a453120b | ||
|
|
fc6d78a1b6 | ||
|
|
ae0bac486c | ||
|
|
b19e665747 | ||
|
|
40f6c2b875 | ||
|
|
e8c70dff1d | ||
|
|
1dc37f76ea | ||
|
|
8da66b8ca1 | ||
|
|
81bd50c106 | ||
|
|
6d8bba0d8a | ||
|
|
2aa77e5d0a | ||
|
|
1fac6234d9 | ||
|
|
bcd666fbd4 | ||
|
|
37250b3470 | ||
|
|
7385235e29 | ||
|
|
3564b48760 | ||
|
|
2ad433b68f | ||
|
|
1b517f2b23 | ||
|
|
bd7255e095 | ||
|
|
a5e653e46c | ||
|
|
d530cd410b | ||
|
|
1831424c71 | ||
|
|
5e008af65d | ||
|
|
fbff1f45b5 | ||
|
|
d950e7cd9b | ||
|
|
116d0030f9 | ||
|
|
ff972dcf1b | ||
|
|
8bec431699 | ||
|
|
21485496b1 | ||
|
|
fda7422022 | ||
|
|
decfabd67e | ||
|
|
10b23b6af2 | ||
|
|
fe3cfbe996 | ||
|
|
3b73caa8e8 |
92
.github/instructions/copilot-instructions.md
vendored
Normal file
92
.github/instructions/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
# Copilot Review Instructions for TinyGLTF
|
||||
|
||||
This document provides guidelines for reviewing code changes in the TinyGLTF repository.
|
||||
|
||||
## Memory Safety
|
||||
|
||||
- **Buffer Overflows**: Check for proper bounds checking when accessing arrays, vectors, and buffers. Verify that buffer sizes are validated before read/write operations.
|
||||
- **Null Pointer Dereferences**: Ensure all pointers are checked for null before dereferencing, especially when handling optional glTF fields.
|
||||
- **Memory Leaks**: Verify proper resource management, including RAII patterns for file handles, image data, and dynamically allocated memory.
|
||||
- **Use-After-Free**: Check for proper lifetime management of objects, especially when dealing with callbacks and asynchronous operations.
|
||||
|
||||
## Error Handling
|
||||
|
||||
- **File I/O**: Verify that all file operations have proper error checking and meaningful error messages.
|
||||
- **JSON Parsing**: Ensure JSON parsing errors are caught and reported with helpful context about the location and nature of the error.
|
||||
- **Resource Loading**: Check that failures in loading images, buffers, and other resources are properly handled and don't cause crashes.
|
||||
- **Error Propagation**: Verify that errors are properly propagated through the call stack with appropriate error messages.
|
||||
|
||||
## glTF 2.0 Specification Compliance
|
||||
|
||||
- **Required Fields**: Ensure all required glTF fields are validated and present.
|
||||
- **Data Types**: Verify that data types match the glTF specification (e.g., component types, accessor types).
|
||||
- **Constraints**: Check that glTF constraints are enforced (e.g., valid ranges for enums, buffer stride requirements).
|
||||
- **Extensions**: Verify proper handling of glTF extensions and that unknown extensions are handled gracefully.
|
||||
- **Validation**: Ensure new features align with the glTF 2.0 specification from the Khronos Group.
|
||||
|
||||
## Cross-Platform Compatibility
|
||||
|
||||
- **Windows**: Check for proper handling of Windows-specific issues (path separators, line endings, file operations).
|
||||
- **Linux**: Verify compatibility with various Linux distributions and compilers (GCC, Clang).
|
||||
- **macOS**: Ensure macOS-specific considerations are addressed (case-sensitive filesystems, Clang compatibility).
|
||||
- **Mobile Platforms**: Consider Android and iOS compatibility where applicable.
|
||||
- **Endianness**: Verify proper handling of byte order when reading binary data.
|
||||
- **Compiler Compatibility**: Ensure code compiles with C++11 standard and supported compilers (MSVC, GCC, Clang).
|
||||
|
||||
## Edge Cases in glTF Parsing
|
||||
|
||||
- **Empty/Minimal Files**: Verify handling of minimal valid glTF files.
|
||||
- **Large Files**: Check for proper handling of large glTF files and buffers without memory exhaustion.
|
||||
- **Malformed Data**: Ensure graceful handling of malformed or invalid glTF data.
|
||||
- **Missing Optional Fields**: Verify correct behavior when optional glTF fields are absent.
|
||||
- **Edge Values**: Check handling of boundary values (e.g., maximum buffer sizes, extreme floating-point values).
|
||||
- **Base64 Encoding**: Verify proper handling of base64-encoded data URIs and invalid encodings.
|
||||
|
||||
## Backwards Compatibility
|
||||
|
||||
- **API Changes**: Ensure public API changes maintain backwards compatibility or are properly deprecated.
|
||||
- **Breaking Changes**: Flag any breaking changes for major version updates and document migration paths.
|
||||
- **Binary Compatibility**: Consider ABI stability for header-only library changes.
|
||||
- **Default Behavior**: Verify that default behavior of existing functionality remains unchanged.
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
- **Parsing Performance**: Check for unnecessary copies, redundant allocations, and inefficient algorithms in parsing logic.
|
||||
- **Memory Usage**: Verify efficient memory usage, especially when loading large glTF files.
|
||||
- **I/O Operations**: Ensure efficient file reading and minimize unnecessary disk access.
|
||||
- **String Operations**: Check for efficient string handling (use of string_view, move semantics).
|
||||
- **STL Usage**: Verify appropriate use of STL containers and algorithms.
|
||||
|
||||
## Documentation
|
||||
|
||||
- **Public API**: Ensure all public functions, classes, and methods have clear documentation comments.
|
||||
- **Parameters**: Verify that function parameters are documented, including expected ranges and constraints.
|
||||
- **Return Values**: Document return values and possible error conditions.
|
||||
- **Examples**: Check that complex features include usage examples.
|
||||
- **Changelog**: Verify that significant changes are documented in release notes or changelog.
|
||||
|
||||
## Testing
|
||||
|
||||
- **Test Coverage**: Ensure new features include appropriate unit tests or integration tests.
|
||||
- **Edge Cases**: Verify that tests cover edge cases and error conditions.
|
||||
- **Cross-Platform Tests**: Check that tests run on all supported platforms.
|
||||
- **Regression Tests**: Ensure bug fixes include regression tests to prevent recurrence.
|
||||
- **Sample Files**: Verify that changes are tested with various valid and invalid glTF sample files.
|
||||
|
||||
## Code Style Consistency
|
||||
|
||||
- **Header-Only Pattern**: Maintain the header-only library structure.
|
||||
- **Naming Conventions**: Follow existing naming conventions (CamelCase for types, snake_case for functions where applicable).
|
||||
- **Formatting**: Adhere to the existing code formatting style (check `.clang-format` if available).
|
||||
- **Include Guards**: Verify proper include guards and header organization.
|
||||
- **Namespace Usage**: Ensure proper use of the `tinygltf` namespace.
|
||||
- **Comments**: Maintain consistent comment style with existing code.
|
||||
- **C++11 Compliance**: Verify that code uses C++11 features appropriately and doesn't require newer standards unless specified.
|
||||
|
||||
## Additional Considerations
|
||||
|
||||
- **Third-Party Dependencies**: Minimize new dependencies; prefer existing dependencies (json.hpp, stb_image).
|
||||
- **Warnings**: Ensure code compiles without warnings on supported compilers.
|
||||
- **const Correctness**: Verify proper use of const for parameters and methods.
|
||||
- **RAII**: Prefer RAII patterns for resource management over manual cleanup.
|
||||
- **noexcept**: Use noexcept appropriately for move constructors and move assignment operators.
|
||||
330
.github/workflows/ci.yml
vendored
Normal file
330
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,330 @@
|
||||
name: Comprehensive CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- release
|
||||
- devel
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- release
|
||||
- devel
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
# Linux x64 - GCC
|
||||
linux-gcc-x64:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux x64 - Clang 21
|
||||
linux-clang-x64:
|
||||
runs-on: ubuntu-24.04
|
||||
name: Linux x64 (Clang 21)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Clang 21
|
||||
run: |
|
||||
wget https://apt.llvm.org/llvm.sh
|
||||
chmod +x llvm.sh
|
||||
sudo ./llvm.sh 21
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
cmake -B build -DCMAKE_C_COMPILER=clang-21 -DCMAKE_CXX_COMPILER=clang++-21 -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux ARM64 - GCC (native)
|
||||
linux-arm64:
|
||||
runs-on: ubuntu-24.04-arm
|
||||
name: Linux ARM64 (GCC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# macOS ARM64 Apple Silicon
|
||||
macos-arm64:
|
||||
runs-on: macos-14
|
||||
name: macOS ARM64 Apple Silicon (Clang)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: ./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Windows x64 - MSVC
|
||||
windows-msvc-x64:
|
||||
runs-on: windows-latest
|
||||
name: Windows x64 (MSVC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A x64 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
.\build\Release\loader_example.exe models\Cube\Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
|
||||
# Windows x86 - MSVC
|
||||
windows-msvc-x86:
|
||||
runs-on: windows-latest
|
||||
name: Windows x86 (MSVC)
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A Win32 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build -C Release --output-on-failure
|
||||
|
||||
# Windows ARM64 - MSVC (cross-compile)
|
||||
windows-msvc-arm64:
|
||||
runs-on: windows-latest
|
||||
name: Windows ARM64 (MSVC) - Cross-compile
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 17 2022" -A ARM64 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=Off ..
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
# Windows MinGW - MSYS2
|
||||
windows-mingw-msys2:
|
||||
runs-on: windows-latest
|
||||
name: Windows x64 (MinGW MSYS2)
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup MSYS2
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: UCRT64
|
||||
install: base-devel
|
||||
pacboy: >-
|
||||
cc:p cmake:p ninja:p
|
||||
update: true
|
||||
release: false
|
||||
|
||||
- name: Build with CMake
|
||||
run: |
|
||||
cmake -G"Ninja" -S . -B build
|
||||
cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Linux -> Windows MinGW Cross-compile
|
||||
linux-mingw-cross:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux→Windows (MinGW Cross) - Build Only
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install MinGW
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential mingw-w64
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
x86_64-w64-mingw32-g++ -std=c++11 -o loader_example.exe loader_example.cc
|
||||
|
||||
# Special Configuration: No Exceptions
|
||||
linux-noexception:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - No Exceptions
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example
|
||||
run: |
|
||||
g++ -DTINYGLTF_NOEXCEPTION -std=c++11 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests
|
||||
run: |
|
||||
cd tests
|
||||
g++ -DTINYGLTF_NOEXCEPTION -I../ -std=c++11 -g -O0 -o tester_noexcept tester.cc
|
||||
./tester_noexcept
|
||||
|
||||
# Special Configuration: Header-Only Mode
|
||||
linux-header-only:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - Header-Only Mode
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build with CMake Header-Only
|
||||
run: |
|
||||
mkdir build
|
||||
cmake -B build -DTINYGLTF_HEADER_ONLY=ON -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON
|
||||
cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Special Configuration: RapidJSON Backend
|
||||
linux-rapidjson:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (GCC) - RapidJSON Backend
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Clone RapidJSON
|
||||
run: |
|
||||
git clone https://github.com/Tencent/rapidjson
|
||||
|
||||
- name: Configure
|
||||
run: |
|
||||
cmake -B build -DTINYGLTF_USE_RAPIDJSON=ON -DTINYGLTF_BUILD_LOADER_EXAMPLE=ON -DCMAKE_PREFIX_PATH=$PWD/rapidjson
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./build/loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Run tests
|
||||
run: ctest --test-dir build --output-on-failure
|
||||
|
||||
# Special Configuration: AddressSanitizer
|
||||
linux-asan:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (Clang) - AddressSanitizer
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example with ASan
|
||||
run: |
|
||||
clang++ -fsanitize=address -std=c++11 -g -O1 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests with ASan
|
||||
run: |
|
||||
cd tests
|
||||
clang++ -fsanitize=address -I../ -std=c++11 -g -O1 -o tester tester.cc
|
||||
./tester
|
||||
|
||||
# Special Configuration: UndefinedBehaviorSanitizer
|
||||
linux-ubsan:
|
||||
runs-on: ubuntu-latest
|
||||
name: Linux x64 (Clang) - UndefinedBehaviorSanitizer
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Build loader_example with UBSan
|
||||
run: |
|
||||
clang++ -fsanitize=undefined -std=c++11 -g -O1 -o loader_example loader_example.cc
|
||||
|
||||
- name: Run loader_example
|
||||
run: |
|
||||
./loader_example models/Cube/Cube.gltf
|
||||
|
||||
- name: Build and run unit tests with UBSan
|
||||
run: |
|
||||
cd tests
|
||||
clang++ -fsanitize=undefined -I../ -std=c++11 -g -O1 -o tester tester.cc
|
||||
./tester
|
||||
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
# CMake
|
||||
/build/
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
@@ -68,8 +69,22 @@ imgui.ini
|
||||
loader_example
|
||||
tests/tester
|
||||
tests/tester_noexcept
|
||||
tests/tester_intensive_customjson
|
||||
tests/issue-97.gltf
|
||||
tests/issue-261.gltf
|
||||
tests/issue-495-external.gltf
|
||||
# Test-generated output files (written by tester.cc during test run)
|
||||
tests/Cube.gltf
|
||||
tests/Cube.bin
|
||||
tests/Cube.glb
|
||||
tests/Cube_BaseColor.png
|
||||
tests/Cube_MetallicRoughness.png
|
||||
tests/Cube_with_embedded_images.gltf
|
||||
tests/Cube_with_image_files.gltf
|
||||
tests/tmp.glb
|
||||
tests/ issue-236.gltf
|
||||
tests/ issue-236.bin
|
||||
tests/ 2x2 image has multiple spaces.png
|
||||
|
||||
# unignore
|
||||
!Makefile
|
||||
|
||||
@@ -14,8 +14,11 @@ option(TINYGLTF_BUILD_LOADER_EXAMPLE "Build loader_example(load glTF and dump in
|
||||
option(TINYGLTF_BUILD_GL_EXAMPLES "Build GL exampels(requires glfw, OpenGL, etc)" OFF)
|
||||
option(TINYGLTF_BUILD_VALIDATOR_EXAMPLE "Build validator exampe" OFF)
|
||||
option(TINYGLTF_BUILD_BUILDER_EXAMPLE "Build glTF builder example" OFF)
|
||||
option(TINYGLTF_BUILD_TESTS "Build unit tests" 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)
|
||||
option(TINYGLTF_USE_CUSTOM_JSON "Use the built-in fast JSON parser (tinygltf_json.h) instead of nlohmann/json" OFF)
|
||||
|
||||
if (TINYGLTF_BUILD_LOADER_EXAMPLE)
|
||||
add_executable(loader_example
|
||||
@@ -36,6 +39,34 @@ if (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
add_subdirectory ( examples/build-gltf )
|
||||
endif (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
|
||||
if (TINYGLTF_BUILD_TESTS)
|
||||
enable_testing()
|
||||
add_executable(tester tests/tester.cc)
|
||||
target_include_directories(tester PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests
|
||||
)
|
||||
add_test(NAME tester COMMAND tester WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
|
||||
# Build and run tests with the custom JSON backend enabled to catch regressions
|
||||
add_executable(tester_customjson tests/tester.cc)
|
||||
target_include_directories(tester_customjson PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests
|
||||
)
|
||||
target_compile_definitions(tester_customjson PRIVATE TINYGLTF_USE_CUSTOM_JSON)
|
||||
add_test(NAME tester_customjson COMMAND tester_customjson WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
|
||||
# Intensive parser tests for the custom JSON backend
|
||||
add_executable(tester_intensive_customjson tests/tester_intensive_customjson.cc)
|
||||
target_include_directories(tester_intensive_customjson PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests
|
||||
)
|
||||
target_compile_definitions(tester_intensive_customjson PRIVATE TINYGLTF_USE_CUSTOM_JSON)
|
||||
add_test(NAME tester_intensive_customjson COMMAND tester_intensive_customjson WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
endif (TINYGLTF_BUILD_TESTS)
|
||||
|
||||
#
|
||||
# for add_subdirectory and standalone build
|
||||
#
|
||||
@@ -59,21 +90,37 @@ else (TINYGLTF_HEADER_ONLY)
|
||||
)
|
||||
endif (TINYGLTF_HEADER_ONLY)
|
||||
|
||||
if (TINYGLTF_USE_CUSTOM_JSON)
|
||||
if (TINYGLTF_HEADER_ONLY)
|
||||
target_compile_definitions(tinygltf INTERFACE TINYGLTF_USE_CUSTOM_JSON)
|
||||
else ()
|
||||
target_compile_definitions(tinygltf PUBLIC TINYGLTF_USE_CUSTOM_JSON)
|
||||
endif ()
|
||||
endif ()
|
||||
|
||||
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_json.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)
|
||||
|
||||
16
README.md
16
README.md
@@ -26,8 +26,6 @@ Currently TinyGLTF is stable and maintenance mode. No drastic changes and featur
|
||||
|
||||
## Builds
|
||||
|
||||
[](https://ci.appveyor.com/project/syoyo/tinygltf)
|
||||
|
||||

|
||||
|
||||
## Features
|
||||
@@ -159,9 +157,10 @@ Model model;
|
||||
TinyGLTF loader;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
std::string filename = "input.gltf";
|
||||
|
||||
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, argv[1]);
|
||||
//bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, argv[1]); // for binary glTF(.glb)
|
||||
bool ret = loader.LoadASCIIFromFile(&model, &err, &warn, filename);
|
||||
//bool ret = loader.LoadBinaryFromFile(&model, &err, &warn, filename); // for binary glTF(.glb)
|
||||
|
||||
if (!warn.empty()) {
|
||||
printf("Warn: %s\n", warn.c_str());
|
||||
@@ -172,8 +171,7 @@ if (!err.empty()) {
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
printf("Failed to parse glTF\n");
|
||||
return -1;
|
||||
printf("Failed to parse glTF: %s\n", filename.c_str());
|
||||
}
|
||||
```
|
||||
|
||||
@@ -194,7 +192,6 @@ if (!ret) {
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE `: Disable including `stb_image.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE `: Disable including `stb_image_write.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_USE_RAPIDJSON` : Use RapidJSON as a JSON parser/serializer. RapidJSON files are not included in TinyGLTF repo. Please set an include path to RapidJSON if you enable this feature.
|
||||
* `TINYGLTF_USE_CPP14` : Use C++14 feature(requires C++14 compiler). This may give better performance than C++11.
|
||||
|
||||
|
||||
## CMake options
|
||||
@@ -211,6 +208,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
|
||||
|
||||
|
||||
18
appveyor.yml
18
appveyor.yml
@@ -1,18 +0,0 @@
|
||||
version: 0.9.{build}
|
||||
|
||||
image:
|
||||
- Visual Studio 2015
|
||||
|
||||
# scripts that runs after repo cloning.
|
||||
install:
|
||||
- vcsetup.bat
|
||||
|
||||
platform: x64
|
||||
configuration: Release
|
||||
|
||||
build:
|
||||
parallel: true
|
||||
project: TinyGLTFSolution.sln
|
||||
|
||||
after_build:
|
||||
- examples.bat
|
||||
@@ -4,3 +4,4 @@
|
||||
all: ../tiny_gltf.h
|
||||
clang++ -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester tester.cc
|
||||
clang++ -DTINYGLTF_NOEXCEPTION -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester_noexcept tester.cc
|
||||
clang++ -DTINYGLTF_USE_CUSTOM_JSON -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester_intensive_customjson tester_intensive_customjson.cc
|
||||
|
||||
@@ -4,9 +4,14 @@ Do fuzzing test for TinyGLTF API.
|
||||
|
||||
## Supported API
|
||||
|
||||
* [x] LoadASCIIFromMemory
|
||||
* [x] LoadASCIIFromString
|
||||
* [ ] LoadBinaryFromMemory
|
||||
|
||||
### Custom JSON backend (`tinygltf_json.h`)
|
||||
|
||||
* [x] LoadASCIIFromString
|
||||
* [x] LoadBinaryFromMemory
|
||||
|
||||
## Requirements
|
||||
|
||||
* meson
|
||||
@@ -36,11 +41,17 @@ $ cd build
|
||||
$ ninja
|
||||
```
|
||||
|
||||
This builds two fuzzers:
|
||||
|
||||
* `fuzz_gltf` – default nlohmann/json backend
|
||||
* `fuzz_gltf_customjson` – custom `tinygltf_json.h` backend (tests both ASCII and binary parsing paths)
|
||||
|
||||
## How to run
|
||||
|
||||
Increase memory limit. e.g. `-rss_limit_mb=50000`
|
||||
|
||||
```
|
||||
$ ./fuzz_gltf -rss_limit_mb=20000 -jobs 4
|
||||
$ ./fuzz_gltf_customjson -rss_limit_mb=20000 -jobs 4
|
||||
```
|
||||
|
||||
|
||||
76
tests/fuzzer/fuzz_gltf_customjson.cc
Normal file
76
tests/fuzzer/fuzz_gltf_customjson.cc
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* LLVM libFuzzer harness for tinygltf with the custom JSON backend
|
||||
* (tinygltf_json.h).
|
||||
*
|
||||
* Exercises:
|
||||
* 1. LoadASCIIFromString – glTF JSON parsing
|
||||
* 2. LoadBinaryFromMemory – GLB binary parsing
|
||||
*
|
||||
* Build (clang with libFuzzer):
|
||||
* clang++ -std=c++11 -fsanitize=address,fuzzer \
|
||||
* -DTINYGLTF_USE_CUSTOM_JSON \
|
||||
* -I../../ fuzz_gltf_customjson.cc \
|
||||
* -o fuzz_gltf_customjson
|
||||
*
|
||||
* Run:
|
||||
* ./fuzz_gltf_customjson -rss_limit_mb=20000 -jobs 4
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#define TINYGLTF_IMPLEMENTATION
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
#define TINYGLTF_USE_CUSTOM_JSON
|
||||
#endif
|
||||
#include "tiny_gltf.h"
|
||||
|
||||
/* Fuzz the ASCII (JSON) parser path */
|
||||
static void fuzz_ascii(const uint8_t *data, size_t size) {
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
const char *str = reinterpret_cast<const char *>(data);
|
||||
|
||||
bool ret =
|
||||
ctx.LoadASCIIFromString(&model, &err, &warn, str,
|
||||
static_cast<unsigned int>(size), /* base_dir */ "");
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
/* Fuzz the binary (GLB) parser path */
|
||||
static void fuzz_binary(const uint8_t *data, size_t size) {
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
bool ret = ctx.LoadBinaryFromMemory(&model, &err, &warn, data,
|
||||
static_cast<unsigned int>(size),
|
||||
/* base_dir */ "");
|
||||
(void)ret;
|
||||
}
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
if (size == 0) return 0;
|
||||
|
||||
/* Use the lowest bit of the first byte to select the parse path.
|
||||
* The remaining bits are left for the fuzzer engine to explore;
|
||||
* additional paths (e.g. LoadASCIIFromFile, check_sections flags)
|
||||
* can be added here in the future using more selector bits. */
|
||||
uint8_t selector = data[0];
|
||||
const uint8_t *payload = data + 1;
|
||||
size_t payload_size = size - 1;
|
||||
|
||||
if (selector & 1) {
|
||||
fuzz_binary(payload, payload_size);
|
||||
} else {
|
||||
fuzz_ascii(payload, payload_size);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -7,3 +7,9 @@ executable('fuzz_gltf',
|
||||
cpp_args : '-fsanitize=address,fuzzer',
|
||||
link_args : '-fsanitize=address,fuzzer' )
|
||||
|
||||
executable('fuzz_gltf_customjson',
|
||||
'fuzz_gltf_customjson.cc',
|
||||
include_directories : incdirs,
|
||||
cpp_args : ['-fsanitize=address,fuzzer', '-DTINYGLTF_USE_CUSTOM_JSON'],
|
||||
link_args : '-fsanitize=address,fuzzer' )
|
||||
|
||||
|
||||
@@ -1121,8 +1121,10 @@ TEST_CASE("images-as-is", "[issue-487]") {
|
||||
// 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());
|
||||
{
|
||||
std::fstream file(image.uri);
|
||||
CHECK(file.good());
|
||||
} // Close file before stbi_load (Windows sharing violation fix)
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Make sure we can load the images
|
||||
int w = -1, h = -1, component = -1;
|
||||
@@ -1199,4 +1201,51 @@ TEST_CASE("inverse-bind-matrices-optional", "[issue-492]") {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
1089
tests/tester_intensive_customjson.cc
Normal file
1089
tests/tester_intensive_customjson.cc
Normal file
File diff suppressed because it is too large
Load Diff
99
tiny_gltf.h
99
tiny_gltf.h
@@ -50,12 +50,6 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// Auto-detect C++14 standard version
|
||||
#if !defined(TINYGLTF_USE_CPP14) && defined(__cplusplus) && \
|
||||
(__cplusplus >= 201402L)
|
||||
#define TINYGLTF_USE_CPP14
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#ifdef TINYGLTF_ANDROID_LOAD_FROM_ASSETS
|
||||
#include <android/asset_manager.h>
|
||||
@@ -834,20 +828,20 @@ struct Accessor {
|
||||
maxValues; // optional. integer value is promoted to double
|
||||
|
||||
struct Sparse {
|
||||
int count;
|
||||
bool isSparse;
|
||||
int count{0};
|
||||
bool isSparse{false};
|
||||
struct {
|
||||
size_t byteOffset;
|
||||
int bufferView;
|
||||
int componentType; // a TINYGLTF_COMPONENT_TYPE_ value
|
||||
size_t byteOffset{0};
|
||||
int bufferView{-1};
|
||||
int componentType{-1}; // a TINYGLTF_COMPONENT_TYPE_ value
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
std::string extras_json_string;
|
||||
std::string extensions_json_string;
|
||||
} indices;
|
||||
struct {
|
||||
int bufferView;
|
||||
size_t byteOffset;
|
||||
int bufferView{-1};
|
||||
size_t byteOffset{0};
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
std::string extras_json_string;
|
||||
@@ -898,11 +892,7 @@ struct Accessor {
|
||||
// unreachable return 0;
|
||||
}
|
||||
|
||||
Accessor()
|
||||
|
||||
{
|
||||
sparse.isSparse = false;
|
||||
}
|
||||
Accessor() = default;
|
||||
DEFAULT_METHODS(Accessor)
|
||||
bool operator==(const tinygltf::Accessor &) const;
|
||||
};
|
||||
@@ -1721,7 +1711,11 @@ class TinyGLTF {
|
||||
#endif // __GNUC__
|
||||
|
||||
#ifndef TINYGLTF_NO_INCLUDE_JSON
|
||||
#ifndef TINYGLTF_USE_RAPIDJSON
|
||||
#ifdef TINYGLTF_USE_CUSTOM_JSON
|
||||
#ifndef TINYGLTF_NO_INCLUDE_CUSTOM_JSON
|
||||
#include "tinygltf_json.h"
|
||||
#endif
|
||||
#elif !defined(TINYGLTF_USE_RAPIDJSON)
|
||||
#include "json.hpp"
|
||||
#else
|
||||
#ifndef TINYGLTF_NO_INCLUDE_RAPIDJSON
|
||||
@@ -1809,7 +1803,10 @@ class TinyGLTF {
|
||||
|
||||
namespace tinygltf {
|
||||
namespace detail {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
#ifdef TINYGLTF_USE_CUSTOM_JSON
|
||||
// Types and JsonParse are provided by tinygltf_json.h (already included above)
|
||||
// via 'namespace tinygltf { namespace detail { ... } }' declarations.
|
||||
#elif defined(TINYGLTF_USE_RAPIDJSON)
|
||||
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON_CRTALLOCATOR
|
||||
// This uses the RapidJSON CRTAllocator. It is thread safe and multiple
|
||||
@@ -1881,6 +1878,7 @@ using json_const_array_iterator = json_const_iterator;
|
||||
using JsonDocument = json;
|
||||
#endif
|
||||
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
void JsonParse(JsonDocument &doc, const char *str, size_t length,
|
||||
bool throwExc = false) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -1890,6 +1888,7 @@ void JsonParse(JsonDocument &doc, const char *str, size_t length,
|
||||
doc = detail::json::parse(str, str + length, nullptr, throwExc);
|
||||
#endif
|
||||
}
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
} // namespace detail
|
||||
} // namespace tinygltf
|
||||
|
||||
@@ -2461,9 +2460,8 @@ inline unsigned char from_hex(unsigned char ch) {
|
||||
}
|
||||
|
||||
static const std::string urldecode(const std::string &str) {
|
||||
using namespace std;
|
||||
string result;
|
||||
string::size_type i;
|
||||
std::string result;
|
||||
std::string::size_type i;
|
||||
for (i = 0; i < str.size(); ++i) {
|
||||
if (str[i] == '+') {
|
||||
result += ' ';
|
||||
@@ -2767,6 +2765,12 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
const Image *image, bool embedImages,
|
||||
const FsCallbacks* fs_cb, const URICallbacks *uri_cb,
|
||||
std::string *out_uri, void *) {
|
||||
// Early out on empty images, report the original uri if the image was not written.
|
||||
if (image->image.empty()) {
|
||||
*out_uri = *filename;
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::string ext = GetFilePathExtension(*filename);
|
||||
|
||||
// Write image to temporary buffer
|
||||
@@ -3309,11 +3313,12 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||
filename = std::to_string(index) + "." + ext;
|
||||
}
|
||||
|
||||
// If callback is set and image data exists, modify image data object. If
|
||||
// image data does not exist, this is not considered a failure and the
|
||||
// original uri should be maintained.
|
||||
// If callback is set, modify image data object.
|
||||
// Note that the callback is also invoked for images without data.
|
||||
// The default callback implementation simply returns true for
|
||||
// empty images and sets the out URI to filename.
|
||||
bool imageWritten = false;
|
||||
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
|
||||
if (WriteImageData != nullptr && !filename.empty()) {
|
||||
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
|
||||
fs_cb, uri_cb, out_uri, user_data);
|
||||
if (!imageWritten) {
|
||||
@@ -3440,6 +3445,7 @@ bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
namespace detail {
|
||||
bool GetInt(const detail::json &o, int &val) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -3663,6 +3669,7 @@ std::string JsonToString(const detail::json &o, int spacing = -1) {
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
|
||||
static bool ParseJsonAsValue(Value *ret, const detail::json &o) {
|
||||
Value val{};
|
||||
@@ -4355,7 +4362,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
// Just only save some information here. Loading actual image data from
|
||||
// bufferView is done after this `ParseImage` function.
|
||||
image->bufferView = bufferView;
|
||||
image->mimeType = mime_type;
|
||||
image->mimeType = std::move( mime_type );
|
||||
image->width = width;
|
||||
image->height = height;
|
||||
|
||||
@@ -4387,7 +4394,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
}
|
||||
} else {
|
||||
// Assume external file
|
||||
// Keep texture path (for textures that cannot be decoded)
|
||||
// Unconditionally keep the external URI of the image
|
||||
image->uri = uri;
|
||||
#ifdef TINYGLTF_NO_EXTERNAL_IMAGE
|
||||
return true;
|
||||
@@ -4434,6 +4441,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return LoadImageData(image, image_idx, err, warn, 0, 0, &img.at(0),
|
||||
static_cast<int>(img.size()), load_image_user_data);
|
||||
}
|
||||
@@ -5253,7 +5261,7 @@ static bool ParseNode(Node *node, std::string *err, const detail::json &o,
|
||||
if (node->extensions.count("MSFT_lod") != 0) {
|
||||
auto const &msft_lod_ext = node->extensions["MSFT_lod"];
|
||||
if (msft_lod_ext.Has("ids")) {
|
||||
auto idsArr = msft_lod_ext.Get("ids");
|
||||
const auto &idsArr = msft_lod_ext.Get("ids");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5282,7 +5290,7 @@ static bool ParseScene(Scene *scene, std::string *err, const detail::json &o,
|
||||
if (scene->extensions.count("KHR_audio") != 0) {
|
||||
auto const &audio_ext = scene->extensions["KHR_audio"];
|
||||
if (audio_ext.Has("emitters")) {
|
||||
auto emittersArr = audio_ext.Get("emitters");
|
||||
const auto &emittersArr = audio_ext.Get("emitters");
|
||||
for (size_t i = 0; i < emittersArr.ArrayLen(); ++i) {
|
||||
scene->audioEmitters.emplace_back(emittersArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5318,7 +5326,7 @@ static bool ParsePbrMetallicRoughness(
|
||||
}
|
||||
return false;
|
||||
}
|
||||
pbr->baseColorFactor = baseColorFactor;
|
||||
pbr->baseColorFactor = std::move( baseColorFactor );
|
||||
}
|
||||
|
||||
{
|
||||
@@ -5470,7 +5478,7 @@ static bool ParseMaterial(Material *material, std::string *err, std::string *war
|
||||
if (material->extensions.count("MSFT_lod") != 0) {
|
||||
auto const &msft_lod_ext = material->extensions["MSFT_lod"];
|
||||
if (msft_lod_ext.Has("ids")) {
|
||||
auto idsArr = msft_lod_ext.Get("ids");
|
||||
const auto &idsArr = msft_lod_ext.Get("ids");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
@@ -5597,7 +5605,7 @@ static bool ParseAnimation(Animation *animation, std::string *err,
|
||||
}
|
||||
sampler.input = inputIndex;
|
||||
sampler.output = outputIndex;
|
||||
ParseExtrasAndExtensions(&sampler, err, o,
|
||||
ParseExtrasAndExtensions(&sampler, err, s,
|
||||
store_original_json_for_extras_and_extensions);
|
||||
|
||||
animation->samplers.emplace_back(std::move(sampler));
|
||||
@@ -6445,6 +6453,15 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
return false;
|
||||
}
|
||||
const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
|
||||
if (bufferView.byteOffset >= buffer.data.size()) {
|
||||
if (err) {
|
||||
std::stringstream ss;
|
||||
ss << "image[" << idx << "] bufferView \"" << image.bufferView
|
||||
<< "\" indexed out of bounds of its buffer." << std::endl;
|
||||
(*err) += ss.str();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (LoadImageData == nullptr) {
|
||||
if (err) {
|
||||
@@ -6956,6 +6973,7 @@ bool TinyGLTF::LoadBinaryFromFile(Model *model, std::string *err,
|
||||
///////////////////////
|
||||
// GLTF Serialization
|
||||
///////////////////////
|
||||
#ifndef TINYGLTF_USE_CUSTOM_JSON
|
||||
namespace detail {
|
||||
detail::json JsonFromString(const char *s) {
|
||||
#ifdef TINYGLTF_USE_RAPIDJSON
|
||||
@@ -7028,6 +7046,7 @@ void JsonReserveArray(detail::json &o, size_t s) {
|
||||
(void)(s);
|
||||
}
|
||||
} // namespace detail
|
||||
#endif // !TINYGLTF_USE_CUSTOM_JSON
|
||||
|
||||
// typedef std::pair<std::string, detail::json> json_object_pair;
|
||||
|
||||
@@ -7199,6 +7218,7 @@ static void SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
|
||||
static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
const std::string &binFilename) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(__GLIBCXX__) // mingw
|
||||
int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(),
|
||||
@@ -7227,6 +7247,9 @@ static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
// write empty file.
|
||||
}
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0 // FIXME(syoyo): not used. will be removed in the future release.
|
||||
@@ -8448,6 +8471,7 @@ static bool WriteGltfStream(std::ostream &stream, const std::string &content) {
|
||||
|
||||
static bool WriteGltfFile(const std::string &output,
|
||||
const std::string &content) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str());
|
||||
@@ -8467,6 +8491,9 @@ static bool WriteGltfFile(const std::string &output,
|
||||
if (!gltfFile.is_open()) return false;
|
||||
#endif
|
||||
return WriteGltfStream(gltfFile, content);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool WriteBinaryGltfStream(std::ostream &stream,
|
||||
@@ -8533,6 +8560,7 @@ static bool WriteBinaryGltfStream(std::ostream &stream,
|
||||
static bool WriteBinaryGltfFile(const std::string &output,
|
||||
const std::string &content,
|
||||
const std::vector<unsigned char> &binBuffer) {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
#ifdef _WIN32
|
||||
#if defined(_MSC_VER)
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary);
|
||||
@@ -8549,6 +8577,9 @@ static bool WriteBinaryGltfFile(const std::string &output,
|
||||
std::ofstream gltfFile(output.c_str(), std::ios::binary);
|
||||
#endif
|
||||
return WriteBinaryGltfStream(gltfFile, content, binBuffer);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
|
||||
|
||||
1907
tinygltf_json.h
Normal file
1907
tinygltf_json.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user