2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*-
|
2009-10-27 23:37:13 +08:00
|
|
|
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
|
2008-11-22 20:14:19 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2008-11-22 20:14:19 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2008-11-22 20:14:19 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-11-22 20:14:19 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGANIMATION_RIGGEOMETRY_H
|
|
|
|
#define OSGANIMATION_RIGGEOMETRY_H
|
|
|
|
|
|
|
|
#include <osgAnimation/Export>
|
|
|
|
#include <osgAnimation/Skeleton>
|
2017-08-28 21:59:13 +08:00
|
|
|
#include <osgAnimation/RigTransformSoftware>
|
2010-01-27 20:24:55 +08:00
|
|
|
#include <osgAnimation/VertexInfluence>
|
2008-11-22 20:14:19 +08:00
|
|
|
#include <osg/Geometry>
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
namespace osgAnimation
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
|
2014-04-29 23:14:39 +08:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
class OSGANIMATION_EXPORT RigGeometry : public osg::Geometry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
RigGeometry();
|
2016-06-25 14:49:56 +08:00
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osgAnimation, RigGeometry);
|
|
|
|
|
2017-08-27 02:37:10 +08:00
|
|
|
inline void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
|
|
|
|
inline const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();}
|
|
|
|
inline VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2017-08-27 02:37:10 +08:00
|
|
|
inline const Skeleton* getSkeleton() const { return _root.get(); }
|
|
|
|
inline Skeleton* getSkeleton() { return _root.get(); }
|
2009-10-27 23:37:13 +08:00
|
|
|
// will be used by the update callback to init correctly the rig mesh
|
2017-08-27 02:37:10 +08:00
|
|
|
inline void setSkeleton(Skeleton* root){ _root = root;}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state;}
|
|
|
|
bool getNeedToComputeMatrix() const { return _needToComputeMatrix;}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
void computeMatrixFromRootSkeleton();
|
|
|
|
|
2009-10-27 23:37:13 +08:00
|
|
|
// set implementation of rig method
|
2017-08-27 02:37:10 +08:00
|
|
|
inline RigTransform* getRigTransformImplementation() { return _rigTransformImplementation.get(); }
|
|
|
|
inline void setRigTransformImplementation(RigTransform* rig) { _rigTransformImplementation = rig; }
|
|
|
|
inline const RigTransform* getRigTransformImplementation() const { return _rigTransformImplementation.get(); }
|
2009-10-27 23:37:13 +08:00
|
|
|
|
|
|
|
void update();
|
|
|
|
|
|
|
|
const osg::Matrix& getMatrixFromSkeletonToGeometry() const;
|
|
|
|
const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const;
|
2009-02-10 06:56:21 +08:00
|
|
|
|
2017-08-27 02:37:10 +08:00
|
|
|
inline osg::Geometry* getSourceGeometry() { return _geometry.get(); }
|
|
|
|
inline const osg::Geometry* getSourceGeometry() const { return _geometry.get(); }
|
|
|
|
inline void setSourceGeometry(osg::Geometry* geometry) { _geometry = geometry; }
|
2010-01-27 20:24:55 +08:00
|
|
|
|
|
|
|
void copyFrom(osg::Geometry& from);
|
2009-02-10 06:56:21 +08:00
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
struct FindNearestParentSkeleton : public osg::NodeVisitor
|
2014-04-29 23:14:39 +08:00
|
|
|
{
|
2015-03-13 01:11:11 +08:00
|
|
|
osg::ref_ptr<Skeleton> _root;
|
|
|
|
FindNearestParentSkeleton() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS) {}
|
|
|
|
void apply(osg::Transform& node)
|
|
|
|
{
|
|
|
|
if (_root.valid())
|
2014-04-29 23:14:39 +08:00
|
|
|
return;
|
2015-03-13 01:11:11 +08:00
|
|
|
_root = dynamic_cast<osgAnimation::Skeleton*>(&node);
|
|
|
|
traverse(node);
|
2014-04-29 23:14:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-01-27 20:24:55 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-27 20:24:55 +08:00
|
|
|
osg::ref_ptr<osg::Geometry> _geometry;
|
2009-10-27 23:37:13 +08:00
|
|
|
osg::ref_ptr<RigTransform> _rigTransformImplementation;
|
2009-02-10 06:56:21 +08:00
|
|
|
|
2009-10-27 23:37:13 +08:00
|
|
|
osg::ref_ptr<VertexInfluenceMap> _vertexInfluenceMap;
|
2009-02-10 06:56:21 +08:00
|
|
|
|
|
|
|
osg::Matrix _matrixFromSkeletonToGeometry;
|
|
|
|
osg::Matrix _invMatrixFromSkeletonToGeometry;
|
|
|
|
osg::observer_ptr<Skeleton> _root;
|
|
|
|
bool _needToComputeMatrix;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-25 14:49:56 +08:00
|
|
|
struct UpdateRigGeometry : public osg::Drawable::UpdateCallback
|
2015-03-13 01:11:11 +08:00
|
|
|
{
|
|
|
|
UpdateRigGeometry() {}
|
|
|
|
|
2016-08-29 16:41:40 +08:00
|
|
|
UpdateRigGeometry(const UpdateRigGeometry& org, const osg::CopyOp& copyop):
|
|
|
|
osg::Object(org, copyop),
|
|
|
|
osg::Callback(org, copyop),
|
|
|
|
osg::DrawableUpdateCallback(org, copyop) {}
|
2015-03-13 01:11:11 +08:00
|
|
|
|
|
|
|
META_Object(osgAnimation, UpdateRigGeometry);
|
|
|
|
|
2016-06-25 14:49:56 +08:00
|
|
|
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drw) {
|
2015-03-13 01:11:11 +08:00
|
|
|
RigGeometry* geom = dynamic_cast<RigGeometry*>(drw);
|
|
|
|
if(!geom)
|
|
|
|
return;
|
|
|
|
if(!geom->getSkeleton() && !geom->getParents().empty())
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
2015-03-13 01:11:11 +08:00
|
|
|
RigGeometry::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())
|
|
|
|
{
|
2015-04-13 18:43:56 +08:00
|
|
|
osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeometry ( " << geom->getName() << " )" << std::endl;
|
2008-11-22 20:14:19 +08:00
|
|
|
return;
|
2015-03-13 01:11:11 +08:00
|
|
|
}
|
|
|
|
geom->setSkeleton(finder._root.get());
|
2017-08-28 21:59:13 +08:00
|
|
|
|
|
|
|
if(!geom->getRigTransformImplementation())
|
|
|
|
{
|
|
|
|
geom->setRigTransformImplementation(new RigTransformSoftware);
|
|
|
|
}
|
|
|
|
geom->getRigTransformImplementation()->prepareData(*geom);
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
if(!geom->getSkeleton())
|
|
|
|
return;
|
2008-11-22 20:14:19 +08:00
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
if(geom->getNeedToComputeMatrix())
|
|
|
|
geom->computeMatrixFromRootSkeleton();
|
|
|
|
|
2016-06-25 14:49:56 +08:00
|
|
|
if(geom->getSourceGeometry()) {
|
|
|
|
osg::Drawable::UpdateCallback * up = dynamic_cast<osg::Drawable::UpdateCallback*>(geom->getSourceGeometry()->getUpdateCallback());
|
|
|
|
if(up)
|
|
|
|
up->update(nv, geom->getSourceGeometry());
|
|
|
|
}
|
|
|
|
|
2015-03-13 01:11:11 +08:00
|
|
|
geom->update();
|
|
|
|
}
|
|
|
|
};
|
2008-11-22 20:14:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|