From afcfb57898a6c0647b854820a38d70b90974811e Mon Sep 17 00:00:00 2001 From: Thomas Gamper Date: Thu, 23 Nov 2023 14:12:54 +0100 Subject: [PATCH] fix #457 tiny_gltf.h - access correct json object when serializing a light, this fixes an assert and causes us to serialze the light index properly; tester.cc - add respective testcase --- tests/tester.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ tiny_gltf.h | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/tester.cc b/tests/tester.cc index 1117a80..5803cfd 100644 --- a/tests/tester.cc +++ b/tests/tester.cc @@ -756,6 +756,52 @@ TEST_CASE("load-issue-416-model", "[issue-416]") { REQUIRE(true == ret); } +TEST_CASE("serialize-light-index", "[issue-457]") { + tinygltf::Model model; + tinygltf::Scene scene; + + // Create the light + tinygltf::Light light; + light.type = "point"; + light.intensity = 0.75; + light.color = std::vector{1.0, 0.8, 0.95}; + // Add the light to the model + model.lights.push_back(light); + // Create a node that uses the light + tinygltf::Node node; + node.light = 0; + // Add the node to the model + model.nodes.push_back(node); + // Add the node to the scene + scene.nodes.push_back(0); + // Add the scene to the model + model.scenes.push_back(scene); + + // Stream to serialize to + std::stringstream os; + + { + // Serialize model to output stream + tinygltf::TinyGLTF ctx; + bool ret = ctx.WriteGltfSceneToStream(&model, 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 == model.lights.size()); + CHECK(model.lights[0] == light); + // Check that the node properly references the light + REQUIRE(1 == model.nodes.size()); + CHECK(model.nodes[0].light == 0); + } +} + TEST_CASE("default-material", "[issue-459]") { const std::vector default_emissive_factor{ 0.0, 0.0, 0.0 }; const std::vector default_base_color_factor{ 1.0, 1.0, 1.0, 1.0 }; diff --git a/tiny_gltf.h b/tiny_gltf.h index 7275d8d..f957968 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -7752,7 +7752,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 {