mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-23 15:48:50 +00:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
847df8456a | ||
|
|
6482c08cf7 | ||
|
|
e08df72575 | ||
|
|
f03fe26579 | ||
|
|
e54660fbf9 | ||
|
|
1bdd404c04 | ||
|
|
2191085580 | ||
|
|
cde43ef668 | ||
|
|
e3f9a7d8b3 | ||
|
|
f57d18ad74 | ||
|
|
ed3d1ec2f5 | ||
|
|
9b4e1eae9e | ||
|
|
cbc8e1bea6 | ||
|
|
212de904ca | ||
|
|
1f5b8f8b8c | ||
|
|
b274b34972 | ||
|
|
22dfeab315 | ||
|
|
b132612307 | ||
|
|
50d90c91ac | ||
|
|
4bfc1fc180 | ||
|
|
e0cc45e88d | ||
|
|
c3bbe97a9e | ||
|
|
f1bdf43e15 | ||
|
|
a42263bdba |
@@ -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;
|
||||
|
||||
130
tests/tester.cc
130
tests/tester.cc
@@ -926,3 +926,133 @@ TEST_CASE("zero-sized-bin-chunk-glb", "[issue-440]") {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
307
tiny_gltf.h
307
tiny_gltf.h
@@ -43,9 +43,11 @@
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// Auto-detect C++14 standard version
|
||||
@@ -337,12 +339,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;
|
||||
}
|
||||
|
||||
@@ -764,6 +765,7 @@ struct Material {
|
||||
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;
|
||||
|
||||
@@ -1018,6 +1020,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
|
||||
@@ -1262,17 +1265,18 @@ enum SectionCheck {
|
||||
/// image URIs differently, for example. See
|
||||
/// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#uris
|
||||
///
|
||||
typedef bool (*URIEncodeFunction)(const std::string &in_uri,
|
||||
const std::string &object_type,
|
||||
std::string *out_uri, void *user_data);
|
||||
using URIEncodeFunction = std::function<bool(
|
||||
const std::string & /* in_uri */, const std::string & /* object_type */,
|
||||
std::string * /* out_uri */, void * /* user_data */)>;
|
||||
|
||||
///
|
||||
/// URIDecodeFunction type. Signature for custom URI decoding of external
|
||||
/// resources such as .bin and image files. Used by tinygltf when computing
|
||||
/// filenames to write resources.
|
||||
///
|
||||
typedef bool (*URIDecodeFunction)(const std::string &in_uri,
|
||||
std::string *out_uri, void *user_data);
|
||||
using URIDecodeFunction =
|
||||
std::function<bool(const std::string & /* in_uri */,
|
||||
std::string * /* out_uri */, void * /* user_data */)>;
|
||||
|
||||
// Declaration of default uri decode function
|
||||
bool URIDecode(const std::string &in_uri, std::string *out_uri,
|
||||
@@ -1291,22 +1295,21 @@ struct URICallbacks {
|
||||
///
|
||||
/// LoadImageDataFunction type. Signature for custom image loading callbacks.
|
||||
///
|
||||
typedef bool (*LoadImageDataFunction)(Image *, const int, std::string *,
|
||||
std::string *, int, int,
|
||||
const unsigned char *, int,
|
||||
void *user_pointer);
|
||||
using LoadImageDataFunction = std::function<bool(
|
||||
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 */)>;
|
||||
|
||||
///
|
||||
/// WriteImageDataFunction type. Signature for custom image writing callbacks.
|
||||
/// The out_uri parameter becomes the URI written to the gltf and may reference
|
||||
/// a file or contain a data URI.
|
||||
///
|
||||
typedef bool (*WriteImageDataFunction)(const std::string *basepath,
|
||||
const std::string *filename,
|
||||
const Image *image, bool embedImages,
|
||||
const URICallbacks *uri_cb,
|
||||
std::string *out_uri,
|
||||
void *user_pointer);
|
||||
using WriteImageDataFunction = std::function<bool(
|
||||
const std::string * /* basepath */, const std::string * /* filename */,
|
||||
const Image *image, bool /* embedImages */,
|
||||
const URICallbacks * /* uri_cb */, std::string * /* out_uri */,
|
||||
void * /* user_pointer */)>;
|
||||
|
||||
#ifndef TINYGLTF_NO_STB_IMAGE
|
||||
// Declaration of default image loader callback
|
||||
@@ -1323,35 +1326,36 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
#endif
|
||||
|
||||
///
|
||||
/// FilExistsFunction type. Signature for custom filesystem callbacks.
|
||||
/// FileExistsFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
typedef bool (*FileExistsFunction)(const std::string &abs_filename, void *);
|
||||
using FileExistsFunction = std::function<bool(
|
||||
const std::string & /* abs_filename */, void * /* user_data */)>;
|
||||
|
||||
///
|
||||
/// ExpandFilePathFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
typedef std::string (*ExpandFilePathFunction)(const std::string &, void *);
|
||||
using ExpandFilePathFunction =
|
||||
std::function<std::string(const std::string &, void *)>;
|
||||
|
||||
///
|
||||
/// ReadWholeFileFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
typedef bool (*ReadWholeFileFunction)(std::vector<unsigned char> *,
|
||||
std::string *, const std::string &,
|
||||
void *);
|
||||
using ReadWholeFileFunction = std::function<bool(
|
||||
std::vector<unsigned char> *, std::string *, const std::string &, void *)>;
|
||||
|
||||
///
|
||||
/// WriteWholeFileFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
typedef bool (*WriteWholeFileFunction)(std::string *, const std::string &,
|
||||
const std::vector<unsigned char> &,
|
||||
void *);
|
||||
using WriteWholeFileFunction =
|
||||
std::function<bool(std::string *, const std::string &,
|
||||
const std::vector<unsigned char> &, void *)>;
|
||||
|
||||
///
|
||||
/// GetFileSizeFunction type. Signature for custom filesystem callbacks.
|
||||
///
|
||||
typedef bool (*GetFileSizeFunction)(size_t *filesize_out, std::string *err,
|
||||
const std::string &abs_filename,
|
||||
void *userdata);
|
||||
using GetFileSizeFunction =
|
||||
std::function<bool(size_t *filesize_out, std::string *err,
|
||||
const std::string &abs_filename, void *userdata)>;
|
||||
|
||||
///
|
||||
/// A structure containing all required filesystem callbacks and a pointer to
|
||||
@@ -1474,7 +1478,8 @@ class TinyGLTF {
|
||||
void SetParseStrictness(ParseStrictness strictness);
|
||||
|
||||
///
|
||||
/// Set callback to use for loading image data
|
||||
/// Set callback to use for loading image data. Passing the nullptr is akin to
|
||||
/// calling RemoveImageLoader().
|
||||
///
|
||||
void SetImageLoader(LoadImageDataFunction LoadImageData, void *user_data);
|
||||
|
||||
@@ -1489,14 +1494,18 @@ class TinyGLTF {
|
||||
void SetImageWriter(WriteImageDataFunction WriteImageData, void *user_data);
|
||||
|
||||
///
|
||||
/// Set callbacks to use for URI encoding and decoding and their user data
|
||||
/// Set callbacks to use for URI encoding and decoding and their user data.
|
||||
/// Returns false if there is an error with the callbacks. If err is not
|
||||
/// nullptr, explanation will be written there.
|
||||
///
|
||||
void SetURICallbacks(URICallbacks callbacks);
|
||||
bool SetURICallbacks(URICallbacks callbacks, std::string* err = nullptr);
|
||||
|
||||
///
|
||||
/// Set callbacks to use for filesystem (fs) access and their user data
|
||||
/// Set callbacks to use for filesystem (fs) access and their user data.
|
||||
/// Returns false if there is an error with the callbacks. If err is not
|
||||
/// nullptr, explanation will be written there.
|
||||
///
|
||||
void SetFsCallbacks(FsCallbacks callbacks);
|
||||
bool SetFsCallbacks(FsCallbacks callbacks, std::string* err = nullptr);
|
||||
|
||||
///
|
||||
/// Set serializing default values(default = false).
|
||||
@@ -1925,7 +1934,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 +2253,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 "";
|
||||
}
|
||||
|
||||
@@ -2570,7 +2579,11 @@ void TinyGLTF::SetParseStrictness(ParseStrictness strictness) {
|
||||
}
|
||||
|
||||
void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
|
||||
LoadImageData = func;
|
||||
if (func == nullptr) {
|
||||
RemoveImageLoader();
|
||||
return;
|
||||
}
|
||||
LoadImageData = std::move(func);
|
||||
load_image_user_data_ = user_data;
|
||||
user_image_loader_ = true;
|
||||
}
|
||||
@@ -2696,7 +2709,7 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err,
|
||||
#endif
|
||||
|
||||
void TinyGLTF::SetImageWriter(WriteImageDataFunction func, void *user_data) {
|
||||
WriteImageData = func;
|
||||
WriteImageData = std::move(func);
|
||||
write_image_user_data_ = user_data;
|
||||
}
|
||||
|
||||
@@ -2787,14 +2800,36 @@ bool WriteImageData(const std::string *basepath, const std::string *filename,
|
||||
}
|
||||
#endif
|
||||
|
||||
void TinyGLTF::SetURICallbacks(URICallbacks callbacks) {
|
||||
assert(callbacks.decode);
|
||||
if (callbacks.decode) {
|
||||
uri_cb = callbacks;
|
||||
bool TinyGLTF::SetURICallbacks(URICallbacks callbacks, std::string* err) {
|
||||
if (callbacks.decode == nullptr) {
|
||||
if (err != nullptr) {
|
||||
*err = "URI Callback require a non-null decode function.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (callbacks.decode) {
|
||||
uri_cb = std::move(callbacks);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void TinyGLTF::SetFsCallbacks(FsCallbacks callbacks) { fs = callbacks; }
|
||||
bool TinyGLTF::SetFsCallbacks(FsCallbacks callbacks, std::string *err) {
|
||||
// If callbacks are defined at all, they must all be defined.
|
||||
if (callbacks.FileExists == nullptr || callbacks.ExpandFilePath == nullptr ||
|
||||
callbacks.ReadWholeFile == nullptr ||
|
||||
callbacks.WriteWholeFile == nullptr ||
|
||||
callbacks.GetFileSizeInBytes == nullptr) {
|
||||
if (err != nullptr) {
|
||||
*err =
|
||||
"FS Callbacks must be completely defined. At least one callback is "
|
||||
"null.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
fs = std::move(callbacks);
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static inline std::wstring UTF8ToWchar(const std::string &str) {
|
||||
@@ -3018,18 +3053,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 +3076,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 +3101,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 +3148,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 +3184,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);
|
||||
@@ -3199,7 +3234,7 @@ static std::string MimeToExt(const std::string &mimeType) {
|
||||
static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||
int index, bool embedImages,
|
||||
const URICallbacks *uri_cb,
|
||||
WriteImageDataFunction *WriteImageData,
|
||||
const WriteImageDataFunction& WriteImageData,
|
||||
void *user_data, std::string *out_uri) {
|
||||
std::string filename;
|
||||
std::string ext;
|
||||
@@ -3229,9 +3264,9 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
|
||||
// image data does not exist, this is not considered a failure and the
|
||||
// original uri should be maintained.
|
||||
bool imageWritten = false;
|
||||
if (*WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
|
||||
imageWritten = (*WriteImageData)(&baseDir, &filename, &image, embedImages,
|
||||
uri_cb, out_uri, user_data);
|
||||
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
|
||||
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
|
||||
uri_cb, out_uri, user_data);
|
||||
if (!imageWritten) {
|
||||
return false;
|
||||
}
|
||||
@@ -4213,7 +4248,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
bool store_original_json_for_extras_and_extensions,
|
||||
const std::string &basedir, const size_t max_file_size,
|
||||
FsCallbacks *fs, const URICallbacks *uri_cb,
|
||||
LoadImageDataFunction *LoadImageData = nullptr,
|
||||
const LoadImageDataFunction& LoadImageData = nullptr,
|
||||
void *load_image_user_data = nullptr) {
|
||||
// A glTF image must either reference a bufferView or an image uri
|
||||
|
||||
@@ -4344,14 +4379,14 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
|
||||
#endif
|
||||
}
|
||||
|
||||
if (*LoadImageData == nullptr) {
|
||||
if (LoadImageData == nullptr) {
|
||||
if (err) {
|
||||
(*err) += "No LoadImageData callback specified.\n";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return (*LoadImageData)(image, image_idx, err, warn, 0, 0, &img.at(0),
|
||||
static_cast<int>(img.size()), load_image_user_data);
|
||||
return LoadImageData(image, image_idx, err, warn, 0, 0, &img.at(0),
|
||||
static_cast<int>(img.size()), load_image_user_data);
|
||||
}
|
||||
|
||||
static bool ParseTexture(Texture *texture, std::string *err,
|
||||
@@ -5165,6 +5200,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 +5417,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;
|
||||
}
|
||||
|
||||
@@ -6304,7 +6375,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
if (!ParseImage(&image, idx, err, warn, o,
|
||||
store_original_json_for_extras_and_extensions_, base_dir,
|
||||
max_external_file_size_, &fs, &uri_cb,
|
||||
&this->LoadImageData, load_image_user_data)) {
|
||||
this->LoadImageData, load_image_user_data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -6333,7 +6404,7 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
}
|
||||
const Buffer &buffer = model->buffers[size_t(bufferView.buffer)];
|
||||
|
||||
if (*LoadImageData == nullptr) {
|
||||
if (LoadImageData == nullptr) {
|
||||
if (err) {
|
||||
(*err) += "No LoadImageData callback specified.\n";
|
||||
}
|
||||
@@ -7032,10 +7103,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;
|
||||
@@ -7089,7 +7160,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);
|
||||
@@ -7579,11 +7650,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) {
|
||||
@@ -7806,7 +7906,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 {
|
||||
@@ -7823,6 +7923,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);
|
||||
}
|
||||
@@ -8236,6 +8367,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);
|
||||
@@ -8254,7 +8411,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);
|
||||
@@ -8339,7 +8496,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);
|
||||
@@ -8390,7 +8547,7 @@ bool TinyGLTF::WriteGltfSceneToStream(const Model *model, std::ostream &stream,
|
||||
// we
|
||||
std::string uri;
|
||||
if (!UpdateImageObject(model->images[i], dummystring, int(i), true,
|
||||
&uri_cb, &this->WriteImageData,
|
||||
&uri_cb, this->WriteImageData,
|
||||
this->write_image_user_data_, &uri)) {
|
||||
return false;
|
||||
}
|
||||
@@ -8498,7 +8655,7 @@ bool TinyGLTF::WriteGltfSceneToFile(const Model *model,
|
||||
|
||||
std::string uri;
|
||||
if (!UpdateImageObject(model->images[i], baseDir, int(i), embedImages,
|
||||
&uri_cb, &this->WriteImageData,
|
||||
&uri_cb, this->WriteImageData,
|
||||
this->write_image_user_data_, &uri)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user