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>
|
2009-10-27 23:37:13 +08:00
|
|
|
#include <osgAnimation/RigTransform>
|
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;
|
|
|
|
};
|
|
|
|
|
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();
|
2010-01-27 20:24:55 +08:00
|
|
|
// RigGeometry(const osg::Geometry& b);
|
2009-02-10 06:56:21 +08:00
|
|
|
RigGeometry(const RigGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osgAnimation, RigGeometry);
|
|
|
|
|
2009-10-27 23:37:13 +08:00
|
|
|
void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
|
|
|
|
const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();}
|
|
|
|
VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
const Skeleton* getSkeleton() const;
|
|
|
|
Skeleton* getSkeleton();
|
2009-10-27 23:37:13 +08:00
|
|
|
// will be used by the update callback to init correctly the rig mesh
|
|
|
|
void setSkeleton(Skeleton*);
|
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-10-27 23:37:13 +08:00
|
|
|
|
|
|
|
// this build the internal database about vertex influence and bones
|
|
|
|
void buildVertexInfluenceSet();
|
|
|
|
const VertexInfluenceSet& getVertexInfluenceSet() const;
|
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
void computeMatrixFromRootSkeleton();
|
|
|
|
|
|
|
|
|
2009-10-27 23:37:13 +08:00
|
|
|
// set implementation of rig method
|
|
|
|
void setRigTransformImplementation(RigTransform*);
|
|
|
|
RigTransform* getRigTransformImplementation();
|
|
|
|
|
|
|
|
virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
|
|
|
|
void update();
|
|
|
|
|
|
|
|
const osg::Matrix& getMatrixFromSkeletonToGeometry() const;
|
|
|
|
const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const;
|
2009-02-10 06:56:21 +08:00
|
|
|
|
2010-01-27 20:24:55 +08:00
|
|
|
osg::Geometry* getSourceGeometry();
|
|
|
|
const osg::Geometry* getSourceGeometry() const;
|
|
|
|
void setSourceGeometry(osg::Geometry* geometry);
|
|
|
|
|
|
|
|
void copyFrom(osg::Geometry& from);
|
2009-02-10 06:56:21 +08:00
|
|
|
|
2014-04-29 23:14:39 +08:00
|
|
|
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<RigGeometry*>(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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
VertexInfluenceSet _vertexInfluenceSet;
|
|
|
|
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
|
|
|
|
2008-11-22 20:14:19 +08:00
|
|
|
struct FindNearestParentSkeleton : public osg::NodeVisitor
|
|
|
|
{
|
2009-10-27 23:37:13 +08:00
|
|
|
osg::ref_ptr<Skeleton> _root;
|
2008-11-22 20:14:19 +08:00
|
|
|
FindNearestParentSkeleton() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
void apply(osg::Transform& node)
|
2008-11-22 20:14:19 +08:00
|
|
|
{
|
|
|
|
if (_root.valid())
|
|
|
|
return;
|
|
|
|
_root = dynamic_cast<osgAnimation::Skeleton*>(&node);
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-02-10 06:56:21 +08:00
|
|
|
};
|
2008-11-22 20:14:19 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|