From 53d5b562024afcb72fe5e44a6d9cd6c1b73d365d Mon Sep 17 00:00:00 2001 From: Cedric Pinson Date: Wed, 9 Dec 2009 18:45:46 +0000 Subject: [PATCH] From Cedric Pinson, Add check in RigTransformSoftware if bones are null Indent TimelineAnimationManager Add check for NaN in UpdateCallback.cpp Fix TimelineAnimationManager clear target (a refactore of Timeline is require for futur) Fix Computation of bounding box for RigGeometry --- include/osgAnimation/AnimationManagerBase | 5 +-- include/osgAnimation/Interpolator | 4 +- include/osgAnimation/RigTransformSoftware | 7 ++- include/osgAnimation/Timeline | 13 +++--- include/osgAnimation/TimelineAnimationManager | 30 ++++++------- src/osgAnimation/RigGeometry.cpp | 43 ++++++++++++++++++- src/osgAnimation/TimelineAnimationManager.cpp | 3 +- src/osgAnimation/UpdateCallback.cpp | 10 +++-- 8 files changed, 81 insertions(+), 34 deletions(-) diff --git a/include/osgAnimation/AnimationManagerBase b/include/osgAnimation/AnimationManagerBase index 69156e1a3..b501b626d 100644 --- a/include/osgAnimation/AnimationManagerBase +++ b/include/osgAnimation/AnimationManagerBase @@ -12,8 +12,8 @@ * OpenSceneGraph Public License for more details. */ -#ifndef OSGANIMATION_ANIMATION_MANAGER_BASE_H -#define OSGANIMATION_ANIMATION_MANAGER_BASE_H +#ifndef OSGANIMATION_ANIMATION_MANAGER_BASE +#define OSGANIMATION_ANIMATION_MANAGER_BASE 1 #include #include @@ -55,7 +55,6 @@ namespace osgAnimation /// set a flag to define the behaviour void setAutomaticLink(bool); bool isAutomaticLink() const; - void dirty(); protected: diff --git a/include/osgAnimation/Interpolator b/include/osgAnimation/Interpolator index 3b0a331f2..f1c7eddac 100644 --- a/include/osgAnimation/Interpolator +++ b/include/osgAnimation/Interpolator @@ -16,8 +16,8 @@ * Michael Platings */ -#ifndef OSGANIMATION_INTERPOLATOR_H -#define OSGANIMATION_INTERPOLATOR_H +#ifndef OSGANIMATION_INTERPOLATOR +#define OSGANIMATION_INTERPOLATOR 1 #include #include diff --git a/include/osgAnimation/RigTransformSoftware b/include/osgAnimation/RigTransformSoftware index 76b76cca8..15cacd60a 100644 --- a/include/osgAnimation/RigTransformSoftware +++ b/include/osgAnimation/RigTransformSoftware @@ -86,7 +86,7 @@ namespace osgAnimation { if (_bones.empty()) { - osg::notify(osg::WARN) << "RigTransformSoftware::UniqBoneSetVertexSet no bones found" << std::endl; + osg::notify(osg::WARN) << this << " RigTransformSoftware::UniqBoneSetVertexSet no bones found" << std::endl; _result = osg::Matrix::identity(); return; } @@ -96,6 +96,11 @@ namespace osgAnimation for (int i = 0; i < size; i++) { const Bone* bone = _bones[i].getBone(); + if (!bone) + { + osg::notify(osg::WARN) << this << " RigTransformSoftware::computeMatrixForVertexSet Warning a bone is null, skip it" << std::endl; + continue; + } const osg::Matrix& invBindMatrix = bone->getInvBindMatrixInSkeletonSpace(); const osg::Matrix& matrix = bone->getMatrixInSkeletonSpace(); osg::Matrix::value_type w = _bones[i].getWeight(); diff --git a/include/osgAnimation/Timeline b/include/osgAnimation/Timeline index 42c0ae998..e578f2430 100644 --- a/include/osgAnimation/Timeline +++ b/include/osgAnimation/Timeline @@ -12,12 +12,13 @@ * OpenSceneGraph Public License for more details. */ -#ifndef OSGANIMATION_TIMELINE_H -#define OSGANIMATION_TIMELINE_H +#ifndef OSGANIMATION_TIMELINE +#define OSGANIMATION_TIMELINE 1 #include #include #include +#include #include #include #include @@ -27,7 +28,7 @@ namespace osgAnimation { - class OSGANIMATION_EXPORT Timeline : public Action //osg::Object + class OSGANIMATION_EXPORT Timeline : public Action { public: @@ -79,9 +80,9 @@ namespace osgAnimation const ActionLayers& getActionLayers() const { return _actions; } void processPendingOperation(); - + void setAnimationManager(AnimationManagerBase*); protected: - + osg::observer_ptr _animationManager; ActionLayers _actions; double _lastUpdate; double _speed; @@ -89,7 +90,7 @@ namespace osgAnimation unsigned int _previousFrameEvaluated; bool _initFirstFrame; TimelineStatus _state; - + bool _collectStats; osg::ref_ptr _stats; osg::ref_ptr _statsVisitor; diff --git a/include/osgAnimation/TimelineAnimationManager b/include/osgAnimation/TimelineAnimationManager index 7392b0c72..68574ec5e 100644 --- a/include/osgAnimation/TimelineAnimationManager +++ b/include/osgAnimation/TimelineAnimationManager @@ -12,8 +12,8 @@ * OpenSceneGraph Public License for more details. */ -#ifndef OSGANIMATION_TIMELINE_ANIMATION_MANAGER_H -#define OSGANIMATION_TIMELINE_ANIMATION_MANAGER_H +#ifndef OSGANIMATION_TIMELINE_ANIMATION_MANAGER +#define OSGANIMATION_TIMELINE_ANIMATION_MANAGER 1 #include #include @@ -23,21 +23,21 @@ namespace osgAnimation { -class OSGANIMATION_EXPORT TimelineAnimationManager : public AnimationManagerBase -{ -protected: - osg::ref_ptr _timeline; + class OSGANIMATION_EXPORT TimelineAnimationManager : public AnimationManagerBase + { + protected: + osg::ref_ptr _timeline; -public: - META_Object(osgAnimation, TimelineAnimationManager); - TimelineAnimationManager(); - TimelineAnimationManager(const AnimationManagerBase& manager); - TimelineAnimationManager(const TimelineAnimationManager& nc,const osg::CopyOp&); + public: + META_Object(osgAnimation, TimelineAnimationManager); + TimelineAnimationManager(); + TimelineAnimationManager(const AnimationManagerBase& manager); + TimelineAnimationManager(const TimelineAnimationManager& nc,const osg::CopyOp&); - Timeline* getTimeline() { return _timeline.get(); } - const Timeline* getTimeline() const { return _timeline.get(); } - void update(double time); -}; + Timeline* getTimeline() { return _timeline.get(); } + const Timeline* getTimeline() const { return _timeline.get(); } + void update(double time); + }; } diff --git a/src/osgAnimation/RigGeometry.cpp b/src/osgAnimation/RigGeometry.cpp index bc23e51b0..71e4c6f92 100644 --- a/src/osgAnimation/RigGeometry.cpp +++ b/src/osgAnimation/RigGeometry.cpp @@ -19,6 +19,45 @@ using namespace osgAnimation; +// The idea is to compute a bounding box with a factor x of the first step we compute the bounding box +class RigComputeBoundingBoxCallback : public osg::Drawable::ComputeBoundingBoxCallback +{ +public: + RigComputeBoundingBoxCallback(double factor = 2.0) : _computed(false), _factor(factor) {} + void reset() { _computed = false; } + virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const + { + const osgAnimation::RigGeometry& rig = dynamic_cast(drawable); + + // if a valid inital bounding box is set we use it without asking more + if (rig.getInitialBound().valid()) + return rig.getInitialBound(); + + if (_computed) + return _boundingBox; + + // if the computing of bb is invalid (like no geometry inside) + // then dont tag the bounding box as computed + osg::BoundingBox bb = rig.computeBound(); + if (!bb.valid()) + return bb; + + + _boundingBox.expandBy(bb); + osg::Vec3 center = _boundingBox.center(); + osg::Vec3 vec = (_boundingBox._max-center)*_factor; + _boundingBox.expandBy(center + vec); + _boundingBox.expandBy(center - vec); + _computed = true; +// osg::notify(osg::NOTICE) << "build the bounding box for RigGeometry " << rig.getName() << " " << _boundingBox._min << " " << _boundingBox._max << std::endl; + return _boundingBox; + } +protected: + mutable bool _computed; + double _factor; + mutable osg::BoundingBox _boundingBox; +}; + RigGeometry::RigGeometry() { _supportsDisplayList = false; @@ -29,7 +68,7 @@ RigGeometry::RigGeometry() _matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity(); // disable the computation of boundingbox for the rig mesh - setComputeBoundingBoxCallback(new ComputeBoundingBoxCallback); + setComputeBoundingBoxCallback(new RigComputeBoundingBoxCallback); } RigGeometry::RigGeometry(const osg::Geometry& b) : osg::Geometry(b, osg::CopyOp::SHALLOW_COPY) @@ -42,7 +81,7 @@ RigGeometry::RigGeometry(const osg::Geometry& b) : osg::Geometry(b, osg::CopyOp: _matrixFromSkeletonToGeometry = _invMatrixFromSkeletonToGeometry = osg::Matrix::identity(); // disable the computation of boundingbox for the rig mesh - setComputeBoundingBoxCallback(new ComputeBoundingBoxCallback); + setComputeBoundingBoxCallback(new RigComputeBoundingBoxCallback); } RigGeometry::RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop) : diff --git a/src/osgAnimation/TimelineAnimationManager.cpp b/src/osgAnimation/TimelineAnimationManager.cpp index 65bb71cd9..97a7cd8f6 100644 --- a/src/osgAnimation/TimelineAnimationManager.cpp +++ b/src/osgAnimation/TimelineAnimationManager.cpp @@ -34,6 +34,7 @@ TimelineAnimationManager::TimelineAnimationManager(const TimelineAnimationManage void TimelineAnimationManager::update(double time) { - clearTargets(); + // clearTargets(); + _timeline->setAnimationManager(this); _timeline->update(time); } diff --git a/src/osgAnimation/UpdateCallback.cpp b/src/osgAnimation/UpdateCallback.cpp index d790e52ec..be36ea470 100644 --- a/src/osgAnimation/UpdateCallback.cpp +++ b/src/osgAnimation/UpdateCallback.cpp @@ -19,6 +19,7 @@ #include #include #include +#include using namespace osgAnimation; @@ -69,10 +70,11 @@ void UpdateTransform::update(osg::MatrixTransform& mat) osg::Matrix::rotate(x,1.0,0.0,0.0) * osg::Matrix::rotate(y,0.0,1.0,0.0) * osg::Matrix::rotate(z,0.0,0.0,1.0); - mat.setMatrix(osg::Matrix::scale(_scale->getValue()) * - m * - osg::Matrix::translate(_position->getValue())); - mat.dirtyBound(); + m = osg::Matrix::scale(_scale->getValue()) * m * osg::Matrix::translate(_position->getValue()); + mat.setMatrix(m); + + if (!m.valid()) + osg::notify(osg::WARN) << this << " UpdateTransform::update detected NaN" << std::endl; } void UpdateTransform::update(osg::PositionAttitudeTransform& pat)