Skip to content

Commit 3f0aa33

Browse files
committed
#4109 Improve LLGLTFLoader::computeCombinedNodeTransform()
1 parent d9d8008 commit 3f0aa33

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

indra/newview/gltf/llgltfloader.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,30 +251,35 @@ bool LLGLTFLoader::parseMeshes()
251251

252252
void LLGLTFLoader::computeCombinedNodeTransform(const LL::GLTF::Asset& asset, S32 node_index, glm::mat4& combined_transform)
253253
{
254-
auto& node = asset.mNodes[node_index];
254+
if (node_index < 0 || node_index >= static_cast<S32>(asset.mNodes.size()))
255+
{
256+
combined_transform = glm::mat4(1.0f);
257+
return;
258+
}
259+
260+
const auto& node = asset.mNodes[node_index];
261+
262+
// Ensure the node's matrix is valid
263+
const_cast<LL::GLTF::Node&>(node).makeMatrixValid();
255264

256265
// Start with this node's transform
257-
glm::mat4 node_transform = node.mMatrix;
266+
combined_transform = node.mMatrix;
258267

259-
// Find parent node and apply its transform if it exists
260-
for (auto& other_node : asset.mNodes)
268+
// Find and apply parent transform if it exists
269+
for (size_t i = 0; i < asset.mNodes.size(); ++i)
261270
{
262-
for (auto& child_index : other_node.mChildren)
263-
{
264-
if (child_index == node_index)
265-
{
266-
// Found a parent, recursively get its combined transform
267-
glm::mat4 parent_transform;
268-
computeCombinedNodeTransform(asset, static_cast<S32>(&other_node - &asset.mNodes[0]), parent_transform);
271+
const auto& potential_parent = asset.mNodes[i];
272+
auto it = std::find(potential_parent.mChildren.begin(), potential_parent.mChildren.end(), node_index);
269273

270-
// Apply parent transform to current node transform
271-
node_transform = parent_transform * node_transform;
272-
break;
273-
}
274+
if (it != potential_parent.mChildren.end())
275+
{
276+
// Found parent - recursively get its combined transform and apply it
277+
glm::mat4 parent_transform;
278+
computeCombinedNodeTransform(asset, static_cast<S32>(i), parent_transform);
279+
combined_transform = parent_transform * combined_transform;
280+
return; // Early exit - a node can only have one parent
274281
}
275282
}
276-
277-
combined_transform = node_transform;
278283
}
279284

280285
bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh& mesh, const LL::GLTF::Node& nodeno, material_map& mats, S32 instance_count)

0 commit comments

Comments
 (0)