From 1f8bed9d9c392c9f2c60d80bc6d32afe805a0b3e Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 22 Oct 2013 15:31:27 -0700 Subject: [PATCH] More transform rejiggery. --- interface/src/avatar/FaceModel.cpp | 55 ++++++++------------------ interface/src/avatar/FaceModel.h | 4 +- interface/src/avatar/SkeletonModel.cpp | 41 ++++++------------- interface/src/avatar/SkeletonModel.h | 2 + interface/src/renderer/Model.cpp | 26 +++++++++++- interface/src/renderer/Model.h | 4 ++ 6 files changed, 61 insertions(+), 71 deletions(-) diff --git a/interface/src/avatar/FaceModel.cpp b/interface/src/avatar/FaceModel.cpp index 22686998d3..94b81fcb60 100644 --- a/interface/src/avatar/FaceModel.cpp +++ b/interface/src/avatar/FaceModel.cpp @@ -44,42 +44,21 @@ void FaceModel::simulate(float deltaTime) { Model::simulate(deltaTime); } -void FaceModel::updateJointState(int index) { - JointState& state = _jointStates[index]; - const FBXGeometry& geometry = _geometry->getFBXGeometry(); - const FBXJoint& joint = geometry.joints.at(index); - - glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation; - if (joint.parentIndex == -1) { - glm::mat4 baseTransform = glm::translate(_translation) * glm::mat4_cast(_rotation) * - glm::scale(_scale) * glm::translate(_offset); - - state.transform = baseTransform * geometry.offset * joint.preTransform * - glm::mat4_cast(combinedRotation) * joint.postTransform; - state.combinedRotation = _rotation * combinedRotation; - - } else { - const JointState& parentState = _jointStates.at(joint.parentIndex); - if (index == geometry.neckJointIndex) { - // get the rotation axes in joint space and use them to adjust the rotation - glm::mat3 axes = glm::mat3_cast(getRotation()); - glm::mat3 inverse = glm::inverse(glm::mat3(parentState.transform * - joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation))); - state.rotation = glm::angleAxis(_owningHead->getRoll(), glm::normalize(inverse * axes[2])) * - glm::angleAxis(_owningHead->getYaw(), glm::normalize(inverse * axes[1])) * - glm::angleAxis(_owningHead->getPitch(), glm::normalize(inverse * axes[0])) * joint.rotation; - - } else if (index == geometry.leftEyeJointIndex || index == geometry.rightEyeJointIndex) { - // likewise with the eye joints - glm::mat4 inverse = glm::inverse(parentState.transform * - joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); - glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getOrientation() * IDENTITY_FRONT, 0.0f)); - glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + - _owningHead->getSaccade(), 1.0f)); - state.rotation = rotationBetween(front, lookAt) * joint.rotation; - } - state.transform = parentState.transform * joint.preTransform * - glm::mat4_cast(combinedRotation) * joint.postTransform; - state.combinedRotation = parentState.combinedRotation * combinedRotation; - } +void FaceModel::maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // get the rotation axes in joint space and use them to adjust the rotation + glm::mat3 axes = glm::mat3_cast(_rotation); + glm::quat inverse = parentState.combinedRotation * joint.preRotation * joint.rotation; + state.rotation = glm::angleAxis(_owningHead->getRoll(), glm::normalize(inverse * axes[2])) * + glm::angleAxis(_owningHead->getYaw(), glm::normalize(inverse * axes[1])) * + glm::angleAxis(_owningHead->getPitch(), glm::normalize(inverse * axes[0])) * joint.rotation; +} + +void FaceModel::maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // likewise with the eye joints + glm::mat4 inverse = glm::inverse(parentState.transform * + joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); + glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getOrientation() * IDENTITY_FRONT, 0.0f)); + glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + + _owningHead->getSaccade(), 1.0f)); + state.rotation = rotationBetween(front, lookAt) * joint.rotation; } diff --git a/interface/src/avatar/FaceModel.h b/interface/src/avatar/FaceModel.h index cd0a562298..acf2d2baf4 100644 --- a/interface/src/avatar/FaceModel.h +++ b/interface/src/avatar/FaceModel.h @@ -25,8 +25,8 @@ public: protected: - /// Updates the state of the joint at the specified index. - virtual void updateJointState(int index); + virtual void maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); + virtual void maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); private: diff --git a/interface/src/avatar/SkeletonModel.cpp b/interface/src/avatar/SkeletonModel.cpp index 0688290928..1a85c46cf6 100644 --- a/interface/src/avatar/SkeletonModel.cpp +++ b/interface/src/avatar/SkeletonModel.cpp @@ -58,38 +58,21 @@ bool SkeletonModel::render(float alpha) { } void SkeletonModel::updateJointState(int index) { - JointState& state = _jointStates[index]; - const FBXGeometry& geometry = _geometry->getFBXGeometry(); - const FBXJoint& joint = geometry.joints.at(index); + Model::updateJointState(index); - glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation; - if (joint.parentIndex == -1) { - glm::mat4 baseTransform = glm::translate(_translation) * glm::mat4_cast(_rotation) * - glm::scale(_scale) * glm::translate(_offset); - - state.transform = baseTransform * geometry.offset * joint.preTransform * - glm::mat4_cast(combinedRotation) * joint.postTransform; - state.combinedRotation = _rotation * combinedRotation; - - } else { - const JointState& parentState = _jointStates.at(joint.parentIndex); - if (index == geometry.leanJointIndex) { - // get the rotation axes in joint space and use them to adjust the rotation - state.combinedRotation = _rotation * glm::quat(glm::radians(glm::vec3(_owningAvatar->getHead().getLeanForward(), - 0.0f, _owningAvatar->getHead().getLeanSideways()))) * glm::inverse(_rotation) * parentState.combinedRotation * - joint.preRotation * joint.rotation; - state.rotation = glm::inverse(joint.postRotation * glm::inverse(state.combinedRotation) * - parentState.combinedRotation * joint.preRotation); - combinedRotation = joint.preRotation * state.rotation * joint.postRotation; - } - state.transform = parentState.transform * joint.preTransform * - glm::mat4_cast(combinedRotation) * joint.postTransform; - state.combinedRotation = parentState.combinedRotation * combinedRotation; - } - - if (index == geometry.rootJointIndex) { + if (index == _geometry->getFBXGeometry().rootJointIndex) { + JointState& state = _jointStates[index]; state.transform[3][0] = _translation.x; state.transform[3][1] = _translation.y; state.transform[3][2] = _translation.z; } } + +void SkeletonModel::maybeUpdateLeanRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // get the rotation axes in joint space and use them to adjust the rotation + glm::mat3 axes = glm::mat3_cast(_rotation); + glm::quat inverse = parentState.combinedRotation * joint.preRotation * joint.rotation; + state.rotation = glm::angleAxis(-_owningAvatar->getHead().getLeanSideways(), glm::normalize(inverse * axes[2])) * + glm::angleAxis(-_owningAvatar->getHead().getLeanForward(), glm::normalize(inverse * axes[0])) * joint.rotation; +} + diff --git a/interface/src/avatar/SkeletonModel.h b/interface/src/avatar/SkeletonModel.h index 9779a31656..dfda39d066 100644 --- a/interface/src/avatar/SkeletonModel.h +++ b/interface/src/avatar/SkeletonModel.h @@ -29,6 +29,8 @@ protected: /// Updates the state of the joint at the specified index. virtual void updateJointState(int index); + virtual void maybeUpdateLeanRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); + private: Avatar* _owningAvatar; diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 4f889da997..525f73e196 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -387,23 +387,45 @@ void Model::updateJointState(int index) { const FBXGeometry& geometry = _geometry->getFBXGeometry(); const FBXJoint& joint = geometry.joints.at(index); - glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation; if (joint.parentIndex == -1) { glm::mat4 baseTransform = glm::translate(_translation) * glm::mat4_cast(_rotation) * glm::scale(_scale) * glm::translate(_offset); - + + glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation; state.transform = baseTransform * geometry.offset * joint.preTransform * glm::mat4_cast(combinedRotation) * joint.postTransform; state.combinedRotation = _rotation * combinedRotation; } else { const JointState& parentState = _jointStates.at(joint.parentIndex); + if (index == geometry.leanJointIndex) { + maybeUpdateLeanRotation(parentState, joint, state); + + } else if (index == geometry.neckJointIndex) { + maybeUpdateNeckRotation(parentState, joint, state); + + } else if (index == geometry.leftEyeJointIndex || index == geometry.rightEyeJointIndex) { + maybeUpdateEyeRotation(parentState, joint, state); + } + glm::quat combinedRotation = joint.preRotation * state.rotation * joint.postRotation; state.transform = parentState.transform * joint.preTransform * glm::mat4_cast(combinedRotation) * joint.postTransform; state.combinedRotation = parentState.combinedRotation * combinedRotation; } } +void Model::maybeUpdateLeanRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // nothing by default +} + +void Model::maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // nothing by default +} + +void Model::maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, JointState& state) { + // nothing by default +} + bool Model::getJointPosition(int jointIndex, glm::vec3& position) const { if (jointIndex == -1 || _jointStates.isEmpty()) { return false; diff --git a/interface/src/renderer/Model.h b/interface/src/renderer/Model.h index 894de7bfe1..3bd1b2fa55 100644 --- a/interface/src/renderer/Model.h +++ b/interface/src/renderer/Model.h @@ -94,6 +94,10 @@ protected: /// Updates the state of the joint at the specified index. virtual void updateJointState(int index); + virtual void maybeUpdateLeanRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); + virtual void maybeUpdateNeckRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); + virtual void maybeUpdateEyeRotation(const JointState& parentState, const FBXJoint& joint, JointState& state); + private: bool getJointPosition(int jointIndex, glm::vec3& position) const;