From 5c2cc20313e14757e14399051dd814846b543b7f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 28 Nov 2014 17:36:55 -0800 Subject: [PATCH] fixes the stutter in animation when observer moves --- interface/src/renderer/AnimationHandle.cpp | 8 +++----- libraries/animation/src/AnimationLoop.cpp | 5 +++-- libraries/animation/src/AnimationLoop.h | 4 ++++ libraries/entities/src/ModelEntityItem.cpp | 4 +++- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/interface/src/renderer/AnimationHandle.cpp b/interface/src/renderer/AnimationHandle.cpp index 767f941049..89c265875b 100644 --- a/interface/src/renderer/AnimationHandle.cpp +++ b/interface/src/renderer/AnimationHandle.cpp @@ -138,11 +138,9 @@ void AnimationHandle::simulate(float deltaTime) { return; } - // TODO: When moving the loop/frame calculations to AnimationLoop class, we changed this behavior - // see AnimationLoop class for more details. Do we need to support clamping the endFrameIndex to - // the max number of frames in the geometry??? - // - // float endFrameIndex = qMin(_lastFrame, animationGeometry.animationFrames.size() - (_loop ? 0.0f : 1.0f)); + if (_animationLoop.getMaxFrameIndexHint() != animationGeometry.animationFrames.size()) { + _animationLoop.setMaxFrameIndexHint(animationGeometry.animationFrames.size()); + } // blend between the closest two frames applyFrame(getFrameIndex()); diff --git a/libraries/animation/src/AnimationLoop.cpp b/libraries/animation/src/AnimationLoop.cpp index f81904990f..75a3cdd338 100644 --- a/libraries/animation/src/AnimationLoop.cpp +++ b/libraries/animation/src/AnimationLoop.cpp @@ -20,7 +20,8 @@ AnimationLoop::AnimationLoop() : _firstFrame(0.0f), _lastFrame(FLT_MAX), _running(false), - _frameIndex(0.0f) + _frameIndex(0.0f), + _maxFrameIndexHint(FLT_MAX) { } @@ -55,7 +56,7 @@ void AnimationLoop::simulate(float deltaTime) { // If we knew the number of frames from the animation, we'd consider using it here // animationGeometry.animationFrames.size() - float maxFrame = _lastFrame; + float maxFrame = _maxFrameIndexHint; float endFrameIndex = qMin(_lastFrame, maxFrame - (_loop ? 0.0f : 1.0f)); float startFrameIndex = qMin(_firstFrame, endFrameIndex); if ((!_loop && (_frameIndex < startFrameIndex || _frameIndex > endFrameIndex)) || startFrameIndex == endFrameIndex) { diff --git a/libraries/animation/src/AnimationLoop.h b/libraries/animation/src/AnimationLoop.h index b56f68f23b..aff2cd86ee 100644 --- a/libraries/animation/src/AnimationLoop.h +++ b/libraries/animation/src/AnimationLoop.h @@ -44,6 +44,9 @@ public: void setFrameIndex(float frameIndex) { _frameIndex = glm::clamp(frameIndex, _firstFrame, _lastFrame); } float getFrameIndex() const { return _frameIndex; } + + void setMaxFrameIndexHint(float value) { _maxFrameIndexHint = value; } + float getMaxFrameIndexHint() const { return _maxFrameIndexHint; } void start() { setRunning(true); } void stop() { setRunning(false); } @@ -58,6 +61,7 @@ private: float _lastFrame; bool _running; float _frameIndex; + float _maxFrameIndexHint; }; #endif // hifi_AnimationLoop_h diff --git a/libraries/entities/src/ModelEntityItem.cpp b/libraries/entities/src/ModelEntityItem.cpp index 31a0c57f5b..63fe3daa03 100644 --- a/libraries/entities/src/ModelEntityItem.cpp +++ b/libraries/entities/src/ModelEntityItem.cpp @@ -344,7 +344,9 @@ QVector ModelEntityItem::getAnimationFrame() { Animation* myAnimation = getAnimation(_animationURL); QVector frames = myAnimation->getFrames(); int frameCount = frames.size(); - + if (_animationLoop.getMaxFrameIndexHint() != frameCount) { + _animationLoop.setMaxFrameIndexHint(frameCount); + } if (frameCount > 0) { int animationFrameIndex = (int)(glm::floor(getAnimationFrameIndex())) % frameCount; if (animationFrameIndex < 0 || animationFrameIndex > frameCount) {