@@ -251,30 +251,35 @@ bool LLGLTFLoader::parseMeshes()
251
251
252
252
void LLGLTFLoader::computeCombinedNodeTransform (const LL::GLTF::Asset& asset, S32 node_index, glm::mat4& combined_transform)
253
253
{
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 ();
255
264
256
265
// Start with this node's transform
257
- glm::mat4 node_transform = node.mMatrix ;
266
+ combined_transform = node.mMatrix ;
258
267
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 )
261
270
{
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);
269
273
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
274
281
}
275
282
}
276
-
277
- combined_transform = node_transform;
278
283
}
279
284
280
285
bool LLGLTFLoader::populateModelFromMesh (LLModel* pModel, const LL::GLTF::Mesh& mesh, const LL::GLTF::Node& nodeno, material_map& mats, S32 instance_count)
0 commit comments