refactoring and fixes

only change in design: decouplage between MorphGeometry and MorphTransform technique
no real change in behavior (i hope)
This commit is contained in:
Julien Valentin
2017-08-26 20:37:10 +02:00
parent c89b08ea1f
commit 32aaeccee1
16 changed files with 643 additions and 422 deletions

View File

@@ -42,6 +42,16 @@ namespace osgAnimation
const AnimationList& getAnimationList() const { return _animations;}
AnimationList& getAnimationList() { return _animations;}
//uniformisation of the API
inline Animation * getRegisteredAnimation(unsigned int i){return _animations[i].get();}
inline unsigned int getNumRegisteredAnimations()const{return _animations.size();}
inline void addRegisteredAnimation(Animation* animation){
_needToLink = true;
_animations.push_back(animation);
buildTargetReference();
}
void removeRegisteredAnimation(Animation* animation);
/** Callback method called by the NodeVisitor when visiting a node.*/
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);

View File

@@ -17,6 +17,7 @@
#include <osgAnimation/Export>
#include <osgAnimation/AnimationUpdateCallback>
#include <osgAnimation/MorphTransformSoftware>
#include <osg/Geometry>
#include <algorithm>
@@ -59,7 +60,10 @@ namespace osgAnimation
virtual const char* libraryName() const { return "osgAnimation"; }
virtual const char* className() const { return "MorphGeometry"; }
virtual void transformSoftwareMethod();
// set implementation of rig method
void setMorphTransformImplementation(MorphTransform*);
MorphTransform* getMorphTransformImplementation();
const MorphTransform* getMorphTransformImplementation() const { return _rigTransformImplementation.get(); }
/** Set the morphing method. */
void setMethod(Method method) { _method = method; }
@@ -71,6 +75,30 @@ namespace osgAnimation
/** Get the flag for morphing normals. */
inline bool getMorphNormals() const { return _morphNormals; }
/** Get the list of MorphTargets.*/
const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
/** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */
MorphTargetList& getMorphTargetList() { return _morphTargets; }
/** Return the \c MorphTarget at position \c i.*/
inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
/** Return the \c MorphTarget at position \c i.*/
inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
/** Set source of vertices for this morph geometry */
inline void setVertexSource(osg::Vec3Array *v){ _positionSource=v;}
/** Get source of vertices for this morph geometry */
inline osg::Vec3Array * getVertexSource()const{return _positionSource;}
/** Set source of normals for this morph geometry */
inline void setNormalSource(osg::Vec3Array *n){ _normalSource=n;}
/** Get source of normals for this morph geometry */
inline osg::Vec3Array * getNormalSource()const{return _normalSource;}
/** Add a \c MorphTarget to the \c MorphGeometry.
* If \c MorphTarget is not \c NULL and is not contained in the \c MorphGeometry
* then increment its reference count, add it to the MorphTargets list and
@@ -101,6 +129,7 @@ namespace osgAnimation
}
/** update a morph target at index setting its current weight to morphWeight */
void setWeight(unsigned int index, float morphWeight)
{
if (index < _morphTargets.size())
@@ -111,29 +140,20 @@ namespace osgAnimation
}
/** Set the MorphGeometry dirty.*/
void dirty() { _dirty = true; }
inline void dirty(bool b=true) { _dirty = b; }
inline bool isDirty()const { return _dirty; }
/** Get the list of MorphTargets.*/
const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
/** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */
MorphTargetList& getMorphTargetList() { return _morphTargets; }
/** Return the \c MorphTarget at position \c i.*/
inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
/** Return the \c MorphTarget at position \c i.*/
inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
protected:
osg::ref_ptr<MorphTransform> _rigTransformImplementation;
/// Do we need to recalculate the morphed geometry?
bool _dirty;
Method _method;
MorphTargetList _morphTargets;
std::vector<osg::Vec3> _positionSource;
std::vector<osg::Vec3> _normalSource;
osg::ref_ptr<osg::Vec3Array> _positionSource;
osg::ref_ptr<osg::Vec3Array> _normalSource;
/// Do we also morph between normals?
bool _morphNormals;
@@ -195,7 +215,13 @@ namespace osgAnimation
if (!geom)
return;
geom->transformSoftwareMethod();
if (!geom->getMorphTransformImplementation())
{
geom->setMorphTransformImplementation( new MorphTransformSoftware);
}
MorphTransform& implementation = *geom->getMorphTransformImplementation();
(implementation)(*geom);
}
};

View File

@@ -0,0 +1,45 @@
/* -*-c++-*-
* Copyright (C) 2009 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_MORPHTRANSFORM_SOFTWARE
#define OSGANIMATION_MORPHTRANSFORM_SOFTWARE 1
#include <osgAnimation/Export>
#include <osgAnimation/RigTransform>
#include <osgAnimation/Bone>
#include <osg/observer_ptr>
namespace osgAnimation
{
class MorphGeometry;
/// This class manage format for software morphing
class OSGANIMATION_EXPORT MorphTransformSoftware : public MorphTransform
{
public:
MorphTransformSoftware():_needInit(true){}
MorphTransformSoftware(const MorphTransformSoftware& rts,const osg::CopyOp& copyop): MorphTransform(rts, copyop), _needInit(true){}
META_Object(osgAnimation,MorphTransformSoftware)
bool init(MorphGeometry&);
virtual void operator()(MorphGeometry&);
protected:
bool _needInit;
};
}
#endif

View File

@@ -57,14 +57,14 @@ namespace osgAnimation
META_Object(osgAnimation, RigGeometry);
void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();}
VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();}
inline void setInfluenceMap(VertexInfluenceMap* vertexInfluenceMap) { _vertexInfluenceMap = vertexInfluenceMap; }
inline const VertexInfluenceMap* getInfluenceMap() const { return _vertexInfluenceMap.get();}
inline VertexInfluenceMap* getInfluenceMap() { return _vertexInfluenceMap.get();}
const Skeleton* getSkeleton() const;
Skeleton* getSkeleton();
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
void setSkeleton(Skeleton*);
inline void setSkeleton(Skeleton* root){ _root = root;}
void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state;}
bool getNeedToComputeMatrix() const { return _needToComputeMatrix;}
@@ -72,25 +72,25 @@ namespace osgAnimation
// this build the internal database about vertex influence and bones
void buildVertexInfluenceSet();
const VertexInfluenceSet& getVertexInfluenceSet() const;
inline const VertexInfluenceSet& getVertexInfluenceSet() const { return _vertexInfluenceSet;}
void computeMatrixFromRootSkeleton();
// set implementation of rig method
void setRigTransformImplementation(RigTransform*);
RigTransform* getRigTransformImplementation();
const RigTransform* getRigTransformImplementation() const { return _rigTransformImplementation.get(); }
inline RigTransform* getRigTransformImplementation() { return _rigTransformImplementation.get(); }
inline void setRigTransformImplementation(RigTransform* rig) { _rigTransformImplementation = rig; }
inline const RigTransform* getRigTransformImplementation() const { return _rigTransformImplementation.get(); }
virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
void update();
const osg::Matrix& getMatrixFromSkeletonToGeometry() const;
const osg::Matrix& getInvMatrixFromSkeletonToGeometry() const;
osg::Geometry* getSourceGeometry();
const osg::Geometry* getSourceGeometry() const;
void setSourceGeometry(osg::Geometry* geometry);
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);

View File

@@ -37,6 +37,23 @@ namespace osgAnimation
virtual ~RigTransform() {}
};
class MorphGeometry;
class MorphTransform : public osg::Object
{
public:
MorphTransform() {}
MorphTransform(const MorphTransform& org, const osg::CopyOp& copyop):
osg::Object(org, copyop) {}
META_Object(osgAnimation,MorphTransform)
virtual void operator()(MorphGeometry&) {}
protected:
virtual ~MorphTransform() {}
};
}

View File

@@ -38,37 +38,36 @@ namespace osgAnimation
META_Object(osgAnimation,RigTransformHardware);
typedef osg::Matrix MatrixType;
typedef osgAnimation::Bone BoneType;
typedef std::vector<osg::ref_ptr<osg::Vec4Array> > BoneWeightAttribList;
typedef std::vector<osg::ref_ptr<BoneType> > BonePalette;
typedef std::map<std::string, int> BoneNamePaletteIndex;
typedef std::vector<osg::ref_ptr<Bone> > BonePalette;
typedef std::map<std::string, unsigned int> BoneNamePaletteIndex;
typedef std::vector<osg::Matrix> MatrixPalette;
struct IndexWeightEntry
{
int _boneIndex;
IndexWeightEntry(unsigned int index=0, float weight=0.0f): _boneIndex(index), _boneWeight(weight){}
IndexWeightEntry(const IndexWeightEntry&o): _boneIndex(o._boneIndex), _boneWeight(o._boneWeight){}
bool operator <(const IndexWeightEntry &o)const{return (_boneIndex<o._boneIndex);}
const unsigned int &getBoneIndex() const { return _boneIndex; }
const float &getWeight() const { return _boneWeight; }
unsigned int _boneIndex;
float _boneWeight;
IndexWeightEntry() { _boneIndex = 0; _boneWeight = 0;}
IndexWeightEntry(int index, float weight) { _boneIndex = index; _boneWeight = weight;}
int getIndex() const { return _boneIndex; }
float getWeight() const { return _boneWeight; }
};
typedef std::vector<std::vector<IndexWeightEntry> > VertexIndexWeightList;
osg::Vec4Array* getVertexAttrib(int index);
int getNumVertexAttrib();
osg::Vec4Array* getVertexAttrib(unsigned int index);
unsigned int getNumVertexAttrib();
osg::Uniform* getMatrixPaletteUniform();
void computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry);
int getNumBonesPerVertex() const;
int getNumVertexes() const;
unsigned int getNumBonesPerVertex() const;
unsigned int getNumVertexes() const;
bool createPalette(int nbVertexes, BoneMap boneMap, const VertexInfluenceSet::VertexIndexToBoneWeightMap& vertexIndexToBoneWeightMap);
bool createPalette(unsigned int nbVertexes,const BoneMap& boneMap, const VertexInfluenceSet::VertIDToBoneWeightList& vertexIndexToBoneWeightMap);
virtual void operator()(RigGeometry&);
void setShader(osg::Shader*);
const BoneNamePaletteIndex& getBoneNameToPalette() {
@@ -78,13 +77,11 @@ namespace osgAnimation
protected:
bool init(RigGeometry&);
osg::Uniform* createVertexUniform();
BoneWeightAttribList createVertexAttribList();
osg::Uniform* createVertexUniform();
unsigned int _bonesPerVertex;
unsigned int _nbVertexes;
int _bonesPerVertex;
int _nbVertexes;
VertexIndexWeightList _vertexIndexMatrixWeightList;
BonePalette _bonePalette;
BoneNamePaletteIndex _boneNameToPalette;
BoneWeightAttribList _boneWeightAttribArrays;

View File

@@ -37,10 +37,10 @@ namespace osgAnimation
virtual void operator()(RigGeometry&);
class BoneWeight
class BonePtrWeight
{
public:
BoneWeight(Bone* bone, float weight) : _bone(bone), _weight(weight) {}
BonePtrWeight(Bone* bone, float weight) : _bone(bone), _weight(weight) {}
const Bone* getBone() const { return _bone.get(); }
float getWeight() const { return _weight; }
void setWeight(float w) { _weight = w; }
@@ -49,13 +49,13 @@ namespace osgAnimation
float _weight;
};
typedef std::vector<BoneWeight> BoneWeightList;
typedef std::vector<int> VertexList;
typedef std::vector<BonePtrWeight> BonePtrWeightList;
typedef std::vector<unsigned int> VertexList;
class UniqBoneSetVertexSet
class VertexGroup
{
public:
BoneWeightList& getBones() { return _bones; }
BonePtrWeightList& getBoneWeights() { return _boneweights; }
VertexList& getVertexes() { return _vertexes; }
void resetMatrix()
@@ -88,18 +88,17 @@ namespace osgAnimation
}
void computeMatrixForVertexSet()
{
if (_bones.empty())
if (_boneweights.empty())
{
osg::notify(osg::WARN) << this << " RigTransformSoftware::UniqBoneSetVertexSet no bones found" << std::endl;
osg::notify(osg::WARN) << this << " RigTransformSoftware::VertexGroup no bones found" << std::endl;
_result = osg::Matrix::identity();
return;
}
resetMatrix();
int size = _bones.size();
for (int i = 0; i < size; i++)
for(BonePtrWeightList::iterator bwit=_boneweights.begin();bwit!=_boneweights.end();++bwit )
{
const Bone* bone = _bones[i].getBone();
const Bone* bone = bwit->getBone();
if (!bone)
{
osg::notify(osg::WARN) << this << " RigTransformSoftware::computeMatrixForVertexSet Warning a bone is null, skip it" << std::endl;
@@ -107,13 +106,13 @@ namespace osgAnimation
}
const osg::Matrix& invBindMatrix = bone->getInvBindMatrixInSkeletonSpace();
const osg::Matrix& matrix = bone->getMatrixInSkeletonSpace();
osg::Matrix::value_type w = _bones[i].getWeight();
osg::Matrix::value_type w = bwit->getWeight();
accummulateMatrix(invBindMatrix, matrix, w);
}
}
const osg::Matrix& getMatrix() const { return _result;}
protected:
BoneWeightList _bones;
BonePtrWeightList _boneweights;
VertexList _vertexes;
osg::Matrix _result;
};
@@ -123,39 +122,34 @@ namespace osgAnimation
template <class V> void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
{
// the result of matrix mult should be cached to be used for vertexes transform and normal transform and maybe other computation
int size = _boneSetVertexSet.size();
for (int i = 0; i < size; i++)
for(std::vector<VertexGroup>::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg)
{
UniqBoneSetVertexSet& uniq = _boneSetVertexSet[i];
VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet();
osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
const VertexList& vertexes = uniq.getVertexes();
int vertexSize = vertexes.size();
for (int j = 0; j < vertexSize; j++)
for(VertexList::const_iterator vertIDit=vertexes.begin(); vertIDit!=vertexes.end(); ++vertIDit)
{
int idx = vertexes[j];
dst[idx] = src[idx] * matrix;
dst[*vertIDit] = src[*vertIDit] * matrix;
}
}
}
template <class V> void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
{
int size = _boneSetVertexSet.size();
for (int i = 0; i < size; i++)
for(std::vector<VertexGroup>::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg)
{
UniqBoneSetVertexSet& uniq = _boneSetVertexSet[i];
VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet();
osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
const VertexList& vertexes = uniq.getVertexes();
int vertexSize = vertexes.size();
for (int j = 0; j < vertexSize; j++)
for(VertexList::const_iterator vertIDit=vertexes.begin(); vertIDit!=vertexes.end(); ++vertIDit)
{
int idx = vertexes[j];
dst[idx] = osg::Matrix::transform3x3(src[idx],matrix);
dst[*vertIDit] = osg::Matrix::transform3x3(src[*vertIDit],matrix);
}
}
}
@@ -163,8 +157,8 @@ namespace osgAnimation
protected:
bool init(RigGeometry&);
void initVertexSetFromBones(const BoneMap& map, const VertexInfluenceSet::UniqVertexSetToBoneSetList& influence);
std::vector<UniqBoneSetVertexSet> _boneSetVertexSet;
void initVertexSetFromBones(const BoneMap& map, const VertexInfluenceSet::UniqVertexGroupList& influence);
std::vector<VertexGroup> _uniqInfluenceSet2VertIDList;
bool _needInit;

View File

@@ -25,7 +25,7 @@ namespace osgAnimation
{
// first is vertex index, and second the weight, the
typedef std::pair<int, float> VertexIndexWeight;
typedef std::pair<unsigned int, float> VertexIndexWeight;
typedef std::vector<VertexIndexWeight> VertexList;
class OSGANIMATION_EXPORT VertexInfluence : public VertexList
{
@@ -62,43 +62,48 @@ namespace osgAnimation
{
public:
BoneWeight(const std::string& name, float weight) : _boneName(name), _weight(weight) {}
BoneWeight(const BoneWeight &bw2) : _boneName(bw2._boneName), _weight(bw2._weight) {}
const std::string& getBoneName() const { return _boneName; }
float getWeight() const { return _weight; }
void setWeight(float weight) { _weight = weight; }
bool operator==(const BoneWeight& b) const { return (_boneName == b.getBoneName() && _weight == b.getWeight()); }
protected:
std::string _boneName;
float _weight;
};
typedef std::vector<BoneWeight> BoneWeightList;
typedef std::map<int,BoneWeightList> VertexIndexToBoneWeightMap;
typedef std::vector<BoneWeightList> VertIDToBoneWeightList;
class UniqVertexSetToBoneSet
class VertexGroup
{
public:
// set Influences of the VertexGroup
void setBones(BoneWeightList& bones) { _bones = bones;}
const BoneWeightList& getBones() const { return _bones;}
std::vector<int>& getVertexes() { return _vertexes;}
const std::vector<int>& getVertexes() const { return _vertexes;}
// set Vertex Indices of the VertexGroup
std::vector<unsigned int>& getVertexes() { return _vertexes;}
const std::vector<unsigned int>& getVertexes() const { return _vertexes;}
protected:
std::vector<int> _vertexes;
std::vector<unsigned int> _vertexes;
BoneWeightList _bones; // here we could limit matrix operation by caching (weight * matrix)
};
typedef std::vector<UniqVertexSetToBoneSet> UniqVertexSetToBoneSetList;
const UniqVertexSetToBoneSetList& getUniqVertexSetToBoneSetList() const { return _uniqVertexSetToBoneSet;}
typedef std::vector<VertexGroup> UniqVertexGroupList;
/** construct a vector of unique VertexGroups and their influences**/
void buildUniqVertexGroupList();
/** return a list of unique VertexGroups and their influences**/
const UniqVertexGroupList& getUniqVertexGroupList() const { return _uniqVertexSetToBoneSet;}
void addVertexInfluence(const VertexInfluence& v);
void buildVertex2BoneList();
void buildUniqVertexSetToBoneSetList();
void buildVertex2BoneList(unsigned int numvertices);
void clear();
const VertexIndexToBoneWeightMap& getVertexToBoneList() const;
const VertIDToBoneWeightList& getVertexToBoneList() const;
protected:
BoneToVertexList _bone2Vertexes;
VertexIndexToBoneWeightMap _vertex2Bones;
UniqVertexSetToBoneSetList _uniqVertexSetToBoneSet;
VertIDToBoneWeightList _vertex2Bones;
UniqVertexGroupList _uniqVertexSetToBoneSet;
};
}