Compare commits

..

1 Commits

Author SHA1 Message Date
Syoyo Fujita
4b5fc0cb31 minijson experiment. 2025-01-22 22:26:02 +09:00
6 changed files with 3209 additions and 83 deletions

View File

@@ -9,7 +9,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch (b
## Status
Currently TinyGLTF is stable and maintenance mode. No drastic changes and feature additions planned.
- v2.9.0 Various fixes and improvements. Filesystem callback API change.
- v2.8.0 Add URICallbacks for custom URI handling in Buffer and Image. PR#397
- v2.7.0 Change WriteImageDataFunction user callback function signature. PR#393
- v2.6.0 Support serializing sparse accessor(Thanks to @fynv).

View File

@@ -1,6 +1,7 @@
//
// TODO(syoyo): Print extensions and extras for each glTF object.
//
#include <iostream>
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION

3186
minijson.h Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -1182,68 +1182,3 @@ TEST_CASE("images-as-is", "[issue-487]") {
}
}
}
TEST_CASE("inverse-bind-matrices-optional", "[issue-492]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
std::string warn;
bool ret = ctx.LoadBinaryFromFile(&model, &err, &warn, "issue-492.glb");
if (!warn.empty()) {
std::cout << "WARN:" << warn << std::endl;
}
if (!err.empty()) {
std::cerr << "ERR:" << err << std::endl;
}
REQUIRE(true == ret);
REQUIRE(err.empty());
}
bool LoadImageData(tinygltf::Image * /* image */, const int /* image_idx */, std::string * /* err */,
std::string * /* warn */, int /* req_width */, int /* req_height */,
const unsigned char * /* bytes */, int /* size */, void * /*user_data */) {
return true;
}
bool WriteImageData(const std::string * /* basepath */, const std::string * /* filename */,
const tinygltf::Image *image, bool /* embedImages */,
const tinygltf::FsCallbacks * /* fs_cb */, const tinygltf::URICallbacks * /* uri_cb */,
std::string * /* out_uri */, void * user_pointer) {
REQUIRE(user_pointer != nullptr);
auto counter = static_cast<int*>(user_pointer);
*counter = *counter + 1;
return true;
}
TEST_CASE("empty-images-not-written", "[issue-495]") {
std::string err;
std::string warn;
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
ctx.SetImageLoader(LoadImageData, nullptr);
bool ok = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/Cube/Cube.gltf");
REQUIRE(ok);
REQUIRE(err.empty());
REQUIRE(warn.empty());
CHECK(model.images.size() == 2);
for (const auto& image : model.images) {
// No data loaded or decoded
CHECK(image.image.empty());
// The URI is kept
CHECK_FALSE(image.uri.empty());
// The URI should not be a data URI
CHECK(image.uri.find("data:") != 0);
}
// Now write the loaded model
int counter = 0;
ctx.SetImageWriter(WriteImageData, &counter);
ok = ctx.WriteGltfSceneToFile(&model, "issue-495-external.gltf");
CHECK(ok);
// WriteImageData should be invoked for both images
CHECK(counter == 2);
}

View File

@@ -1721,6 +1721,9 @@ class TinyGLTF {
#endif // __GNUC__
#ifndef TINYGLTF_NO_INCLUDE_JSON
#ifdef TINYGLTF_USE_MINIJSON
#include "minijson.h"
#else // !TINYGLTF_USE_MINIJSON
#ifndef TINYGLTF_USE_RAPIDJSON
#include "json.hpp"
#else
@@ -1732,6 +1735,7 @@ class TinyGLTF {
#include "writer.h"
#endif
#endif
#endif // !TINYGLTF_USE_MINIJSON
#endif
#ifdef TINYGLTF_ENABLE_DRACO
@@ -2767,12 +2771,6 @@ 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
@@ -3315,12 +3313,11 @@ static bool UpdateImageObject(const Image &image, std::string &baseDir,
filename = std::to_string(index) + "." + ext;
}
// 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.
// 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.
bool imageWritten = false;
if (WriteImageData != nullptr && !filename.empty()) {
if (WriteImageData != nullptr && !filename.empty() && !image.image.empty()) {
imageWritten = WriteImageData(&baseDir, &filename, &image, embedImages,
fs_cb, uri_cb, out_uri, user_data);
if (!imageWritten) {
@@ -4394,7 +4391,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err,
}
} else {
// Assume external file
// Unconditionally keep the external URI of the image
// Keep texture path (for textures that cannot be decoded)
image->uri = uri;
#ifdef TINYGLTF_NO_EXTERNAL_IMAGE
return true;
@@ -4441,7 +4438,6 @@ 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);
}
@@ -5605,7 +5601,7 @@ static bool ParseAnimation(Animation *animation, std::string *err,
}
sampler.input = inputIndex;
sampler.output = outputIndex;
ParseExtrasAndExtensions(&sampler, err, s,
ParseExtrasAndExtensions(&sampler, err, o,
store_original_json_for_extras_and_extensions);
animation->samplers.emplace_back(std::move(sampler));
@@ -5668,7 +5664,7 @@ static bool ParseSkin(Skin *skin, std::string *err, const detail::json &o,
skin->skeleton = skeleton;
int invBind = -1;
ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", false, "Skin");
ParseIntegerProperty(&invBind, err, o, "inverseBindMatrices", true, "Skin");
skin->inverseBindMatrices = invBind;
ParseExtrasAndExtensions(skin, err, o,
@@ -6116,8 +6112,16 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
}
}
// Reset the model
(*model) = Model();
model->buffers.clear();
model->bufferViews.clear();
model->accessors.clear();
model->meshes.clear();
model->cameras.clear();
model->nodes.clear();
model->extensionsUsed.clear();
model->extensionsRequired.clear();
model->extensions.clear();
model->defaultScene = -1;
// 1. Parse Asset
{