Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions indra/newview/gltf/llgltfloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,6 @@ bool LLGLTFLoader::parseMeshes()
validate_model(pModel))
{
mModelList.push_back(pModel);
LLMatrix4 saved_transform = mTransform;

// This will make sure the matrix is always valid from the node.
node.makeMatrixValid();

mTransform.setIdentity();
transformation = mTransform;
Expand Down Expand Up @@ -292,7 +288,6 @@ bool LLGLTFLoader::parseMeshes()

mScene[transformation].push_back(LLModelInstance(pModel, pModel->mLabel, transformation, mats));
stretch_extents(pModel, transformation);
mTransform = saved_transform;
}
else
{
Expand Down Expand Up @@ -708,14 +703,30 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh&
{
// Process bind matrix
LL::GLTF::mat4 gltf_mat = gltf_skin.mInverseBindMatricesData[i];
LLMatrix4 gltf_transform(glm::value_ptr(gltf_mat));

// For inverse bind matrices, we need to:
// 1. Get the original bind matrix by inverting
// 2. Apply coordinate rotation to the original
// 3. Invert again to get the rotated inverse bind matrix
glm::mat4 original_bind_matrix = glm::inverse(gltf_mat);
glm::mat4 coord_rotation = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 rotated_original = coord_rotation * original_bind_matrix;
glm::mat4 rotated_inverse_bind_matrix = glm::inverse(rotated_original);

LLMatrix4 gltf_transform(glm::value_ptr(rotated_inverse_bind_matrix));
skin_info.mInvBindMatrix.push_back(LLMatrix4a(gltf_transform));

LL_INFOS("GLTF") << "mInvBindMatrix name: " << legal_name << " val: " << gltf_transform << LL_ENDL;
LL_INFOS("GLTF_DEBUG") << "mInvBindMatrix name: " << legal_name << " val: " << gltf_transform << LL_ENDL;

// For alternate bind matrix, use the ORIGINAL joint transform (before rotation)
// Get the original joint node and use its matrix directly
S32 joint = gltf_skin.mJoints[i];
LL::GLTF::Node& jointNode = mGLTFAsset.mNodes[joint];
jointNode.makeMatrixValid();
LLMatrix4 original_joint_transform(glm::value_ptr(jointNode.mMatrix));

// Translate based of mJointList
gltf_transform.setTranslation(mJointList[legal_name].getTranslation()); // name is supposed to be in mJointList
skin_info.mAlternateBindMatrix.push_back(LLMatrix4a(gltf_transform));
LL_INFOS("GLTF_DEBUG") << "mAlternateBindMatrix name: " << legal_name << " val: " << original_joint_transform << LL_ENDL;
skin_info.mAlternateBindMatrix.push_back(LLMatrix4a(original_joint_transform));
}
}

Expand All @@ -742,6 +753,11 @@ bool LLGLTFLoader::populateModelFromMesh(LLModel* pModel, const LL::GLTF::Mesh&

void LLGLTFLoader::populateJointFromSkin(const LL::GLTF::Skin& skin)
{
// Create coordinate system rotation matrix - GLTF is Y-up, SL is Z-up
glm::mat4 coord_system_rotation = glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));

LL_INFOS("GLTF_DEBUG") << "populateJointFromSkin: Processing " << skin.mJoints.size() << " joints" << LL_ENDL;

for (auto joint : skin.mJoints)
{
auto jointNode = mGLTFAsset.mNodes[joint];
Expand All @@ -754,17 +770,37 @@ void LLGLTFLoader::populateJointFromSkin(const LL::GLTF::Skin& skin)
else
{
// ignore unrecognized joint
LL_DEBUGS("GLTF") << "Ignoring joing: " << legal_name << LL_ENDL;
LL_DEBUGS("GLTF") << "Ignoring joint: " << legal_name << LL_ENDL;
continue;
}

jointNode.makeMatrixValid();

LLMatrix4 gltf_transform = LLMatrix4(glm::value_ptr(jointNode.mMatrix));
// Debug: Log original joint matrix
glm::mat4 gltf_joint_matrix = jointNode.mMatrix;
LL_INFOS("GLTF_DEBUG") << "Joint '" << legal_name << "' original matrix:" << LL_ENDL;
for(int i = 0; i < 4; i++)
{
LL_INFOS("GLTF_DEBUG") << " [" << gltf_joint_matrix[i][0] << ", " << gltf_joint_matrix[i][1]
<< ", " << gltf_joint_matrix[i][2] << ", " << gltf_joint_matrix[i][3] << "]" << LL_ENDL;
}

// Apply coordinate system rotation to joint transform
glm::mat4 rotated_joint_matrix = coord_system_rotation * gltf_joint_matrix;

// Debug: Log rotated joint matrix
LL_INFOS("GLTF_DEBUG") << "Joint '" << legal_name << "' rotated matrix:" << LL_ENDL;
for(int i = 0; i < 4; i++)
{
LL_INFOS("GLTF_DEBUG") << " [" << rotated_joint_matrix[i][0] << ", " << rotated_joint_matrix[i][1]
<< ", " << rotated_joint_matrix[i][2] << ", " << rotated_joint_matrix[i][3] << "]" << LL_ENDL;
}

LLMatrix4 gltf_transform = LLMatrix4(glm::value_ptr(rotated_joint_matrix));
mJointList[legal_name] = gltf_transform;
mJointsFromNode.push_front(legal_name);

LL_DEBUGS("GLTF") << "mJointList name: " << legal_name << " val: " << gltf_transform << LL_ENDL;
LL_INFOS("GLTF_DEBUG") << "mJointList name: " << legal_name << " val: " << gltf_transform << LL_ENDL;
}
}

Expand Down