mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-08-14 17:35:55 +08:00
Support conditional checking of glTF sections
This commit is contained in:
parent
cf6e07b1ec
commit
9e36b61ed5
@ -341,6 +341,18 @@ class Scene {
|
|||||||
Asset asset;
|
Asset asset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum SectionCheck
|
||||||
|
{
|
||||||
|
NO_REQUIRE = 0x00,
|
||||||
|
REQUIRE_SCENE = 0x01,
|
||||||
|
REQUIRE_SCENES = 0x02,
|
||||||
|
REQUIRE_NODES = 0x04,
|
||||||
|
REQUIRE_ACCESSORS = 0x08,
|
||||||
|
REQUIRE_BUFFERS = 0x10,
|
||||||
|
REQUIRE_BUFFER_VIEWS = 0x20,
|
||||||
|
REQUIRE_ALL = 0x3f
|
||||||
|
};
|
||||||
|
|
||||||
class TinyGLTFLoader {
|
class TinyGLTFLoader {
|
||||||
public:
|
public:
|
||||||
TinyGLTFLoader() : bin_data_(NULL), bin_size_(0), is_binary_(false) {
|
TinyGLTFLoader() : bin_data_(NULL), bin_size_(0), is_binary_(false) {
|
||||||
@ -351,19 +363,22 @@ class TinyGLTFLoader {
|
|||||||
/// Loads glTF ASCII asset from a file.
|
/// Loads glTF ASCII asset from a file.
|
||||||
/// Returns false and set error string to `err` if there's an error.
|
/// Returns false and set error string to `err` if there's an error.
|
||||||
bool LoadASCIIFromFile(Scene *scene, std::string *err,
|
bool LoadASCIIFromFile(Scene *scene, std::string *err,
|
||||||
const std::string &filename);
|
const std::string &filename,
|
||||||
|
unsigned int check_sections = REQUIRE_ALL);
|
||||||
|
|
||||||
/// Loads glTF ASCII asset from string(memory).
|
/// Loads glTF ASCII asset from string(memory).
|
||||||
/// `length` = strlen(str);
|
/// `length` = strlen(str);
|
||||||
/// Returns false and set error string to `err` if there's an error.
|
/// Returns false and set error string to `err` if there's an error.
|
||||||
bool LoadASCIIFromString(Scene *scene, std::string *err, const char *str,
|
bool LoadASCIIFromString(Scene *scene, std::string *err, const char *str,
|
||||||
const unsigned int length,
|
const unsigned int length,
|
||||||
const std::string &base_dir);
|
const std::string &base_dir,
|
||||||
|
unsigned int check_sections = REQUIRE_ALL);
|
||||||
|
|
||||||
/// Loads glTF binary asset from a file.
|
/// Loads glTF binary asset from a file.
|
||||||
/// Returns false and set error string to `err` if there's an error.
|
/// Returns false and set error string to `err` if there's an error.
|
||||||
bool LoadBinaryFromFile(Scene *scene, std::string *err,
|
bool LoadBinaryFromFile(Scene *scene, std::string *err,
|
||||||
const std::string &filename);
|
const std::string &filename,
|
||||||
|
unsigned int check_sections = REQUIRE_ALL);
|
||||||
|
|
||||||
/// Loads glTF binary asset from memory.
|
/// Loads glTF binary asset from memory.
|
||||||
/// `length` = strlen(str);
|
/// `length` = strlen(str);
|
||||||
@ -371,14 +386,16 @@ class TinyGLTFLoader {
|
|||||||
bool LoadBinaryFromMemory(Scene *scene, std::string *err,
|
bool LoadBinaryFromMemory(Scene *scene, std::string *err,
|
||||||
const unsigned char *bytes,
|
const unsigned char *bytes,
|
||||||
const unsigned int length,
|
const unsigned int length,
|
||||||
const std::string &base_dir = "");
|
const std::string &base_dir = "",
|
||||||
|
unsigned int check_sections = REQUIRE_ALL);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Loads glTF asset from string(memory).
|
/// Loads glTF asset from string(memory).
|
||||||
/// `length` = strlen(str);
|
/// `length` = strlen(str);
|
||||||
/// Returns false and set error string to `err` if there's an error.
|
/// Returns false and set error string to `err` if there's an error.
|
||||||
bool LoadFromString(Scene *scene, std::string *err, const char *str,
|
bool LoadFromString(Scene *scene, std::string *err, const char *str,
|
||||||
const unsigned int length, const std::string &base_dir);
|
const unsigned int length, const std::string &base_dir,
|
||||||
|
unsigned int check_sections);
|
||||||
|
|
||||||
const unsigned char *bin_data_;
|
const unsigned char *bin_data_;
|
||||||
size_t bin_size_;
|
size_t bin_size_;
|
||||||
@ -1905,7 +1922,8 @@ static bool ParseSampler(Sampler *sampler, std::string *err,
|
|||||||
|
|
||||||
bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
||||||
const char *str, unsigned int length,
|
const char *str, unsigned int length,
|
||||||
const std::string &base_dir) {
|
const std::string &base_dir,
|
||||||
|
unsigned int check_sections) {
|
||||||
picojson::value v;
|
picojson::value v;
|
||||||
std::string perr = picojson::parse(v, str, str + length);
|
std::string perr = picojson::parse(v, str, str + length);
|
||||||
|
|
||||||
@ -1918,7 +1936,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
if (v.contains("scene") && v.get("scene").is<std::string>()) {
|
if (v.contains("scene") && v.get("scene").is<std::string>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_SCENE) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"scene\" object not found in .gltf\n";
|
(*err) += "\"scene\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -1927,7 +1945,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
if (v.contains("scenes") && v.get("scenes").is<picojson::object>()) {
|
if (v.contains("scenes") && v.get("scenes").is<picojson::object>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_SCENES) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"scenes\" object not found in .gltf\n";
|
(*err) += "\"scenes\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -1936,7 +1954,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
if (v.contains("nodes") && v.get("nodes").is<picojson::object>()) {
|
if (v.contains("nodes") && v.get("nodes").is<picojson::object>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_NODES) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"nodes\" object not found in .gltf\n";
|
(*err) += "\"nodes\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -1945,7 +1963,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
if (v.contains("accessors") && v.get("accessors").is<picojson::object>()) {
|
if (v.contains("accessors") && v.get("accessors").is<picojson::object>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_ACCESSORS) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"accessors\" object not found in .gltf\n";
|
(*err) += "\"accessors\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -1954,7 +1972,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
if (v.contains("buffers") && v.get("buffers").is<picojson::object>()) {
|
if (v.contains("buffers") && v.get("buffers").is<picojson::object>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_BUFFERS) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"buffers\" object not found in .gltf\n";
|
(*err) += "\"buffers\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -1964,7 +1982,7 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
if (v.contains("bufferViews") &&
|
if (v.contains("bufferViews") &&
|
||||||
v.get("bufferViews").is<picojson::object>()) {
|
v.get("bufferViews").is<picojson::object>()) {
|
||||||
// OK
|
// OK
|
||||||
} else {
|
} else if (check_sections & REQUIRE_BUFFER_VIEWS) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) += "\"bufferViews\" object not found in .gltf\n";
|
(*err) += "\"bufferViews\" object not found in .gltf\n";
|
||||||
}
|
}
|
||||||
@ -2261,16 +2279,18 @@ bool TinyGLTFLoader::LoadFromString(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
bool TinyGLTFLoader::LoadASCIIFromString(Scene *scene, std::string *err,
|
bool TinyGLTFLoader::LoadASCIIFromString(Scene *scene, std::string *err,
|
||||||
const char *str, unsigned int length,
|
const char *str, unsigned int length,
|
||||||
const std::string &base_dir) {
|
const std::string &base_dir,
|
||||||
|
unsigned int check_sections) {
|
||||||
is_binary_ = false;
|
is_binary_ = false;
|
||||||
bin_data_ = NULL;
|
bin_data_ = NULL;
|
||||||
bin_size_ = 0;
|
bin_size_ = 0;
|
||||||
|
|
||||||
return LoadFromString(scene, err, str, length, base_dir);
|
return LoadFromString(scene, err, str, length, base_dir, check_sections);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TinyGLTFLoader::LoadASCIIFromFile(Scene *scene, std::string *err,
|
bool TinyGLTFLoader::LoadASCIIFromFile(Scene *scene, std::string *err,
|
||||||
const std::string &filename) {
|
const std::string &filename,
|
||||||
|
unsigned int check_sections) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
std::ifstream f(filename.c_str());
|
std::ifstream f(filename.c_str());
|
||||||
@ -2292,8 +2312,10 @@ bool TinyGLTFLoader::LoadASCIIFromFile(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
std::string basedir = GetBaseDir(filename);
|
std::string basedir = GetBaseDir(filename);
|
||||||
|
|
||||||
bool ret = LoadASCIIFromString(
|
bool ret = LoadASCIIFromString(scene, err, &buf.at(0),
|
||||||
scene, err, &buf.at(0), static_cast<unsigned int>(buf.size()), basedir);
|
static_cast<unsigned int>(buf.size()), basedir,
|
||||||
|
check_sections);
|
||||||
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -2301,7 +2323,8 @@ bool TinyGLTFLoader::LoadASCIIFromFile(Scene *scene, std::string *err,
|
|||||||
bool TinyGLTFLoader::LoadBinaryFromMemory(Scene *scene, std::string *err,
|
bool TinyGLTFLoader::LoadBinaryFromMemory(Scene *scene, std::string *err,
|
||||||
const unsigned char *bytes,
|
const unsigned char *bytes,
|
||||||
unsigned int size,
|
unsigned int size,
|
||||||
const std::string &base_dir) {
|
const std::string &base_dir,
|
||||||
|
unsigned int check_sections) {
|
||||||
if (size < 20) {
|
if (size < 20) {
|
||||||
if (err) {
|
if (err) {
|
||||||
(*err) = "Too short data size for glTF Binary.";
|
(*err) = "Too short data size for glTF Binary.";
|
||||||
@ -2353,7 +2376,7 @@ bool TinyGLTFLoader::LoadBinaryFromMemory(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
bool ret =
|
bool ret =
|
||||||
LoadFromString(scene, err, reinterpret_cast<const char *>(&bytes[20]),
|
LoadFromString(scene, err, reinterpret_cast<const char *>(&bytes[20]),
|
||||||
scene_length, base_dir);
|
scene_length, base_dir, check_sections);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -2362,7 +2385,8 @@ bool TinyGLTFLoader::LoadBinaryFromMemory(Scene *scene, std::string *err,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool TinyGLTFLoader::LoadBinaryFromFile(Scene *scene, std::string *err,
|
bool TinyGLTFLoader::LoadBinaryFromFile(Scene *scene, std::string *err,
|
||||||
const std::string &filename) {
|
const std::string &filename,
|
||||||
|
unsigned int check_sections) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
|
||||||
std::ifstream f(filename.c_str());
|
std::ifstream f(filename.c_str());
|
||||||
@ -2386,7 +2410,7 @@ bool TinyGLTFLoader::LoadBinaryFromFile(Scene *scene, std::string *err,
|
|||||||
|
|
||||||
bool ret = LoadBinaryFromMemory(
|
bool ret = LoadBinaryFromMemory(
|
||||||
scene, err, reinterpret_cast<unsigned char *>(&buf.at(0)),
|
scene, err, reinterpret_cast<unsigned char *>(&buf.at(0)),
|
||||||
static_cast<unsigned int>(buf.size()), basedir);
|
static_cast<unsigned int>(buf.size()), basedir, check_sections);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user