#4109 Fix inside-out geometry from negative scale transforms in GLTF loader

meow-7.2.2
Andrey Lihatskiy 2025-05-25 00:53:25 +03:00
parent e8eac13b7b
commit 83fa366de9
1 changed files with 18 additions and 2 deletions

View File

@ -262,6 +262,9 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh&
// Combine transforms: coordinate rotation applied to hierarchy transform
const glm::mat4 final_transform = coord_system_rotation * hierarchy_transform;
// Check if we have a negative scale (flipped coordinate system)
bool hasNegativeScale = glm::determinant(final_transform) < 0.0f;
// Pre-compute normal transform matrix (transpose of inverse of upper-left 3x3)
const glm::mat3 normal_transform = glm::transpose(glm::inverse(glm::mat3(final_transform)));
@ -429,9 +432,22 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh&
vertices.push_back(vert);
}
for (U32 i = 0; i < prim.getIndexCount(); i++)
// When processing indices, flip winding order if needed
for (U32 i = 0; i < prim.getIndexCount(); i += 3)
{
indices.push_back(prim.mIndexArray[i]);
if (hasNegativeScale)
{
// Flip winding order for negative scale
indices.push_back(prim.mIndexArray[i]);
indices.push_back(prim.mIndexArray[i + 2]); // Swap these two
indices.push_back(prim.mIndexArray[i + 1]);
}
else
{
indices.push_back(prim.mIndexArray[i]);
indices.push_back(prim.mIndexArray[i + 1]);
indices.push_back(prim.mIndexArray[i + 2]);
}
}
// Check for empty vertex array before processing