Align forearms with wrists. Closes #2577.

This commit is contained in:
Andrzej Kapolka 2014-04-01 16:01:29 -07:00
parent fb37ef5bca
commit 865ca7de20

View file

@ -133,12 +133,17 @@ void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJoin
return; return;
} }
const FBXGeometry& geometry = _geometry->getFBXGeometry(); const FBXGeometry& geometry = _geometry->getFBXGeometry();
setJointPosition(jointIndex, palm.getPosition());
float sign = (jointIndex == geometry.rightHandJointIndex) ? 1.0f : -1.0f; float sign = (jointIndex == geometry.rightHandJointIndex) ? 1.0f : -1.0f;
int parentJointIndex = geometry.joints.at(jointIndex).parentIndex;
if (parentJointIndex == -1) {
return;
}
// rotate forearm to align with palm direction
glm::quat palmRotation; glm::quat palmRotation;
getJointRotation(jointIndex, palmRotation, true); getJointRotation(parentJointIndex, palmRotation, true);
applyRotationDelta(jointIndex, rotationBetween(palmRotation * geometry.palmDirection, palm.getNormal()), false); applyRotationDelta(parentJointIndex, rotationBetween(palmRotation * geometry.palmDirection, palm.getNormal()), false);
getJointRotation(jointIndex, palmRotation, true); getJointRotation(parentJointIndex, palmRotation, true);
// sort the finger indices by raw x, get the average direction // sort the finger indices by raw x, get the average direction
QVector<IndexValue> fingerIndices; QVector<IndexValue> fingerIndices;
@ -155,33 +160,21 @@ void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJoin
} }
qSort(fingerIndices.begin(), fingerIndices.end()); qSort(fingerIndices.begin(), fingerIndices.end());
// rotate palm according to average finger direction // rotate forearm according to average finger direction
float directionLength = glm::length(direction); float directionLength = glm::length(direction);
const unsigned int MIN_ROTATION_FINGERS = 3; const unsigned int MIN_ROTATION_FINGERS = 3;
if (directionLength > EPSILON && palm.getNumFingers() >= MIN_ROTATION_FINGERS) { if (directionLength > EPSILON && palm.getNumFingers() >= MIN_ROTATION_FINGERS) {
applyRotationDelta(jointIndex, rotationBetween(palmRotation * glm::vec3(-sign, 0.0f, 0.0f), direction), false); applyRotationDelta(parentJointIndex, rotationBetween(palmRotation * glm::vec3(-sign, 0.0f, 0.0f), direction), false);
getJointRotation(jointIndex, palmRotation, true); getJointRotation(parentJointIndex, palmRotation, true);
} }
// no point in continuing if there are no fingers // let wrist inherit forearm rotation
if (palm.getNumFingers() == 0 || fingerJointIndices.isEmpty()) { _jointStates[jointIndex].rotation = glm::quat();
return;
}
// match them up as best we can // set elbow position from wrist position
float proportion = fingerIndices.size() / (float)fingerJointIndices.size(); glm::vec3 forearmVector = palmRotation * glm::vec3(sign, 0.0f, 0.0f);
for (int i = 0; i < fingerJointIndices.size(); i++) { setJointPosition(parentJointIndex, palm.getPosition() + forearmVector *
int fingerIndex = fingerIndices.at(roundf(i * proportion)).index; geometry.joints.at(jointIndex).distanceToParent * extractUniformScale(_scale));
glm::vec3 fingerVector = palm.getFingers()[fingerIndex].getTipPosition() -
palm.getFingers()[fingerIndex].getRootPosition();
int fingerJointIndex = fingerJointIndices.at(i);
int fingertipJointIndex = fingertipJointIndices.at(i);
glm::vec3 jointVector = extractTranslation(geometry.joints.at(fingertipJointIndex).bindTransform) -
extractTranslation(geometry.joints.at(fingerJointIndex).bindTransform);
setJointRotation(fingerJointIndex, rotationBetween(palmRotation * jointVector, fingerVector) * palmRotation, true);
}
} }
void SkeletonModel::updateJointState(int index) { void SkeletonModel::updateJointState(int index) {