diff --git a/include/osgAnimation/RigGeometry b/include/osgAnimation/RigGeometry index 3015a3a64..76dd2519e 100644 --- a/include/osgAnimation/RigGeometry +++ b/include/osgAnimation/RigGeometry @@ -24,6 +24,28 @@ namespace osgAnimation { + // The idea is to compute a bounding box with a factor x of the first step we compute the bounding box + class OSGANIMATION_EXPORT RigComputeBoundingBoxCallback : public osg::Drawable::ComputeBoundingBoxCallback + { + public: + RigComputeBoundingBoxCallback(double factor = 2.0): _computed(false), _factor(factor) {} + + RigComputeBoundingBoxCallback(const RigComputeBoundingBoxCallback& rhs, const osg::CopyOp& copyop) : + osg::Drawable::ComputeBoundingBoxCallback(rhs, copyop), + _computed(false), + _factor(rhs._factor) {} + + META_Object(osgAnimation, RigComputeBoundingBoxCallback); + + + void reset() { _computed = false; } + virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const; + protected: + mutable bool _computed; + double _factor; + mutable osg::BoundingBox _boundingBox; + }; + class OSGANIMATION_EXPORT RigGeometry : public osg::Geometry { public: @@ -70,6 +92,44 @@ namespace osgAnimation void copyFrom(osg::Geometry& from); + struct UpdateVertex : public osg::Drawable::UpdateCallback + { + UpdateVertex() {} + + UpdateVertex(const UpdateCallback&, const osg::CopyOp&) {} + + META_Object(osgAnimation, UpdateVertex); + + virtual void update(osg::NodeVisitor*, osg::Drawable* drw) { + RigGeometry* geom = dynamic_cast(drw); + if(!geom) + return; + if(!geom->getSkeleton() && !geom->getParents().empty()) + { + FindNearestParentSkeleton finder; + if(geom->getParents().size() > 1) + osg::notify(osg::WARN) << "A RigGeometry should not have multi parent ( " << geom->getName() << " )" << std::endl; + geom->getParents()[0]->accept(finder); + + if(!finder._root.valid()) + { + osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeomtry ( " << geom->getName() << " )" << std::endl; + return; + } + geom->buildVertexInfluenceSet(); + geom->setSkeleton(finder._root.get()); + } + + if(!geom->getSkeleton()) + return; + + if(geom->getNeedToComputeMatrix()) + geom->computeMatrixFromRootSkeleton(); + + geom->update(); + } + }; + protected: osg::ref_ptr _geometry; @@ -96,39 +156,6 @@ namespace osgAnimation } }; - - struct UpdateVertex : public osg::Drawable::UpdateCallback - { - virtual void update(osg::NodeVisitor*, osg::Drawable* drw) - { - RigGeometry* geom = dynamic_cast(drw); - if (!geom) - return; - if (!geom->getSkeleton() && !geom->getParents().empty()) - { - FindNearestParentSkeleton finder; - if (geom->getParents().size() > 1) - osg::notify(osg::WARN) << "A RigGeometry should not have multi parent ( " << geom->getName() << " )" << std::endl; - geom->getParents()[0]->accept(finder); - - if (!finder._root.valid()) - { - osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeomtry ( " << geom->getName() << " )" << std::endl; - return; - } - geom->buildVertexInfluenceSet(); - geom->setSkeleton(finder._root.get()); - } - - if (!geom->getSkeleton()) - return; - - if (geom->getNeedToComputeMatrix()) - geom->computeMatrixFromRootSkeleton(); - - geom->update(); - } - }; }; } diff --git a/src/osgAnimation/RigGeometry.cpp b/src/osgAnimation/RigGeometry.cpp index bd2957d0f..3fd358607 100644 --- a/src/osgAnimation/RigGeometry.cpp +++ b/src/osgAnimation/RigGeometry.cpp @@ -21,43 +21,34 @@ 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 +osg::BoundingBox RigComputeBoundingBoxCallback::computeBound(const osg::Drawable& drawable) const { -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); + 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 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_NOTICE << "build the bounding box for RigGeometry " << rig.getName() << " " << _boundingBox._min << " " << _boundingBox._max << std::endl; + if (_computed) return _boundingBox; - } -protected: - mutable bool _computed; - double _factor; - mutable osg::BoundingBox _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_NOTICE << "build the bounding box for RigGeometry " << rig.getName() << " " << _boundingBox._min << " " << _boundingBox._max << std::endl; + return _boundingBox; +} + RigGeometry::RigGeometry() { diff --git a/src/osgWrappers/serializers/osg/ComputeBoundingBoxCallback.cpp b/src/osgWrappers/serializers/osg/ComputeBoundingBoxCallback.cpp new file mode 100644 index 000000000..f07bb1a1f --- /dev/null +++ b/src/osgWrappers/serializers/osg/ComputeBoundingBoxCallback.cpp @@ -0,0 +1,17 @@ +#undef OBJECT_CAST +#define OBJECT_CAST dynamic_cast + +#include +#include +#include +#include + +REGISTER_OBJECT_WRAPPER2(osg_ComputeBoundingBoxCallback, + new osg::Drawable::ComputeBoundingBoxCallback, + osg::Drawable::ComputeBoundingBoxCallback, + "osg::ComputeBoundingBoxCallback", + "osg::Object osg::ComputeBoundingBoxCallback") { +} + +#undef OBJECT_CAST +#define OBJECT_CAST static_cast diff --git a/src/osgWrappers/serializers/osg/UpdateCallback.cpp b/src/osgWrappers/serializers/osg/UpdateCallback.cpp new file mode 100644 index 000000000..aaaa803ff --- /dev/null +++ b/src/osgWrappers/serializers/osg/UpdateCallback.cpp @@ -0,0 +1,16 @@ +#undef OBJECT_CAST +#define OBJECT_CAST dynamic_cast + +#include +#include +#include +#include + +REGISTER_OBJECT_WRAPPER2(osg_Drawable_UpdateCallback, + new osg::Drawable::UpdateCallback, + osg::Drawable::UpdateCallback, + "osg::UpdateCallback", + "osg::Object osg::UpdateCallback") {} + +#undef OBJECT_CAST +#define OBJECT_CAST static_cast diff --git a/src/osgWrappers/serializers/osgAnimation/RigComputeBoundingBoxCallback.cpp b/src/osgWrappers/serializers/osgAnimation/RigComputeBoundingBoxCallback.cpp new file mode 100644 index 000000000..8812c6ef0 --- /dev/null +++ b/src/osgWrappers/serializers/osgAnimation/RigComputeBoundingBoxCallback.cpp @@ -0,0 +1,16 @@ +#undef OBJECT_CAST +#define OBJECT_CAST dynamic_cast + +#include +#include +#include +#include + +REGISTER_OBJECT_WRAPPER(osgAnimation_RigComputeBoundingBoxCallback, + new osgAnimation::RigComputeBoundingBoxCallback, + osgAnimation::RigComputeBoundingBoxCallback, + "osg::Object osg::ComputeBoundingBoxCallback osgAnimation::RigComputeBoundingBoxCallback") { +} + +#undef OBJECT_CAST +#define OBJECT_CAST static_cast diff --git a/src/osgWrappers/serializers/osgAnimation/UpdateVertex.cpp b/src/osgWrappers/serializers/osgAnimation/UpdateVertex.cpp new file mode 100644 index 000000000..89f4b2e33 --- /dev/null +++ b/src/osgWrappers/serializers/osgAnimation/UpdateVertex.cpp @@ -0,0 +1,16 @@ +#undef OBJECT_CAST +#define OBJECT_CAST dynamic_cast + +#include +#include +#include +#include + +REGISTER_OBJECT_WRAPPER2(osg_Drawable_UpdateCallback, + new osgAnimation::RigGeometry::UpdateVertex, + osgAnimation::RigGeometry::UpdateVertex, + "osgAnimation::UpdateVertex", + "osg::Object osg::UpdateCallback osgAnimation::UpdateVertex") {} + +#undef OBJECT_CAST +#define OBJECT_CAST static_cast