mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-13 10:48:56 +00:00
Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f03fe26579 | ||
|
|
e54660fbf9 | ||
|
|
1bdd404c04 | ||
|
|
2191085580 | ||
|
|
cde43ef668 | ||
|
|
e3f9a7d8b3 | ||
|
|
f57d18ad74 | ||
|
|
ed3d1ec2f5 | ||
|
|
9b4e1eae9e | ||
|
|
cbc8e1bea6 | ||
|
|
212de904ca | ||
|
|
1f5b8f8b8c | ||
|
|
b274b34972 | ||
|
|
22dfeab315 | ||
|
|
b132612307 | ||
|
|
50d90c91ac | ||
|
|
4bfc1fc180 | ||
|
|
e0cc45e88d | ||
|
|
c3bbe97a9e | ||
|
|
f1bdf43e15 | ||
|
|
a42263bdba | ||
|
|
4fea26f6c8 | ||
|
|
c5641f2c22 | ||
|
|
6782f887bb | ||
|
|
8fdeca146e | ||
|
|
7fd75df70e | ||
|
|
77238cf23c | ||
|
|
8acf861db7 | ||
|
|
03b3a31e02 | ||
|
|
30ec815748 | ||
|
|
8387fdbd50 | ||
|
|
32198f757f | ||
|
|
1c6f6efafc | ||
|
|
d32f1fb2fb | ||
|
|
3203e1985e | ||
|
|
211f86e3f5 | ||
|
|
afcfb57898 | ||
|
|
b6e2398e1d | ||
|
|
d4ea67cae1 | ||
|
|
f32475c952 | ||
|
|
1f42c963e6 | ||
|
|
fd6c7855e7 |
@@ -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
|
||||
63
.travis.yml
63
.travis.yml
@@ -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
|
||||
@@ -26,8 +26,6 @@ Currently TinyGLTF is stable and maintenance mode. No drastic changes and featur
|
||||
|
||||
## Builds
|
||||
|
||||
[](https://travis-ci.org/syoyo/tinygltf)
|
||||
|
||||
[](https://ci.appveyor.com/project/syoyo/tinygltf)
|
||||
|
||||

|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
BIN
models/regression/zero-sized-bin-chunk-issue-440.glb
Normal file
BIN
models/regression/zero-sized-bin-chunk-issue-440.glb
Normal file
Binary file not shown.
@@ -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);
|
||||
|
||||
|
||||
323
tests/tester.cc
323
tests/tester.cc
@@ -494,25 +494,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]") {
|
||||
@@ -757,3 +755,304 @@ TEST_CASE("load-issue-416-model", "[issue-416]") {
|
||||
// external file load fails, but reading glTF itself is ok.
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
306
tiny_gltf.h
306
tiny_gltf.h
@@ -337,12 +337,11 @@ class Value {
|
||||
T &Get();
|
||||
|
||||
// Lookup value from an array
|
||||
const Value &Get(int idx) const {
|
||||
const Value &Get(size_t idx) const {
|
||||
static Value null_value;
|
||||
assert(IsArray());
|
||||
assert(idx >= 0);
|
||||
return (static_cast<size_t>(idx) < array_value_.size())
|
||||
? array_value_[static_cast<size_t>(idx)]
|
||||
return (idx < array_value_.size())
|
||||
? array_value_[idx]
|
||||
: null_value;
|
||||
}
|
||||
|
||||
@@ -735,7 +734,7 @@ struct OcclusionTextureInfo {
|
||||
|
||||
// pbrMetallicRoughness class defined in glTF 2.0 spec.
|
||||
struct PbrMetallicRoughness {
|
||||
std::vector<double> baseColorFactor; // len = 4. default [1,1,1,1]
|
||||
std::vector<double> baseColorFactor{1.0, 1.0, 1.0, 1.0}; // len = 4. default [1,1,1,1]
|
||||
TextureInfo baseColorTexture;
|
||||
double metallicFactor{1.0}; // default 1
|
||||
double roughnessFactor{1.0}; // default 1
|
||||
@@ -748,9 +747,9 @@ struct PbrMetallicRoughness {
|
||||
std::string extras_json_string;
|
||||
std::string extensions_json_string;
|
||||
|
||||
PbrMetallicRoughness()
|
||||
: baseColorFactor(std::vector<double>{1.0, 1.0, 1.0, 1.0}) {}
|
||||
PbrMetallicRoughness() = default;
|
||||
DEFAULT_METHODS(PbrMetallicRoughness)
|
||||
|
||||
bool operator==(const PbrMetallicRoughness &) const;
|
||||
};
|
||||
|
||||
@@ -760,10 +759,11 @@ struct PbrMetallicRoughness {
|
||||
struct Material {
|
||||
std::string name;
|
||||
|
||||
std::vector<double> emissiveFactor; // length 3. default [0, 0, 0]
|
||||
std::string alphaMode; // default "OPAQUE"
|
||||
double alphaCutoff{0.5}; // default 0.5
|
||||
bool doubleSided{false}; // default false;
|
||||
std::vector<double> emissiveFactor{0.0, 0.0, 0.0}; // length 3. default [0, 0, 0]
|
||||
std::string alphaMode{"OPAQUE"}; // default "OPAQUE"
|
||||
double alphaCutoff{0.5}; // default 0.5
|
||||
bool doubleSided{false}; // default false
|
||||
std::vector<int> lods; // level of detail materials (MSFT_lod)
|
||||
|
||||
PbrMetallicRoughness pbrMetallicRoughness;
|
||||
|
||||
@@ -783,7 +783,7 @@ struct Material {
|
||||
std::string extras_json_string;
|
||||
std::string extensions_json_string;
|
||||
|
||||
Material() : alphaMode("OPAQUE") {}
|
||||
Material() = default;
|
||||
DEFAULT_METHODS(Material)
|
||||
|
||||
bool operator==(const Material &) const;
|
||||
@@ -1018,6 +1018,7 @@ class Node {
|
||||
int mesh{-1};
|
||||
int light{-1}; // light source index (KHR_lights_punctual)
|
||||
int emitter{-1}; // audio emitter index (KHR_audio)
|
||||
std::vector<int> lods; // level of detail nodes (MSFT_lod)
|
||||
std::vector<int> children;
|
||||
std::vector<double> rotation; // length must be 0 or 4
|
||||
std::vector<double> scale; // length must be 0 or 3
|
||||
@@ -1925,7 +1926,7 @@ static bool Equals(const tinygltf::Value &one, const tinygltf::Value &other) {
|
||||
}
|
||||
case ARRAY_TYPE: {
|
||||
if (one.Size() != other.Size()) return false;
|
||||
for (int i = 0; i < int(one.Size()); ++i)
|
||||
for (size_t i = 0; i < one.Size(); ++i)
|
||||
if (!Equals(one.Get(i), other.Get(i))) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -2244,7 +2245,7 @@ static std::string GetFilePathExtension(const std::string &FileName) {
|
||||
|
||||
static std::string GetBaseDir(const std::string &filepath) {
|
||||
if (filepath.find_last_of("/\\") != std::string::npos)
|
||||
return filepath.substr(0, filepath.find_last_of("/\\"));
|
||||
return filepath.substr(0, filepath.find_last_of("/\\") + 1);
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -3018,18 +3019,18 @@ bool GetFileSizeInBytes(size_t *filesize_out, std::string *err,
|
||||
}
|
||||
|
||||
f.seekg(0, f.end);
|
||||
size_t sz = static_cast<size_t>(f.tellg());
|
||||
const auto sz = f.tellg();
|
||||
|
||||
// std::cout << "sz = " << sz << "\n";
|
||||
f.seekg(0, f.beg);
|
||||
|
||||
if (int64_t(sz) < 0) {
|
||||
if (sz < 0) {
|
||||
if (err) {
|
||||
(*err) += "Invalid file size : " + filepath +
|
||||
" (does the path point to a directory?)";
|
||||
}
|
||||
return false;
|
||||
} else if (sz == 0) {
|
||||
} else if (sz == std::streamoff(0)) {
|
||||
if (err) {
|
||||
(*err) += "File is empty : " + filepath + "\n";
|
||||
}
|
||||
@@ -3041,7 +3042,7 @@ bool GetFileSizeInBytes(size_t *filesize_out, std::string *err,
|
||||
return false;
|
||||
}
|
||||
|
||||
(*filesize_out) = sz;
|
||||
(*filesize_out) = static_cast<size_t>(sz);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
@@ -3066,7 +3067,7 @@ bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
|
||||
}
|
||||
return false;
|
||||
}
|
||||
out->resize(size);
|
||||
out->resize(static_cast<size_t>(size));
|
||||
AAsset_read(asset, reinterpret_cast<char *>(&out->at(0)), size);
|
||||
AAsset_close(asset);
|
||||
return true;
|
||||
@@ -3113,18 +3114,18 @@ bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
|
||||
}
|
||||
|
||||
f.seekg(0, f.end);
|
||||
size_t sz = static_cast<size_t>(f.tellg());
|
||||
const auto sz = f.tellg();
|
||||
|
||||
// std::cout << "sz = " << sz << "\n";
|
||||
f.seekg(0, f.beg);
|
||||
|
||||
if (int64_t(sz) < 0) {
|
||||
if (sz < 0) {
|
||||
if (err) {
|
||||
(*err) += "Invalid file size : " + filepath +
|
||||
" (does the path point to a directory?)";
|
||||
}
|
||||
return false;
|
||||
} else if (sz == 0) {
|
||||
} else if (sz == std::streamoff(0)) {
|
||||
if (err) {
|
||||
(*err) += "File is empty : " + filepath + "\n";
|
||||
}
|
||||
@@ -3149,7 +3150,7 @@ bool WriteWholeFile(std::string *err, const std::string &filepath,
|
||||
#ifdef _WIN32
|
||||
#if defined(__GLIBCXX__) // mingw
|
||||
int file_descriptor = _wopen(UTF8ToWchar(filepath).c_str(),
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE);
|
||||
__gnu_cxx::stdio_filebuf<char> wfile_buf(
|
||||
file_descriptor, std::ios_base::out | std::ios_base::binary);
|
||||
std::ostream f(&wfile_buf);
|
||||
@@ -5165,6 +5166,24 @@ static bool ParseNode(Node *node, std::string *err, const detail::json &o,
|
||||
}
|
||||
node->emitter = emitter;
|
||||
|
||||
node->lods.clear();
|
||||
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");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
node->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
} else {
|
||||
if (err) {
|
||||
*err +=
|
||||
"Node has extension MSFT_lod, but does not reference "
|
||||
"other nodes via their ids.\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5364,6 +5383,24 @@ static bool ParseMaterial(Material *material, std::string *err, std::string *war
|
||||
ParseExtrasAndExtensions(material, err, o,
|
||||
store_original_json_for_extras_and_extensions);
|
||||
|
||||
material->lods.clear();
|
||||
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");
|
||||
for (size_t i = 0; i < idsArr.ArrayLen(); ++i) {
|
||||
material->lods.emplace_back(idsArr.Get(i).GetNumberAsInt());
|
||||
}
|
||||
} else {
|
||||
if (err) {
|
||||
*err +=
|
||||
"Material has extension MSFT_lod, but does not reference "
|
||||
"other materials via their ids.\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6149,7 +6186,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
model->accessors[size_t(primitive.indices)].bufferView;
|
||||
if (bufferView < 0) {
|
||||
// skip, bufferView could be null(-1) for certain extensions
|
||||
} else if (size_t(bufferView) >= model->bufferViews.size()) {
|
||||
} else if (size_t(bufferView) >= model->bufferViews.size()) {
|
||||
if (err) {
|
||||
(*err) += "accessor[" + std::to_string(primitive.indices) +
|
||||
"] invalid bufferView";
|
||||
@@ -6652,7 +6689,7 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
|
||||
memcpy(&version, bytes + 4, 4);
|
||||
swap4(&version);
|
||||
memcpy(&length, bytes + 8, 4);
|
||||
memcpy(&length, bytes + 8, 4); // Total glb size, including header and all chunks.
|
||||
swap4(&length);
|
||||
memcpy(&chunk0_length, bytes + 12, 4); // JSON data length
|
||||
swap4(&chunk0_length);
|
||||
@@ -6669,9 +6706,12 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
// Use 64bit uint to avoid integer overflow.
|
||||
uint64_t header_and_json_size = 20ull + uint64_t(chunk0_length);
|
||||
|
||||
if (header_and_json_size > std::numeric_limits<uint32_t>::max()) {
|
||||
if (header_and_json_size > (std::numeric_limits<uint32_t>::max)()) {
|
||||
// Do not allow 4GB or more GLB data.
|
||||
(*err) = "Invalid glTF binary. GLB data exceeds 4GB.";
|
||||
if (err) {
|
||||
(*err) = "Invalid glTF binary. GLB data exceeds 4GB.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((header_and_json_size > uint64_t(size)) || (chunk0_length < 1) ||
|
||||
@@ -6690,6 +6730,7 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
if (err) {
|
||||
(*err) = "JSON Chunk end does not aligned to a 4-byte boundary.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// std::cout << "header_and_json_size = " << header_and_json_size << "\n";
|
||||
@@ -6704,69 +6745,87 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
bin_size_ = 0;
|
||||
} else {
|
||||
// Read Chunk1 info(BIN data)
|
||||
// At least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin
|
||||
// payload could be 1~3 bytes, but need to be aligned to 4 bytes)
|
||||
if ((header_and_json_size + 12ull) > uint64_t(length)) {
|
||||
//
|
||||
// issue-440:
|
||||
// 'SHOULD' in glTF spec means 'RECOMMENDED',
|
||||
// So there is a situation that Chunk1(BIN) is composed of zero-sized BIN data
|
||||
// (chunksize(0) + binformat(BIN) = 8bytes).
|
||||
//
|
||||
if ((header_and_json_size + 8ull) > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) =
|
||||
"Insufficient storage space for Chunk1(BIN data). At least Chunk1 "
|
||||
"Must have 4 or more bytes, but got " +
|
||||
"Must have 8 or more bytes, but got " +
|
||||
std::to_string((header_and_json_size + 8ull) - uint64_t(length)) +
|
||||
".\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int chunk1_length; // 4 bytes
|
||||
unsigned int chunk1_format; // 4 bytes;
|
||||
unsigned int chunk1_length{0}; // 4 bytes
|
||||
unsigned int chunk1_format{0}; // 4 bytes;
|
||||
memcpy(&chunk1_length, bytes + header_and_json_size,
|
||||
4); // JSON data length
|
||||
4); // Bin data length
|
||||
swap4(&chunk1_length);
|
||||
memcpy(&chunk1_format, bytes + header_and_json_size + 4, 4);
|
||||
swap4(&chunk1_format);
|
||||
|
||||
// std::cout << "chunk1_length = " << chunk1_length << "\n";
|
||||
|
||||
if (chunk1_length < 4) {
|
||||
if (chunk1_format != 0x004e4942) {
|
||||
if (err) {
|
||||
(*err) = "Insufficient Chunk1(BIN) data size.";
|
||||
(*err) = "Invalid chunkType for Chunk1.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((chunk1_length % 4) != 0) {
|
||||
if (strictness_==ParseStrictness::Permissive) {
|
||||
if (warn) {
|
||||
(*warn) += "BIN Chunk end is not aligned to a 4-byte boundary.\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (chunk1_length == 0) {
|
||||
|
||||
if (header_and_json_size + 8 > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk end is not aligned to a 4-byte boundary.";
|
||||
(*err) = "BIN Chunk header location exceeds the GLB size.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (uint64_t(chunk1_length) + header_and_json_size > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk data length exceeds the GLB size.";
|
||||
bin_data_ = nullptr;
|
||||
|
||||
} else {
|
||||
|
||||
// When BIN chunk size is not zero, at least Chunk1 should have 12 bytes(8 bytes(header) + 4 bytes(bin
|
||||
// payload could be 1~3 bytes, but need to be aligned to 4 bytes)
|
||||
|
||||
if (chunk1_length < 4) {
|
||||
if (err) {
|
||||
(*err) = "Insufficient Chunk1(BIN) data size.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (chunk1_format != 0x004e4942) {
|
||||
if (err) {
|
||||
(*err) = "Invalid type for chunk1 data.";
|
||||
if ((chunk1_length % 4) != 0) {
|
||||
if (strictness_==ParseStrictness::Permissive) {
|
||||
if (warn) {
|
||||
(*warn) += "BIN Chunk end is not aligned to a 4-byte boundary.\n";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk end is not aligned to a 4-byte boundary.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
// +8 chunk1 header size.
|
||||
if (uint64_t(chunk1_length) + header_and_json_size + 8 > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk data length exceeds the GLB size.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bin_data_ = bytes + header_and_json_size +
|
||||
8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format)
|
||||
}
|
||||
|
||||
// std::cout << "chunk1_length = " << chunk1_length << "\n";
|
||||
|
||||
bin_data_ = bytes + header_and_json_size +
|
||||
8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format)
|
||||
|
||||
bin_size_ = size_t(chunk1_length);
|
||||
}
|
||||
|
||||
@@ -7010,10 +7069,10 @@ static bool ValueToJson(const Value &value, detail::json *ret) {
|
||||
obj = detail::json(value.Get<std::string>());
|
||||
break;
|
||||
case ARRAY_TYPE: {
|
||||
for (unsigned int i = 0; i < value.ArrayLen(); ++i) {
|
||||
Value elementValue = value.Get(int(i));
|
||||
for (size_t i = 0; i < value.ArrayLen(); ++i) {
|
||||
Value elementValue = value.Get(i);
|
||||
detail::json elementJson;
|
||||
if (ValueToJson(value.Get(int(i)), &elementJson))
|
||||
if (ValueToJson(value.Get(i), &elementJson))
|
||||
obj.push_back(elementJson);
|
||||
}
|
||||
break;
|
||||
@@ -7067,7 +7126,7 @@ static bool SerializeGltfBufferData(const std::vector<unsigned char> &data,
|
||||
#ifdef _WIN32
|
||||
#if defined(__GLIBCXX__) // mingw
|
||||
int file_descriptor = _wopen(UTF8ToWchar(binFilename).c_str(),
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE);
|
||||
__gnu_cxx::stdio_filebuf<char> wfile_buf(
|
||||
file_descriptor, std::ios_base::out | std::ios_base::binary);
|
||||
std::ostream output(&wfile_buf);
|
||||
@@ -7557,11 +7616,40 @@ static void SerializeGltfMaterial(const Material &material, detail::json &o) {
|
||||
}
|
||||
|
||||
SerializeParameterMap(material.additionalValues, o);
|
||||
#else
|
||||
|
||||
#endif
|
||||
|
||||
SerializeExtrasAndExtensions(material, o);
|
||||
|
||||
// MSFT_lod
|
||||
if (!material.lods.empty()) {
|
||||
detail::json_iterator it;
|
||||
if (!detail::FindMember(o, "extensions", it)) {
|
||||
detail::json extensions;
|
||||
detail::JsonSetObject(extensions);
|
||||
detail::JsonAddMember(o, "extensions", std::move(extensions));
|
||||
detail::FindMember(o, "extensions", it);
|
||||
}
|
||||
auto &extensions = detail::GetValue(it);
|
||||
if (!detail::FindMember(extensions, "MSFT_lod", it)) {
|
||||
detail::json lod;
|
||||
detail::JsonSetObject(lod);
|
||||
detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod));
|
||||
detail::FindMember(extensions, "MSFT_lod", it);
|
||||
}
|
||||
SerializeNumberArrayProperty<int>("ids", material.lods, detail::GetValue(it));
|
||||
} else {
|
||||
detail::json_iterator ext_it;
|
||||
if (detail::FindMember(o, "extensions", ext_it)) {
|
||||
auto &extensions = detail::GetValue(ext_it);
|
||||
detail::json_iterator lp_it;
|
||||
if (detail::FindMember(extensions, "MSFT_lod", lp_it)) {
|
||||
detail::Erase(extensions, lp_it);
|
||||
}
|
||||
if (detail::IsEmpty(extensions)) {
|
||||
detail::Erase(o, ext_it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void SerializeGltfMesh(const Mesh &mesh, detail::json &o) {
|
||||
@@ -7752,7 +7840,7 @@ static void SerializeGltfNode(const Node &node, detail::json &o) {
|
||||
detail::JsonSetObject(lights_punctual);
|
||||
detail::JsonAddMember(extensions, "KHR_lights_punctual",
|
||||
std::move(lights_punctual));
|
||||
detail::FindMember(o, "KHR_lights_punctual", it);
|
||||
detail::FindMember(extensions, "KHR_lights_punctual", it);
|
||||
}
|
||||
SerializeNumberProperty("light", node.light, detail::GetValue(it));
|
||||
} else {
|
||||
@@ -7784,7 +7872,7 @@ static void SerializeGltfNode(const Node &node, detail::json &o) {
|
||||
detail::json audio;
|
||||
detail::JsonSetObject(audio);
|
||||
detail::JsonAddMember(extensions, "KHR_audio", std::move(audio));
|
||||
detail::FindMember(o, "KHR_audio", it);
|
||||
detail::FindMember(extensions, "KHR_audio", it);
|
||||
}
|
||||
SerializeNumberProperty("emitter", node.emitter, detail::GetValue(it));
|
||||
} else {
|
||||
@@ -7801,6 +7889,37 @@ static void SerializeGltfNode(const Node &node, detail::json &o) {
|
||||
}
|
||||
}
|
||||
|
||||
// MSFT_lod
|
||||
if (!node.lods.empty()) {
|
||||
detail::json_iterator it;
|
||||
if (!detail::FindMember(o, "extensions", it)) {
|
||||
detail::json extensions;
|
||||
detail::JsonSetObject(extensions);
|
||||
detail::JsonAddMember(o, "extensions", std::move(extensions));
|
||||
detail::FindMember(o, "extensions", it);
|
||||
}
|
||||
auto &extensions = detail::GetValue(it);
|
||||
if (!detail::FindMember(extensions, "MSFT_lod", it)) {
|
||||
detail::json lod;
|
||||
detail::JsonSetObject(lod);
|
||||
detail::JsonAddMember(extensions, "MSFT_lod", std::move(lod));
|
||||
detail::FindMember(extensions, "MSFT_lod", it);
|
||||
}
|
||||
SerializeNumberArrayProperty<int>("ids", node.lods, detail::GetValue(it));
|
||||
} else {
|
||||
detail::json_iterator ext_it;
|
||||
if (detail::FindMember(o, "extensions", ext_it)) {
|
||||
auto &extensions = detail::GetValue(ext_it);
|
||||
detail::json_iterator lp_it;
|
||||
if (detail::FindMember(extensions, "MSFT_lod", lp_it)) {
|
||||
detail::Erase(extensions, lp_it);
|
||||
}
|
||||
if (detail::IsEmpty(extensions)) {
|
||||
detail::Erase(o, ext_it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!node.name.empty()) SerializeStringProperty("name", node.name, o);
|
||||
SerializeNumberArrayProperty<int>("children", node.children, o);
|
||||
}
|
||||
@@ -8036,6 +8155,16 @@ static void SerializeGltfModel(const Model *model, detail::json &o) {
|
||||
for (unsigned int i = 0; i < model->nodes.size(); ++i) {
|
||||
detail::json node;
|
||||
SerializeGltfNode(model->nodes[i], node);
|
||||
|
||||
if (detail::JsonIsNull(node)) {
|
||||
// Issue 457.
|
||||
// `node` does not have any required parameters,
|
||||
// so the result may be null(unmodified) when all node parameters
|
||||
// have default value.
|
||||
//
|
||||
// null is not allowed thus we create an empty JSON object.
|
||||
detail::JsonSetObject(node);
|
||||
}
|
||||
detail::JsonPushBack(nodes, std::move(node));
|
||||
}
|
||||
detail::JsonAddMember(o, "nodes", std::move(nodes));
|
||||
@@ -8053,6 +8182,15 @@ static void SerializeGltfModel(const Model *model, detail::json &o) {
|
||||
for (unsigned int i = 0; i < model->scenes.size(); ++i) {
|
||||
detail::json currentScene;
|
||||
SerializeGltfScene(model->scenes[i], currentScene);
|
||||
if (detail::JsonIsNull(currentScene)) {
|
||||
// Issue 464.
|
||||
// `scene` does not have any required parameters,
|
||||
// so the result may be null(unmodified) when all scene parameters
|
||||
// have default value.
|
||||
//
|
||||
// null is not allowed thus we create an empty JSON object.
|
||||
detail::JsonSetObject(currentScene);
|
||||
}
|
||||
detail::JsonPushBack(scenes, std::move(currentScene));
|
||||
}
|
||||
detail::JsonAddMember(o, "scenes", std::move(scenes));
|
||||
@@ -8195,6 +8333,32 @@ static void SerializeGltfModel(const Model *model, detail::json &o) {
|
||||
}
|
||||
}
|
||||
|
||||
// MSFT_lod
|
||||
|
||||
// Look if there is a node that employs MSFT_lod
|
||||
auto msft_lod_nodes_it = std::find_if(
|
||||
model->nodes.begin(), model->nodes.end(),
|
||||
[](const Node& node) { return !node.lods.empty(); });
|
||||
|
||||
// Look if there is a material that employs MSFT_lod
|
||||
auto msft_lod_materials_it = std::find_if(
|
||||
model->materials.begin(), model->materials.end(),
|
||||
[](const Material& material) {return !material.lods.empty(); });
|
||||
|
||||
// If either a node or a material employ MSFT_lod, then we need
|
||||
// to add MSFT_lod to the list of used extensions.
|
||||
if (msft_lod_nodes_it != model->nodes.end() || msft_lod_materials_it != model->materials.end()) {
|
||||
// First check if MSFT_lod is already registered as used extension
|
||||
auto has_msft_lod = std::find_if(
|
||||
extensionsUsed.begin(), extensionsUsed.end(),
|
||||
[](const std::string &s) { return (s.compare("MSFT_lod") == 0); });
|
||||
|
||||
// If MSFT_lod is not registered yet, add it
|
||||
if (has_msft_lod == extensionsUsed.end()) {
|
||||
extensionsUsed.push_back("MSFT_lod");
|
||||
}
|
||||
}
|
||||
|
||||
// Extensions used
|
||||
if (extensionsUsed.size()) {
|
||||
SerializeStringArrayProperty("extensionsUsed", extensionsUsed, o);
|
||||
@@ -8213,7 +8377,7 @@ static bool WriteGltfFile(const std::string &output,
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str());
|
||||
#elif defined(__GLIBCXX__)
|
||||
int file_descriptor = _wopen(UTF8ToWchar(output).c_str(),
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE);
|
||||
__gnu_cxx::stdio_filebuf<char> wfile_buf(
|
||||
file_descriptor, std::ios_base::out | std::ios_base::binary);
|
||||
std::ostream gltfFile(&wfile_buf);
|
||||
@@ -8298,7 +8462,7 @@ static bool WriteBinaryGltfFile(const std::string &output,
|
||||
std::ofstream gltfFile(UTF8ToWchar(output).c_str(), std::ios::binary);
|
||||
#elif defined(__GLIBCXX__)
|
||||
int file_descriptor = _wopen(UTF8ToWchar(output).c_str(),
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY);
|
||||
_O_CREAT | _O_WRONLY | _O_TRUNC | _O_BINARY, _S_IWRITE);
|
||||
__gnu_cxx::stdio_filebuf<char> wfile_buf(
|
||||
file_descriptor, std::ios_base::out | std::ios_base::binary);
|
||||
std::ostream gltfFile(&wfile_buf);
|
||||
|
||||
Reference in New Issue
Block a user