mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-07-04 21:18:52 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f03fe26579 | ||
|
|
e54660fbf9 | ||
|
|
1bdd404c04 | ||
|
|
2191085580 | ||
|
|
cde43ef668 | ||
|
|
e3f9a7d8b3 | ||
|
|
f57d18ad74 | ||
|
|
ed3d1ec2f5 | ||
|
|
9b4e1eae9e | ||
|
|
cbc8e1bea6 | ||
|
|
212de904ca | ||
|
|
1f5b8f8b8c | ||
|
|
b274b34972 | ||
|
|
22dfeab315 | ||
|
|
b132612307 | ||
|
|
50d90c91ac |
@@ -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;
|
||||
|
||||
@@ -978,19 +978,20 @@ TEST_CASE("serialize-lods", "[lods]") {
|
||||
{
|
||||
tinygltf::Model m;
|
||||
|
||||
m.nodes.resize(3);
|
||||
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(3);
|
||||
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 as root node
|
||||
// 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);
|
||||
|
||||
@@ -1006,28 +1007,52 @@ TEST_CASE("serialize-lods", "[lods]") {
|
||||
// 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 three materials are there
|
||||
REQUIRE(3 == m.materials.size());
|
||||
// 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 three nodes are there
|
||||
REQUIRE(3 == m.nodes.size());
|
||||
// Make sure the single root node of the scene is there
|
||||
REQUIRE(1 == m.scenes[0].nodes.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]);
|
||||
// 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(2 == node.lods.size());
|
||||
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 == node.lods[0]);
|
||||
CHECK(2 == node.lods[1]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
67
tiny_gltf.h
67
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;
|
||||
}
|
||||
|
||||
@@ -1927,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;
|
||||
}
|
||||
@@ -2246,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 "";
|
||||
}
|
||||
|
||||
@@ -3020,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";
|
||||
}
|
||||
@@ -3043,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
|
||||
}
|
||||
@@ -3068,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;
|
||||
@@ -3115,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";
|
||||
}
|
||||
@@ -3151,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);
|
||||
@@ -7070,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;
|
||||
@@ -7127,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);
|
||||
@@ -8334,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);
|
||||
@@ -8352,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);
|
||||
@@ -8437,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