Skip to content

Commit

Permalink
fix #457
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptc-tgamper committed Nov 23, 2023
1 parent b6e2398 commit afcfb57
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions tests/tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>{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<double> default_emissive_factor{ 0.0, 0.0, 0.0 };
const std::vector<double> default_base_color_factor{ 1.0, 1.0, 1.0, 1.0 };
Expand Down
2 changes: 1 addition & 1 deletion tiny_gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit afcfb57

Please sign in to comment.