From fb670275c2731d710d55634bb145e750b3d45e41 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 28 Jul 2014 16:42:16 -0700 Subject: [PATCH] Don't recompute unchaged transform --- interface/src/avatar/SkeletonModel.cpp | 3 ++- interface/src/renderer/JointState.cpp | 28 +++++++++++++++++++++----- interface/src/renderer/JointState.h | 5 ++++- interface/src/renderer/Model.cpp | 9 ++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/interface/src/avatar/SkeletonModel.cpp b/interface/src/avatar/SkeletonModel.cpp index 3caaad1391..dc6a309e70 100644 --- a/interface/src/avatar/SkeletonModel.cpp +++ b/interface/src/avatar/SkeletonModel.cpp @@ -52,7 +52,8 @@ const float PALM_PRIORITY = 3.0f; void SkeletonModel::simulate(float deltaTime, bool fullUpdate) { setTranslation(_owningAvatar->getPosition()); - setRotation(_owningAvatar->getOrientation() * glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f))); + static const glm::quat refOrientation = glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f)); + setRotation(_owningAvatar->getOrientation() * refOrientation); const float MODEL_SCALE = 0.0006f; setScale(glm::vec3(1.0f, 1.0f, 1.0f) * _owningAvatar->getScale() * MODEL_SCALE); diff --git a/interface/src/renderer/JointState.cpp b/interface/src/renderer/JointState.cpp index 2a4372401e..0c27db3942 100644 --- a/interface/src/renderer/JointState.cpp +++ b/interface/src/renderer/JointState.cpp @@ -19,6 +19,7 @@ JointState::JointState() : _animationPriority(0.0f), + _transformChanged(true), _positionInParentFrame(0.0f), _distanceToParent(0.0f), _fbxJoint(NULL), @@ -26,6 +27,7 @@ JointState::JointState() : } JointState::JointState(const JointState& other) : _constraint(NULL) { + _transformChanged = true; _transform = other._transform; _rotation = other._rotation; _rotationInConstrainedFrame = other._rotationInConstrainedFrame; @@ -47,7 +49,10 @@ JointState::~JointState() { void JointState::setFBXJoint(const FBXJoint* joint) { assert(joint != NULL); - _rotationInConstrainedFrame = joint->rotation; + if (joint->rotation != _rotationInConstrainedFrame) { + _rotationInConstrainedFrame = joint->rotation; + _transformChanged = true; + } // NOTE: JointState does not own the FBXJoint to which it points. _fbxJoint = joint; if (_constraint) { @@ -71,6 +76,7 @@ void JointState::updateConstraint() { void JointState::copyState(const JointState& state) { _animationPriority = state._animationPriority; _transform = state._transform; + _transformChanged = true; _rotation = extractRotation(_transform); _rotationInConstrainedFrame = state._rotationInConstrainedFrame; _positionInParentFrame = state._positionInParentFrame; @@ -88,11 +94,20 @@ void JointState::initTransform(const glm::mat4& parentTransform) { _distanceToParent = glm::length(_positionInParentFrame); } -void JointState::computeTransform(const glm::mat4& parentTransform) { +void JointState::computeTransform(const glm::mat4& parentTransform, bool parentTransformChanged) { + if (!parentTransformChanged && !_transformChanged) { + return; + } + glm::quat rotationInParentFrame = _fbxJoint->preRotation * _rotationInConstrainedFrame * _fbxJoint->postRotation; glm::mat4 transformInParentFrame = _fbxJoint->preTransform * glm::mat4_cast(rotationInParentFrame) * _fbxJoint->postTransform; - _transform = parentTransform * glm::translate(_fbxJoint->translation) * transformInParentFrame; - _rotation = extractRotation(_transform); + glm::mat4 newTransform = parentTransform * glm::translate(_fbxJoint->translation) * transformInParentFrame; + _transformChanged = true; + + if (newTransform != _transform) { + _transform = newTransform; + _rotation = extractRotation(_transform); + } } void JointState::computeVisibleTransform(const glm::mat4& parentTransform) { @@ -139,6 +154,7 @@ void JointState::clearTransformTranslation() { _transform[3][0] = 0.0f; _transform[3][1] = 0.0f; _transform[3][2] = 0.0f; + _transformChanged = true; _visibleTransform[3][0] = 0.0f; _visibleTransform[3][1] = 0.0f; _visibleTransform[3][2] = 0.0f; @@ -151,13 +167,15 @@ void JointState::setRotation(const glm::quat& rotation, bool constrain, float pr void JointState::applyRotationDelta(const glm::quat& delta, bool constrain, float priority) { // NOTE: delta is in model-frame assert(_fbxJoint != NULL); - if (priority < _animationPriority) { + if (priority < _animationPriority || delta.null) { return; } _animationPriority = priority; if (!constrain || _constraint == NULL) { // no constraints _rotationInConstrainedFrame = _rotationInConstrainedFrame * glm::inverse(_rotation) * delta * _rotation; + _transformChanged = true; + _rotation = delta * _rotation; return; } diff --git a/interface/src/renderer/JointState.h b/interface/src/renderer/JointState.h index 94811fe13c..ea506a5db9 100644 --- a/interface/src/renderer/JointState.h +++ b/interface/src/renderer/JointState.h @@ -33,7 +33,7 @@ public: void copyState(const JointState& state); void initTransform(const glm::mat4& parentTransform); - void computeTransform(const glm::mat4& parentTransform); + void computeTransform(const glm::mat4& parentTransform, bool parentTransformChanged = true); void computeVisibleTransform(const glm::mat4& parentTransform); const glm::mat4& getVisibleTransform() const { return _visibleTransform; } @@ -41,6 +41,8 @@ public: glm::vec3 getVisiblePosition() const { return extractTranslation(_visibleTransform); } const glm::mat4& getTransform() const { return _transform; } + void resetTransformChanged() { _transformChanged = false; } + bool getTransformChanged() const { return _transformChanged; } glm::quat getRotation() const { return _rotation; } glm::vec3 getPosition() const { return extractTranslation(_transform); } @@ -104,6 +106,7 @@ private: /// debug helper function void loadBindRotation(); + bool _transformChanged; glm::mat4 _transform; // joint- to model-frame glm::quat _rotation; // joint- to model-frame glm::quat _rotationInConstrainedFrame; // rotation in frame where angular constraints would be applied diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 8c19c11ed3..63a94772a7 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -922,7 +922,7 @@ void Model::simulate(float deltaTime, bool fullUpdate) { void Model::simulateInternal(float deltaTime) { // NOTE: this is a recursive call that walks all attachments, and their attachments // update the world space transforms for all joints - + // update animations foreach (const AnimationHandlePointer& handle, _runningAnimations) { handle->simulate(deltaTime); @@ -931,8 +931,11 @@ void Model::simulateInternal(float deltaTime) { for (int i = 0; i < _jointStates.size(); i++) { updateJointState(i); } + for (int i = 0; i < _jointStates.size(); i++) { + _jointStates[i].resetTransformChanged(); + } - _shapesAreDirty = ! _shapes.isEmpty(); + _shapesAreDirty = !_shapes.isEmpty(); // update the attachment transforms and simulate them const FBXGeometry& geometry = _geometry->getFBXGeometry(); @@ -994,7 +997,7 @@ void Model::updateJointState(int index) { state.computeTransform(parentTransform); } else { const JointState& parentState = _jointStates.at(parentIndex); - state.computeTransform(parentState.getTransform()); + state.computeTransform(parentState.getTransform(), parentState.getTransformChanged()); } }