Merge pull request #382 from openscenegraph/osganimation

Osganimation improvements
This commit is contained in:
OpenSceneGraph git repository
2017-11-11 14:43:10 +00:00
committed by GitHub
25 changed files with 1860 additions and 851 deletions

View File

@@ -42,6 +42,17 @@ 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);
@@ -49,7 +60,6 @@ namespace osgAnimation
this Operation must be done each frame */
void clearTargets();
LinkVisitor* getOrCreateLinkVisitor();
void setLinkVisitor(LinkVisitor*);

View File

@@ -17,6 +17,7 @@
#include <osgAnimation/Export>
#include <osgAnimation/AnimationUpdateCallback>
#include <osgAnimation/MorphTransformSoftware>
#include <osg/Geometry>
#include <algorithm>
@@ -28,7 +29,8 @@ namespace osgAnimation
public:
enum Method {
enum Method
{
NORMALIZED,
RELATIVE
};
@@ -59,18 +61,45 @@ namespace osgAnimation
virtual const char* libraryName() const { return "osgAnimation"; }
virtual const char* className() const { return "MorphGeometry"; }
virtual void transformSoftwareMethod();
// set implementation of rig method
inline void setMorphTransformImplementation(MorphTransform*mt) { _morphTransformImplementation=mt; }
inline MorphTransform* getMorphTransformImplementation() { return _morphTransformImplementation.get(); }
inline const MorphTransform* getMorphTransformImplementation() const { return _morphTransformImplementation.get(); }
/** Set the morphing method. */
void setMethod(Method method) { _method = method; }
inline void setMethod(Method method) { _method = method; }
/** Get the morphing method. */
inline Method getMethod() const { return _method; }
/** Set flag for morphing normals. */
void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
inline void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
/** Get the flag for morphing normals. */
inline bool getMorphNormals() const { return _morphNormals; }
/** Get the list of MorphTargets.*/
inline const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
/** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */
inline 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.get(); }
/** 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.get(); }
/** 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
@@ -80,20 +109,30 @@ namespace osgAnimation
* @param weight The weight to be added to the \c MorphGeometry.
* @return \c true for success; \c false otherwise.
*/
virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 ) { _morphTargets.push_back(MorphTarget(morphTarget, weight)); _dirty = true; }
virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 )
{
_morphTargets.push_back(MorphTarget(morphTarget, weight));
_dirty = true;
}
virtual void removeMorphTarget( osg::Geometry *morphTarget ) {
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator) {
if(iterator->getGeometry() == morphTarget) {
virtual void removeMorphTarget( osg::Geometry *morphTarget )
{
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
{
if(iterator->getGeometry() == morphTarget)
{
_morphTargets.erase(iterator);
break;
}
}
}
virtual void removeMorphTarget( const std::string& name ) {
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator) {
if(iterator->getGeometry() && iterator->getGeometry()->getName() == name) {
virtual void removeMorphTarget( const std::string& name )
{
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
{
if(iterator->getGeometry() && iterator->getGeometry()->getName() == name)
{
_morphTargets.erase(iterator);
break;
}
@@ -101,7 +140,8 @@ namespace osgAnimation
}
void setWeight(unsigned int index, float morphWeight)
/** update a morph target at index setting its current weight to morphWeight */
inline void setWeight(unsigned int index, float morphWeight)
{
if (index < _morphTargets.size())
{
@@ -111,29 +151,22 @@ 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]; }
/** for retrocompatibility */
void transformSoftwareMethod() { (*_morphTransformImplementation.get())(*this); }
protected:
osg::ref_ptr<MorphTransform> _morphTransformImplementation;
/// 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;
@@ -153,7 +186,8 @@ namespace osgAnimation
void addTarget(const std::string& name) { _targetNames.push_back(name); }
unsigned int getNumTarget() const { return _targetNames.size(); }
const std::string& getTargetName(unsigned int index) { return _targetNames[index]; }
void removeTarget(const std::string& name) {
void removeTarget(const std::string& name)
{
TargetNames::iterator found = std::find(_targetNames.begin(), _targetNames.end(), name);
if(found != _targetNames.end())
_targetNames.erase(found);
@@ -163,9 +197,7 @@ namespace osgAnimation
const std::vector<std::string>& getTargetNames() const { return _targetNames; }
std::vector<std::string>& getTargetNames() { return _targetNames; }
void setTargetNames(const TargetNames& targetNames) {
_targetNames.assign(targetNames.begin(), targetNames.end());
}
void setTargetNames(const TargetNames& targetNames) { _targetNames.assign(targetNames.begin(), targetNames.end()); }
/** Callback method called by the NodeVisitor when visiting a node.*/
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
@@ -195,11 +227,15 @@ namespace osgAnimation
if (!geom)
return;
geom->transformSoftwareMethod();
if (!geom->getMorphTransformImplementation())
{
geom->setMorphTransformImplementation( new MorphTransformSoftware);
}
MorphTransform& implementation = *geom->getMorphTransformImplementation();
(implementation)(*geom);
}
};
}
#endif

View File

@@ -0,0 +1,65 @@
/* -*-c++-*-
* Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
*
* 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_MORPH_TRANSFORM_HARDWARE
#define OSGANIMATION_MORPH_TRANSFORM_HARDWARE 1
#include <osgAnimation/Export>
#include <osgAnimation/RigTransform>
#include <osgAnimation/VertexInfluence>
#include <osgAnimation/Bone>
#include <osg/Matrix>
#include <osg/Array>
///texture unit reserved for morphtarget TBO
#define MORPHTRANSHW_DEFAULTMORPHTEXTUREUNIT 7
namespace osgAnimation
{
class MorphGeometry;
/// This class manage format for hardware morphing
class OSGANIMATION_EXPORT MorphTransformHardware : public MorphTransform
{
public:
MorphTransformHardware();
MorphTransformHardware(const MorphTransformHardware& rth, const osg::CopyOp& copyop);
META_Object(osgAnimation,MorphTransformHardware);
virtual void operator()(MorphGeometry&);
inline void setShader( osg::Shader*s ) { _shader=s; }
inline const osg::Shader * getShader() const { return _shader.get(); }
inline osg::Shader * getShader() { return _shader.get(); }
///texture unit reserved for morphtarget TBO default is 7
void setReservedTextureUnit(unsigned int t) { _reservedTextureUnit=t; }
unsigned int getReservedTextureUnit() const { return _reservedTextureUnit; }
protected:
bool init(MorphGeometry&);
osg::ref_ptr<osg::Uniform> _uniformTargetsWeight;
osg::ref_ptr<osg::Shader> _shader;
bool _needInit;
unsigned int _reservedTextureUnit;
};
}
#endif

View File

@@ -0,0 +1,46 @@
/* -*-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

@@ -23,7 +23,6 @@
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
{
@@ -37,8 +36,8 @@ namespace osgAnimation
META_Object(osgAnimation, RigComputeBoundingBoxCallback);
void reset() { _computed = false; }
virtual osg::BoundingBox computeBound(const osg::Drawable& drawable) const;
protected:
mutable bool _computed;
@@ -57,40 +56,36 @@ 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;}
// this build the internal database about vertex influence and bones
void buildVertexInfluenceSet();
const VertexInfluenceSet& getVertexInfluenceSet() const;
void setNeedToComputeMatrix(bool state) { _needToComputeMatrix = state; }
bool getNeedToComputeMatrix() const { return _needToComputeMatrix; }
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();
void buildVertexInfluenceSet() { _rigTransformImplementation->prepareData(*this); }
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);
@@ -111,8 +106,6 @@ namespace osgAnimation
osg::ref_ptr<osg::Geometry> _geometry;
osg::ref_ptr<RigTransform> _rigTransformImplementation;
VertexInfluenceSet _vertexInfluenceSet;
osg::ref_ptr<VertexInfluenceMap> _vertexInfluenceMap;
osg::Matrix _matrixFromSkeletonToGeometry;
@@ -120,8 +113,7 @@ namespace osgAnimation
osg::observer_ptr<Skeleton> _root;
bool _needToComputeMatrix;
};
};
struct UpdateRigGeometry : public osg::Drawable::UpdateCallback
@@ -135,7 +127,8 @@ namespace osgAnimation
META_Object(osgAnimation, UpdateRigGeometry);
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drw) {
virtual void update(osg::NodeVisitor* nv, osg::Drawable* drw)
{
RigGeometry* geom = dynamic_cast<RigGeometry*>(drw);
if(!geom)
return;
@@ -151,7 +144,7 @@ namespace osgAnimation
osg::notify(osg::WARN) << "A RigGeometry did not find a parent skeleton for RigGeometry ( " << geom->getName() << " )" << std::endl;
return;
}
geom->buildVertexInfluenceSet();
geom->getRigTransformImplementation()->prepareData(*geom);
geom->setSkeleton(finder._root.get());
}
@@ -161,7 +154,8 @@ namespace osgAnimation
if(geom->getNeedToComputeMatrix())
geom->computeMatrixFromRootSkeleton();
if(geom->getSourceGeometry()) {
if(geom->getSourceGeometry())
{
osg::Drawable::UpdateCallback * up = dynamic_cast<osg::Drawable::UpdateCallback*>(geom->getSourceGeometry()->getUpdateCallback());
if(up)
up->update(nv, geom->getSourceGeometry());

View File

@@ -1,15 +1,15 @@
/* -*-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.
/* -*-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_RIGTRANSFORM
@@ -33,10 +33,31 @@ namespace osgAnimation
virtual void operator()(RigGeometry&) {}
/// to call manually when a skeleton is reacheable from the rig
/// in order to prepare technic data before rendering
virtual bool prepareData(RigGeometry&) { return true; }
protected:
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

@@ -1,5 +1,6 @@
/* -*-c++-*-
* Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
* Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
*
* 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
@@ -22,6 +23,8 @@
#include <osg/Matrix>
#include <osg/Array>
#define RIGTRANSHW_DEFAULT_FIRST_VERTATTRIB_TARGETTED 11
namespace osgAnimation
{
class RigGeometry;
@@ -37,54 +40,42 @@ 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;
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;
///set the first Vertex Attribute Array index of the rig generated by this technic (default:11)
void setFirstVertexAttributeTarget(unsigned int i) { _minAttribIndex=i; }
unsigned int getFirstVertexAttributeTarget()const { return _minAttribIndex; }
void setShader(osg::Shader* shader) { _shader = shader; }
const osg::Shader* getShader() const { return _shader.get(); }
osg::Shader* getShader() { return _shader.get(); }
osg::Vec4Array* getVertexAttrib(int index);
int getNumVertexAttrib();
osg::Vec4Array* getVertexAttrib(unsigned int index);
unsigned int getNumVertexAttrib() const { return _boneWeightAttribArrays.size(); }
const unsigned int &getNumBonesPerVertex() const { return _bonesPerVertex; }
const unsigned int &getNumVertexes() const { return _nbVertices; }
const BoneNamePaletteIndex& getBoneNameToPalette() { return _boneNameToPalette; }
const BonePalette& getBonePalette() { return _bonePalette; }
osg::Uniform* getMatrixPaletteUniform() { return _uniformMatrixPalette.get(); }
osg::Uniform* getMatrixPaletteUniform();
void computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry);
int getNumBonesPerVertex() const;
int getNumVertexes() const;
bool createPalette(int nbVertexes, BoneMap boneMap, const VertexInfluenceSet::VertexIndexToBoneWeightMap& vertexIndexToBoneWeightMap);
// update rig if needed
virtual void operator()(RigGeometry&);
void setShader(osg::Shader*);
const BoneNamePaletteIndex& getBoneNameToPalette() {
return _boneNameToPalette;
}
// init/reset animations data
virtual bool prepareData(RigGeometry& );
protected:
bool init(RigGeometry&);
unsigned int _bonesPerVertex;
unsigned int _nbVertices;
BoneWeightAttribList createVertexAttribList();
osg::Uniform* createVertexUniform();
int _bonesPerVertex;
int _nbVertexes;
VertexIndexWeightList _vertexIndexMatrixWeightList;
BonePalette _bonePalette;
BoneNamePaletteIndex _boneNameToPalette;
BoneWeightAttribList _boneWeightAttribArrays;
@@ -92,6 +83,13 @@ namespace osgAnimation
osg::ref_ptr<osg::Shader> _shader;
bool _needInit;
unsigned int _minAttribIndex;
bool buildPalette(const BoneMap& boneMap,const RigGeometry& rig);
//on first update
virtual bool init(RigGeometry& );
std::vector<IndexWeightList> _perVertexInfluences;
};
}

View File

@@ -1,5 +1,6 @@
/* -*-c++-*-
* Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
* Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
*
* 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
@@ -36,36 +37,49 @@ namespace osgAnimation
META_Object(osgAnimation,RigTransformSoftware)
virtual void operator()(RigGeometry&);
//to call when a skeleton is reacheable from the rig to prepare technic data
virtual bool prepareData(RigGeometry&);
class BoneWeight
typedef std::pair<unsigned int, float> LocalBoneIDWeight;
class BonePtrWeight: LocalBoneIDWeight
{
public:
BoneWeight(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; }
BonePtrWeight(unsigned int id,float weight, Bone*bone=0 ): LocalBoneIDWeight(id,weight), _boneptr(bone) {}
BonePtrWeight(const BonePtrWeight &bw2): LocalBoneIDWeight(bw2.getBoneID(),bw2.getWeight()), _boneptr(bw2._boneptr.get()) {}
inline const float & getWeight() const { return second; }
inline void setWeight(float b) { second=b; }
inline const unsigned int & getBoneID() const { return first; }
inline void setBoneID(unsigned int b) { first=b; }
inline bool operator< (const BonePtrWeight &b1) const {
if (second > b1.second) return true;
if (second < b1.second) return false;
return (first > b1.first);
}
///set Bone pointer
inline const Bone * getBonePtr() const { return _boneptr.get(); }
inline void setBonePtr(Bone*b) { _boneptr=b; }
protected:
osg::observer_ptr<Bone> _bone;
float _weight;
osg::observer_ptr< Bone > _boneptr;
};
typedef std::vector<BoneWeight> BoneWeightList;
typedef std::vector<int> VertexList;
typedef std::vector<BonePtrWeight> BonePtrWeightList;
class UniqBoneSetVertexSet
/// map a set of boneinfluence to a list of vertex indices sharing this set
class VertexGroup
{
public:
BoneWeightList& getBones() { return _bones; }
VertexList& getVertexes() { return _vertexes; }
inline BonePtrWeightList& getBoneWeights() { return _boneweights; }
void resetMatrix()
inline IndexList& getVertices() { return _vertexes; }
inline void resetMatrix()
{
_result.set(0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1);
}
void accummulateMatrix(const osg::Matrix& invBindMatrix, const osg::Matrix& matrix, osg::Matrix::value_type weight)
inline void accummulateMatrix(const osg::Matrix& invBindMatrix, const osg::Matrix& matrix, osg::Matrix::value_type weight)
{
osg::Matrix m = invBindMatrix * matrix;
osg::Matrix::value_type* ptr = m.ptr();
@@ -86,20 +100,19 @@ namespace osgAnimation
ptrresult[13] += ptr[13] * weight;
ptrresult[14] += ptr[14] * weight;
}
void computeMatrixForVertexSet()
inline 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->getBonePtr();
if (!bone)
{
osg::notify(osg::WARN) << this << " RigTransformSoftware::computeMatrixForVertexSet Warning a bone is null, skip it" << std::endl;
@@ -107,68 +120,67 @@ 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;}
void normalize();
inline const osg::Matrix& getMatrix() const { return _result; }
protected:
BoneWeightList _bones;
VertexList _vertexes;
BonePtrWeightList _boneweights;
IndexList _vertexes;
osg::Matrix _result;
};
template <class V> void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
template <class V>
inline 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(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.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++)
const IndexList& vertexes = uniq.getVertices();
for(IndexList::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)
template <class V>
inline 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(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.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++)
const IndexList& vertexes = uniq.getVertices();
for(IndexList::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);
}
}
}
protected:
bool init(RigGeometry&);
void initVertexSetFromBones(const BoneMap& map, const VertexInfluenceSet::UniqVertexSetToBoneSetList& influence);
std::vector<UniqBoneSetVertexSet> _boneSetVertexSet;
bool _needInit;
virtual bool init(RigGeometry&);
std::map<std::string,bool> _invalidInfluence;
typedef std::vector<VertexGroup> VertexGroupList;
VertexGroupList _uniqVertexGroupList;
void buildMinimumUpdateSet(const RigGeometry&rig );
};
}

View File

@@ -1,5 +1,6 @@
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
* Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
*
* 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
@@ -23,22 +24,31 @@
namespace osgAnimation
{
class Skeleton;
// first is vertex index, and second the weight, the
typedef std::pair<int, float> VertexIndexWeight;
typedef std::vector<VertexIndexWeight> VertexList;
class OSGANIMATION_EXPORT VertexInfluence : public VertexList
// first is bonename, and second the weight
typedef std::pair<std::string, float> BoneWeight;
// first is vertex index, and second the weight
typedef std::pair<unsigned int, float> VertexIndexWeight;
// list of IndexWeight
typedef std::vector<VertexIndexWeight> IndexWeightList;
// list of IndexWeight
typedef std::vector<BoneWeight> BoneWeightList;
// list of Index
typedef std::vector<unsigned int> IndexList;
//Bone influence list
class OSGANIMATION_EXPORT VertexInfluence : public IndexWeightList
{
public:
const std::string& getName() const { return _name;}
void setName(const std::string& name) { _name = name;}
const std::string& getName() const { return _name; }
void setName(const std::string& name) { _name = name; }
protected:
// the name is the bone to link to
std::string _name;
};
class VertexInfluenceMap : public std::map<std::string, VertexInfluence> , public osg::Object
class VertexInfluenceMap : public std::map<std::string, VertexInfluence>, public osg::Object
{
public:
META_Object(osgAnimation, VertexInfluenceMap);
@@ -46,61 +56,32 @@ namespace osgAnimation
VertexInfluenceMap() {}
VertexInfluenceMap(const osgAnimation::VertexInfluenceMap& org, const osg::CopyOp& copyop):
std::map<std::string, VertexInfluence>(org),
osg::Object(org, copyop)
{}
};
osg::Object(org, copyop) {}
///normalize per vertex weights given numvert of the attached mesh
void normalize(unsigned int numvert);
// this class manage VertexInfluence database by mesh
// reference bones per vertex ...
class OSGANIMATION_EXPORT VertexInfluenceSet
{
public:
typedef std::vector<VertexInfluence> BoneToVertexList;
///remove weakest influences in order to fit targetted numbonepervertex
void cullInfluenceCountPerVertex(unsigned int maxnumbonepervertex, float minweight=0, bool renormalize=true);
class BoneWeight
//compute PerVertexInfluenceList
void computePerVertexInfluenceList(std::vector<BoneWeightList>& perVertexInfluenceList, unsigned int numvert) const;
/// map a set of boneinfluence to a list of vertex indices sharing this set
class VertexGroup: public std::pair<BoneWeightList, IndexList>
{
public:
BoneWeight(const std::string& name, float weight) : _boneName(name), _weight(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;
inline const BoneWeightList& getBoneWeights() const { return first; }
inline void setBoneWeights( BoneWeightList& o ) { first=o; }
inline IndexList& vertIDs() { return second; }
};
typedef std::vector<BoneWeight> BoneWeightList;
typedef std::map<int,BoneWeightList> VertexIndexToBoneWeightMap;
/// compute the minimal VertexGroup Set in which vertices shares the same influence set
void computeMinimalVertexGroupList(std::vector<VertexGroup>&uniqVertexGroupList, unsigned int numvert) const;
class UniqVertexSetToBoneSet
{
public:
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;}
protected:
std::vector<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;}
void addVertexInfluence(const VertexInfluence& v);
void buildVertex2BoneList();
void buildUniqVertexSetToBoneSetList();
void clear();
const VertexIndexToBoneWeightMap& getVertexToBoneList() const;
protected:
BoneToVertexList _bone2Vertexes;
VertexIndexToBoneWeightMap _vertex2Bones;
UniqVertexSetToBoneSetList _uniqVertexSetToBoneSet;
//Experimental removal of unexpressed bone from the skeleton
void removeUnexpressedBones(Skeleton &skel) const;
};
}
#endif