RigGeometry
Go to the documentation of this file.
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
*
* 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
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGANIMATION_RIGGEOMETRY_H
#define OSGANIMATION_RIGGEOMETRY_H
#include <osgAnimation/Export>
#include <osgAnimation/Skeleton>
#include <osgAnimation/RigTransform>
#include <osgAnimation/VertexInfluence>
#include <osg/Geometry>
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:
RigGeometry();
RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Object(osgAnimation, RigGeometry);
inline void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
inline const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get(); }
inline VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get(); }
inline const Skeleton* getSkeleton() const { return _root.get(); }
inline Skeleton* getSkeleton() { return _root.get(); }
// will be used by the update callback to init correctly the rig mesh
inline void setSkeleton(Skeleton* root) { _root = root; }
void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state; }
bool getNeedToComputeMatrix() const { return _needToComputeMatrix; }
void computeMatrixFromRootSkeleton();
// set implementation of rig method
inline RigTransform* getRigTransformImplementation() { return _rigTransformImplementation.get(); }
inline void setRigTransformImplementation(RigTransform* rig) { _rigTransformImplementation = rig; }
inline const RigTransform* getRigTransformImplementation() const { return _rigTransformImplementation.get(); }
void update();
void buildVertexInfluenceSet() { _rigTransformImplementation->prepareData(*this); }
const osg::Matrix& getMatrixFromSkeletonToGeometry() const;
const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const;
inline osg::Geometry* getSourceGeometry() { return _geometry.get(); }
inline const osg::Geometry* getSourceGeometry() const { return _geometry.get(); }
inline void setSourceGeometry(osg::Geometry* geometry) { _geometry = geometry; }
void copyFrom(osg::Geometry& from);
struct FindNearestParentSkeleton : public osg::NodeVisitor
{
osg::ref_ptr<Skeleton> _root;
FindNearestParentSkeleton() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS) {}
void apply(osg::Transform& node)
{
if (_root.valid())
return;
_root = dynamic_cast<osgAnimation::Skeleton*>(&node);
traverse(node);
}
};
protected:
osg::ref_ptr<osg::Geometry> _geometry;
osg::ref_ptr<RigTransform> _rigTransformImplementation;
osg::ref_ptr<VertexInfluenceMap> _vertexInfluenceMap;
osg::Matrix _matrixFromSkeletonToGeometry;
osg::Matrix _invMatrixFromSkeletonToGeometry;
osg::observer_ptr<Skeleton> _root;
bool _needToComputeMatrix;
};
struct UpdateRigGeometry : public osg::Drawable::UpdateCallback
{
UpdateRigGeometry() {}
UpdateRigGeometry(const UpdateRigGeometry& org, const osg::CopyOp& copyop):
osg::Object(org, copyop),
osg::Callback(org, copyop),
osg::DrawableUpdateCallback(org, copyop) {}
META_Object(osgAnimation, UpdateRigGeometry);
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drw)
{
RigGeometry* geom = dynamic_cast<RigGeometry*>(drw);
if(!geom)
return;
if(!geom->getSkeleton() && !geom->getParents().empty())
{
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())
{
osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeometry ( " << geom->getName() << " )" << std::endl;
return;
}
geom->getRigTransformImplementation()->prepareData(*geom);
geom->setSkeleton(finder._root.get());
}
if(!geom->getSkeleton())
return;
if(geom->getNeedToComputeMatrix())
geom->computeMatrixFromRootSkeleton();
if(geom->getSourceGeometry())
{
osg::Drawable::UpdateCallback * up = dynamic_cast<osg::Drawable::UpdateCallback*>(geom->getSourceGeometry()->getUpdateCallback());
if(up)
up->update(nv, geom->getSourceGeometry());
}
geom->update();
}
};
}
#endif
Geometry
RigTransform
Skeleton
VertexInfluence
osgAnimation::RigComputeBoundingBoxCallback::reset
void reset()
Definition RigGeometry:39
osgAnimation::RigComputeBoundingBoxCallback::RigComputeBoundingBoxCallback
RigComputeBoundingBoxCallback(double factor=2.0)
Definition RigGeometry:30
osgAnimation::RigComputeBoundingBoxCallback::META_Object
META_Object(osgAnimation, RigComputeBoundingBoxCallback)
osgAnimation::RigComputeBoundingBoxCallback::computeBound
virtual osg::BoundingBox computeBound(const osg::Drawable &drawable) const
osgAnimation::RigComputeBoundingBoxCallback::RigComputeBoundingBoxCallback
RigComputeBoundingBoxCallback(const RigComputeBoundingBoxCallback &rhs, const osg::CopyOp &copyop)
Definition RigGeometry:32
osgAnimation::RigComputeBoundingBoxCallback::_factor
double _factor
Definition RigGeometry:44
osgAnimation::RigComputeBoundingBoxCallback::_boundingBox
osg::BoundingBox _boundingBox
Definition RigGeometry:45
osgAnimation::RigComputeBoundingBoxCallback::_computed
bool _computed
Definition RigGeometry:43
osgAnimation::RigGeometry
Definition RigGeometry:50
osgAnimation::RigGeometry::getSourceGeometry
osg::Geometry * getSourceGeometry()
Definition RigGeometry:86
osgAnimation::RigGeometry::_geometry
osg::ref_ptr< osg::Geometry > _geometry
Definition RigGeometry:107
osgAnimation::RigGeometry::_root
osg::observer_ptr< Skeleton > _root
Definition RigGeometry:113
osgAnimation::RigGeometry::META_Object
META_Object(osgAnimation, RigGeometry)
osgAnimation::RigGeometry::RigGeometry
RigGeometry()
osgAnimation::RigGeometry::_invMatrixFromSkeletonToGeometry
osg::Matrix _invMatrixFromSkeletonToGeometry
Definition RigGeometry:112
osgAnimation::RigGeometry::computeMatrixFromRootSkeleton
void computeMatrixFromRootSkeleton()
osgAnimation::RigGeometry::copyFrom
void copyFrom(osg::Geometry &from)
osgAnimation::RigGeometry::setNeedToComputeMatrix
void setNeedToComputeMatrix(bool state)
Definition RigGeometry:68
osgAnimation::RigGeometry::_matrixFromSkeletonToGeometry
osg::Matrix _matrixFromSkeletonToGeometry
Definition RigGeometry:111
osgAnimation::RigGeometry::buildVertexInfluenceSet
void buildVertexInfluenceSet()
Definition RigGeometry:80
osgAnimation::RigGeometry::getSkeleton
const Skeleton * getSkeleton() const
Definition RigGeometry:63
osgAnimation::RigGeometry::setInfluenceMap
void setInfluenceMap(VertexInfluenceMap *vertexInfluenceMap)
Definition RigGeometry:59
osgAnimation::RigGeometry::_rigTransformImplementation
osg::ref_ptr< RigTransform > _rigTransformImplementation
Definition RigGeometry:108
osgAnimation::RigGeometry::getInfluenceMap
VertexInfluenceMap * getInfluenceMap()
Definition RigGeometry:61
osgAnimation::RigGeometry::getSkeleton
Skeleton * getSkeleton()
Definition RigGeometry:64
osgAnimation::RigGeometry::update
void update()
osgAnimation::RigGeometry::getMatrixFromSkeletonToGeometry
const osg::Matrix & getMatrixFromSkeletonToGeometry() const
osgAnimation::RigGeometry::_vertexInfluenceMap
osg::ref_ptr< VertexInfluenceMap > _vertexInfluenceMap
Definition RigGeometry:109
osgAnimation::RigGeometry::_needToComputeMatrix
bool _needToComputeMatrix
Definition RigGeometry:114
osgAnimation::RigGeometry::getRigTransformImplementation
RigTransform * getRigTransformImplementation()
Definition RigGeometry:74
osgAnimation::RigGeometry::setSkeleton
void setSkeleton(Skeleton *root)
Definition RigGeometry:66
osgAnimation::RigGeometry::getSourceGeometry
const osg::Geometry * getSourceGeometry() const
Definition RigGeometry:87
osgAnimation::RigGeometry::getNeedToComputeMatrix
bool getNeedToComputeMatrix() const
Definition RigGeometry:69
osgAnimation::RigGeometry::getInvMatrixFromSkeletonToGeometry
const osg::Matrix & getInvMatrixFromSkeletonToGeometry() const
osgAnimation::RigGeometry::setSourceGeometry
void setSourceGeometry(osg::Geometry *geometry)
Definition RigGeometry:88
osgAnimation::RigGeometry::getRigTransformImplementation
const RigTransform * getRigTransformImplementation() const
Definition RigGeometry:76
osgAnimation::RigGeometry::getInfluenceMap
const VertexInfluenceMap * getInfluenceMap() const
Definition RigGeometry:60
osgAnimation::RigGeometry::RigGeometry
RigGeometry(const RigGeometry &b, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgAnimation::RigGeometry::setRigTransformImplementation
void setRigTransformImplementation(RigTransform *rig)
Definition RigGeometry:75
osgAnimation::RigTransform
Definition RigTransform:26
osgAnimation::RigTransform::prepareData
virtual bool prepareData(RigGeometry &)
Definition RigTransform:38
osgAnimation::Skeleton
Definition Skeleton:26
osgAnimation::VertexInfluenceMap
Definition VertexInfluence:52
osg::Callback::Callback
Callback()
Definition Callback:38
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::DrawableUpdateCallback::DrawableUpdateCallback
DrawableUpdateCallback()
Definition Callback:300
osg::DrawableUpdateCallback::update
virtual void update(osg::NodeVisitor *, osg::Drawable *)
Definition Callback:315
osg::Drawable
Definition Drawable:89
osg::Drawable::UpdateCallback
DrawableUpdateCallback UpdateCallback
Definition Drawable:344
osg::Geometry
Definition Geometry:31
osg::NodeVisitor
Definition NodeVisitor:82
osg::NodeVisitor::TRAVERSE_PARENTS
@ TRAVERSE_PARENTS
Definition NodeVisitor:88
osg::NodeVisitor::traverse
void traverse(Node &node)
Definition NodeVisitor:274
osg::NodeVisitor::NodeVisitor
NodeVisitor(TraversalMode tm=TRAVERSE_NONE)
osg::Node::getParents
const ParentList & getParents() const
Definition Node:173
osg::Object::Object
Object()
Definition Object:69
osg::Object::getName
const std::string & getName() const
Definition Object:213
osg::Transform
Definition Transform:75
osg::observer_ptr
Definition observer_ptr:39
osg::ref_ptr
Definition ref_ptr:32
osgAnimation
Definition Action:34
osg
Definition AlphaFunc:19
osg::WARN
@ WARN
Definition Notify:33
osg::BoundingBox
BoundingBoxd BoundingBox
Definition BoundingBox:257
osg::Matrix
Matrixd Matrix
Definition Matrix:27
osg::notify
std::ostream & notify(void)
Definition Notify:80
Export
OSGANIMATION_EXPORT
#define OSGANIMATION_EXPORT
Definition osgAnimation/Export:40
osgAnimation::RigGeometry::FindNearestParentSkeleton
Definition RigGeometry:93
osgAnimation::RigGeometry::FindNearestParentSkeleton::_root
osg::ref_ptr< Skeleton > _root
Definition RigGeometry:94
osgAnimation::RigGeometry::FindNearestParentSkeleton::FindNearestParentSkeleton
FindNearestParentSkeleton()
Definition RigGeometry:95
osgAnimation::RigGeometry::FindNearestParentSkeleton::apply
void apply(osg::Transform &node)
Definition RigGeometry:96
osgAnimation::UpdateRigGeometry::UpdateRigGeometry
UpdateRigGeometry()
Definition RigGeometry:121
osgAnimation::UpdateRigGeometry::META_Object
META_Object(osgAnimation, UpdateRigGeometry)
osgAnimation::UpdateRigGeometry::update
virtual void update(osg::NodeVisitor *nv, osg::Drawable *drw)
Definition RigGeometry:130
osgAnimation::UpdateRigGeometry::UpdateRigGeometry
UpdateRigGeometry(const UpdateRigGeometry &org, const osg::CopyOp &copyop)
Definition RigGeometry:123
osg::Drawable::ComputeBoundingBoxCallback
Definition Drawable:162
osg::Drawable::ComputeBoundingBoxCallback::ComputeBoundingBoxCallback
ComputeBoundingBoxCallback()
Definition Drawable:163