This commit is contained in:
Julien Valentin 2017-08-30 13:21:32 +02:00
parent 985d766f05
commit fd9b8f103e
5 changed files with 67 additions and 101 deletions

View File

@ -17,7 +17,7 @@
#include <osgAnimation/Export> #include <osgAnimation/Export>
#include <osgAnimation/Skeleton> #include <osgAnimation/Skeleton>
#include <osgAnimation/RigTransformSoftware> #include <osgAnimation/RigTransform>
#include <osgAnimation/VertexInfluence> #include <osgAnimation/VertexInfluence>
#include <osg/Geometry> #include <osg/Geometry>

View File

@ -38,36 +38,32 @@ namespace osgAnimation
META_Object(osgAnimation,RigTransformHardware); META_Object(osgAnimation,RigTransformHardware);
typedef osg::Matrix MatrixType;
typedef std::vector<osg::ref_ptr<osg::Vec4Array> > BoneWeightAttribList; typedef std::vector<osg::ref_ptr<osg::Vec4Array> > BoneWeightAttribList;
typedef std::vector<osg::ref_ptr<Bone> > BonePalette; typedef std::vector<osg::ref_ptr<Bone> > BonePalette;
typedef std::map<std::string, unsigned int> BoneNamePaletteIndex; typedef std::map<std::string, unsigned int> BoneNamePaletteIndex;
typedef std::vector<osg::Matrix> MatrixPalette; typedef std::vector<osg::Matrix> MatrixPalette;
osg::Vec4Array* getVertexAttrib(unsigned int index); osg::Vec4Array* getVertexAttrib(unsigned int index);
unsigned int getNumVertexAttrib(); unsigned int getNumVertexAttrib() const {return _boneWeightAttribArrays.size();}
void setShader(osg::Shader* shader) { _shader = shader; }
osg::Shader* getShader() const { return _shader; }
const unsigned int &getNumBonesPerVertex() const{ return _bonesPerVertex; }
const unsigned int &getNumVertexes() const { return _nbVertexes; }
const BoneNamePaletteIndex& getBoneNameToPalette(){ return _boneNameToPalette; }
const BonePalette& getBonePalette() { return _bonePalette; }
osg::Uniform* getMatrixPaletteUniform() { return _uniformMatrixPalette; }
osg::Uniform* getMatrixPaletteUniform();
void computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry); void computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry);
unsigned int getNumBonesPerVertex() const;
unsigned int getNumVertexes() const;
virtual void operator()(RigGeometry&); virtual void operator()(RigGeometry&);
virtual bool prepareData(RigGeometry& ); virtual bool prepareData(RigGeometry& );
void setShader(osg::Shader*);
const BoneNamePaletteIndex& getBoneNameToPalette() {
return _boneNameToPalette;
}
protected: protected:
osg::Uniform* createVertexUniform();
unsigned int _bonesPerVertex; unsigned int _bonesPerVertex;
unsigned int _nbVertexes; unsigned int _nbVertexes;
@ -79,7 +75,7 @@ namespace osgAnimation
bool _needInit; bool _needInit;
bool buildPalette(BoneMap&boneMap ,RigGeometry&rig); bool buildPalette(const BoneMap& boneMap ,const RigGeometry& rig);
}; };
} }

View File

@ -129,7 +129,7 @@ namespace osgAnimation
inline void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst) 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 // the result of matrix mult should be cached to be used for vertexes transform and normal transform and maybe other computation
for(VertexGroupList::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg) for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
{ {
VertexGroup& uniq = *itvg; VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet(); uniq.computeMatrixForVertexSet();
@ -148,7 +148,7 @@ namespace osgAnimation
template <class V> template <class V>
inline void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst) inline void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
{ {
for(VertexGroupList::iterator itvg=_uniqInfluenceSet2VertIDList.begin(); itvg!=_uniqInfluenceSet2VertIDList.end(); ++itvg) for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
{ {
VertexGroup& uniq = *itvg; VertexGroup& uniq = *itvg;
uniq.computeMatrixForVertexSet(); uniq.computeMatrixForVertexSet();
@ -170,7 +170,7 @@ namespace osgAnimation
typedef std::vector<VertexGroup> VertexGroupList; typedef std::vector<VertexGroup> VertexGroupList;
VertexGroupList _uniqInfluenceSet2VertIDList; VertexGroupList _uniqVertexGroupList;
void buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ); void buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig );
}; };

View File

@ -48,17 +48,6 @@ osg::Vec4Array* RigTransformHardware::getVertexAttrib(unsigned int index)
return _boneWeightAttribArrays[index].get(); return _boneWeightAttribArrays[index].get();
} }
unsigned int RigTransformHardware::getNumVertexAttrib()
{
return _boneWeightAttribArrays.size();
}
osg::Uniform* RigTransformHardware::getMatrixPaletteUniform()
{
return _uniformMatrixPalette.get();
}
void RigTransformHardware::computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry) void RigTransformHardware::computeMatrixPaletteUniform(const osg::Matrix& transformFromSkeletonToGeometry, const osg::Matrix& invTransformFromSkeletonToGeometry)
{ {
for (unsigned int i = 0; i < _bonePalette.size(); i++) for (unsigned int i = 0; i < _bonePalette.size(); i++)
@ -74,11 +63,7 @@ void RigTransformHardware::computeMatrixPaletteUniform(const osg::Matrix& transf
} }
unsigned int RigTransformHardware::getNumBonesPerVertex() const { return _bonesPerVertex;} void createVertexAttribList(RigTransformHardware& rig,const std::vector<std::vector<IndexWeight> > &perVertexInfluences,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
unsigned int RigTransformHardware::getNumVertexes() const { return _nbVertexes;}
typedef std::vector<std::vector<IndexWeight> > VertexIndexWeightList;
void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightList&_vertexIndexMatrixWeightList,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
// //
// create vertex attribute by 2 bones // create vertex attribute by 2 bones
@ -89,11 +74,15 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
// than the 4 bones using two vertex attributes // than the 4 bones using two vertex attributes
// //
void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightList& _vertexIndexMatrixWeightList, RigTransformHardware::BoneWeightAttribList& boneWeightAttribArrays) typedef std::vector<std::vector<IndexWeight> > PerVertexInfList;
void createVertexAttribList(RigTransformHardware& rig,
const PerVertexInfList & perVertexInfluences,
RigTransformHardware::BoneWeightAttribList& boneWeightAttribArrays)
{ {
unsigned int nbVertices= rig.getNumVertexes(); unsigned int nbVertices= rig.getNumVertexes();
unsigned int maxbonepervertex=rig.getNumBonesPerVertex(); unsigned int maxbonepervertex=rig.getNumBonesPerVertex();
unsigned int nbArray = static_cast<unsigned int>(ceilf( ((float)maxbonepervertex) * 0.5f)); unsigned int nbArray = static_cast<unsigned int>(ceilf( ((float)maxbonepervertex) * 0.5f));
if (!nbArray) if (!nbArray)
return ; return ;
@ -105,7 +94,6 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
array->resize( nbVertices); array->resize( nbVertices);
for (unsigned int j = 0; j < nbVertices; j++) for (unsigned int j = 0; j < nbVertices; j++)
{ {
for (unsigned int b = 0; b < 2; b++) for (unsigned int b = 0; b < 2; b++)
{ {
// the granularity is 2 so if we have only one bone // the granularity is 2 so if we have only one bone
@ -116,8 +104,8 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
(*array)[j][1 + boneIndexInVec4] = 0; (*array)[j][1 + boneIndexInVec4] = 0;
if (boneIndexInList < maxbonepervertex) if (boneIndexInList < maxbonepervertex)
{ {
float boneIndex = static_cast<float>(_vertexIndexMatrixWeightList[j][boneIndexInList].getIndex()); float boneIndex = static_cast<float>(perVertexInfluences[j][boneIndexInList].getIndex());
float boneWeight = _vertexIndexMatrixWeightList[j][boneIndexInList].getWeight(); float boneWeight = perVertexInfluences[j][boneIndexInList].getWeight();
// fill the vec4 // fill the vec4
(*array)[j][0 + boneIndexInVec4] = boneIndex; (*array)[j][0 + boneIndexInVec4] = boneIndex;
(*array)[j][1 + boneIndexInVec4] = boneWeight; (*array)[j][1 + boneIndexInVec4] = boneWeight;
@ -125,19 +113,7 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
} }
} }
} }
return ; return;
}
osg::Uniform* RigTransformHardware::createVertexUniform()
{
osg::Uniform* uniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "matrixPalette", _bonePalette.size());
return uniform;
}
void RigTransformHardware::setShader(osg::Shader* shader)
{
_shader = shader;
} }
bool RigTransformHardware::prepareData(RigGeometry& rig) bool RigTransformHardware::prepareData(RigGeometry& rig)
@ -252,37 +228,32 @@ bool RigTransformHardware::prepareData(RigGeometry& rig)
_needInit = false; _needInit = false;
return true; return true;
} }
void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightList&_vertexIndexMatrixWeightList,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) { bool RigTransformHardware::buildPalette(const BoneMap&boneMap ,const RigGeometry&rig) {
_nbVertexes = rig.getVertexArray()->getNumElements();
IndexWeightList::size_type maxBonePerVertex=0;
typedef std::pair<float,unsigned int> FloatInt;
std::vector< FloatInt > sums;///stat totalweight nbref
sums.resize(_nbVertexes);
typedef std::map<std::string, int> BoneNameCountMap; typedef std::map<std::string, int> BoneNameCountMap;
_nbVertexes = rig.getVertexArray()->getNumElements();
_boneWeightAttribArrays.resize(0);
_bonePalette.clear(); _bonePalette.clear();
_boneNameToPalette.clear(); _boneNameToPalette.clear();
IndexWeightList::size_type maxBonePerVertex=0;
BoneNameCountMap boneNameCountMap; BoneNameCountMap boneNameCountMap;
VertexInfluenceMap *vertexInfluenceMap=rig.getInfluenceMap(); const VertexInfluenceMap &vertexInfluenceMap=*rig.getInfluenceMap();
BoneNamePaletteIndex::iterator boneName2PaletteIndex; BoneNamePaletteIndex::iterator boneName2PaletteIndex;
_boneWeightAttribArrays.resize(0);
// init temp vertex attribute data // init temp vertex attribute data
VertexIndexWeightList vertexIndexWeight; std::vector<std::vector<IndexWeight> > perVertexInfluences;
vertexIndexWeight.resize(_nbVertexes); perVertexInfluences.resize(_nbVertexes);
unsigned int paletteindex; unsigned int paletteindex;
for (osgAnimation::VertexInfluenceMap::iterator mapit = vertexInfluenceMap->begin(); for (osgAnimation::VertexInfluenceMap::const_iterator boneinflistit = vertexInfluenceMap.begin();
mapit != vertexInfluenceMap->end(); boneinflistit != vertexInfluenceMap.end();
++mapit) ++boneinflistit)
{ {
const IndexWeightList& boneinflist = mapit->second; const IndexWeightList& boneinflist = boneinflistit->second;
const std::string& bonename = mapit->first; const std::string& bonename = boneinflistit->first;
BoneMap::const_iterator bonebyname; BoneMap::const_iterator bonebyname;
if ((bonebyname=boneMap.find(bonename)) == boneMap.end()) if ((bonebyname=boneMap.find(bonename)) == boneMap.end())
{ {
@ -307,7 +278,7 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
const IndexWeight& iw = *infit; const IndexWeight& iw = *infit;
const unsigned int &index = iw.getIndex(); const unsigned int &index = iw.getIndex();
const float &weight = iw.getWeight(); const float &weight = iw.getWeight();
IndexWeightList & iwlist=vertexIndexWeight[index]; IndexWeightList & iwlist=perVertexInfluences[index];
if(fabs(weight) > 1e-4) // don't use bone with weight too small if(fabs(weight) > 1e-4) // don't use bone with weight too small
{ {
@ -315,7 +286,7 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
} }
else else
{ {
OSG_WARN << "RigTransformHardware::buildPalette Bone " << bonename << " has a weight " << weight << " for vertex " << index << " this bone will not be in the palette" << std::endl; OSG_INFO << "RigTransformHardware::buildPalette Bone " << bonename << " has a weight " << weight << " for vertex " << index << " this bone will not be in the palette" << std::endl;
} }
maxBonePerVertex = osg::maximum(maxBonePerVertex, iwlist.size()); maxBonePerVertex = osg::maximum(maxBonePerVertex, iwlist.size());
@ -334,11 +305,11 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
} }
///normalize ///normalize
/*unsigned int vertid=0; unsigned int vertid=0;
for(VertexIndexWeightList::iterator it=vertexIndexWeight.begin();it!=vertexIndexWeight.end();++it,++vertid) for(PerVertexInfList::iterator vertinfit=perVertexInfluences.begin(); vertinfit != perVertexInfluences.end(); ++vertinfit,++vertid)
{ {
float sum=0; float sum=0;
for(IndexWeightList::iterator iwit=it->begin();iwit!=it->end();++iwit) for(IndexWeightList::iterator iwit = vertinfit->begin(); iwit != vertinfit->end(); ++iwit)
sum+=iwit->second; sum+=iwit->second;
if(sum< 1e-4){ if(sum< 1e-4){
@ -347,15 +318,15 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
else else
{ {
sum=1.0f/sum; sum=1.0f/sum;
for(IndexWeightList::iterator iwit=it->begin();iwit!=it->end();++iwit) for(IndexWeightList::iterator iwit=vertinfit->begin();iwit!=vertinfit->end();++iwit)
iwit->second*=sum; iwit->second*=sum;
} }
}*/ }
_bonesPerVertex = maxBonePerVertex; _bonesPerVertex = maxBonePerVertex;
_uniformMatrixPalette = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "matrixPalette", _bonePalette.size()); _uniformMatrixPalette = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "matrixPalette", _bonePalette.size());
createVertexAttribList(*this,vertexIndexWeight,this->_boneWeightAttribArrays); createVertexAttribList(*this,perVertexInfluences,this->_boneWeightAttribArrays);
return true; return true;
} }

View File

@ -38,19 +38,19 @@ RigTransformSoftware::RigTransformSoftware(const RigTransformSoftware& rts,const
typedef std::vector<RigTransformSoftware::BonePtrWeight> BonePtrWeightList; typedef std::vector<RigTransformSoftware::BonePtrWeight> BonePtrWeightList;
void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const RigGeometry&rig ){ void RigTransformSoftware::buildMinimumUpdateSet( const BoneMap&boneMap, const RigGeometry&rig ){
///1 Create Index2Vec<BoneWeight> ///1 Create Index2Vec<BoneWeight>
std::vector<BonePtrWeightList> _vertex2Bones; const VertexInfluenceMap &vertexInfluenceMap=*rig.getInfluenceMap();
_vertex2Bones.resize(rig.getSourceGeometry()->getVertexArray()->getNumElements()); std::vector<BonePtrWeightList> perVertexInfluences;
perVertexInfluences.resize(rig.getSourceGeometry()->getVertexArray()->getNumElements());
const VertexInfluenceMap *_vertexInfluenceMap=rig.getInfluenceMap(); for (osgAnimation::VertexInfluenceMap::const_iterator perBoneinfit = vertexInfluenceMap.begin();
for (osgAnimation::VertexInfluenceMap::const_iterator it = _vertexInfluenceMap->begin(); perBoneinfit != vertexInfluenceMap.end();
it != _vertexInfluenceMap->end(); ++perBoneinfit)
++it)
{ {
const IndexWeightList& inflist = it->second; const IndexWeightList& inflist = perBoneinfit->second;
const std::string& bonename = it->first; const std::string& bonename = perBoneinfit->first;
if (bonename.empty()) { if (bonename.empty()) {
OSG_WARN << "RigTransformSoftware::VertexInfluenceMap contains unamed bone IndexWeightList" << std::endl; OSG_WARN << "RigTransformSoftware::VertexInfluenceMap contains unamed bone IndexWeightList" << std::endl;
@ -71,13 +71,13 @@ void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const Rig
const unsigned int &index = iw.getIndex(); const unsigned int &index = iw.getIndex();
float weight = iw.getWeight(); float weight = iw.getWeight();
_vertex2Bones[index].push_back(BonePtrWeight(bone, weight)); perVertexInfluences[index].push_back(BonePtrWeight(bone, weight));
} }
} }
// normalize _vertex2Bones weight per vertex // normalize _vertex2Bones weight per vertex
unsigned vertexID=0; unsigned vertexID=0;
for (std::vector<BonePtrWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it, ++vertexID) for (std::vector<BonePtrWeightList>::iterator it = perVertexInfluences.begin(); it != perVertexInfluences.end(); ++it, ++vertexID)
{ {
BonePtrWeightList& bones = *it; BonePtrWeightList& bones = *it;
float sum = 0; float sum = 0;
@ -97,32 +97,31 @@ void RigTransformSoftware::buildMinimumUpdateSet(const BoneMap&boneMap,const Rig
///2 Create inverse mapping Vec<BoneWeight>2Vec<Index> from previous built Index2Vec<BoneWeight> ///2 Create inverse mapping Vec<BoneWeight>2Vec<Index> from previous built Index2Vec<BoneWeight>
///in order to minimize weighted matrices computation on update ///in order to minimize weighted matrices computation on update
_uniqInfluenceSet2VertIDList.clear(); _uniqVertexGroupList.clear();
typedef std::map<BonePtrWeightList, VertexGroup> UnifyBoneGroup; typedef std::map<BonePtrWeightList, VertexGroup> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer; UnifyBoneGroup unifyBuffer;
vertexID=0; vertexID=0;
; for (std::vector<BonePtrWeightList>::iterator perVertinfit = perVertexInfluences.begin(); perVertinfit!=perVertexInfluences.end(); ++perVertinfit,++vertexID)
for (std::vector<BonePtrWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it,++vertexID)
{ {
BonePtrWeightList &bones = *it; BonePtrWeightList &boneinfs = *perVertinfit;
// sort the vector to have a consistent key // sort the vector to have a consistent key
std::sort(bones.begin(), bones.end() ); std::sort(boneinfs.begin(), boneinfs.end() );
// we use the vector<BoneWeight> as key to differentiate group // we use the vector<BoneWeight> as key to differentiate group
UnifyBoneGroup::iterator result = unifyBuffer.find(bones); UnifyBoneGroup::iterator result = unifyBuffer.find(boneinfs);
if (result != unifyBuffer.end()) if (result != unifyBuffer.end())
result->second.getVertices().push_back(vertexID); result->second.getVertices().push_back(vertexID);
else else
{ {
VertexGroup& vg = unifyBuffer[bones]; VertexGroup& vg = unifyBuffer[boneinfs];
vg.getBoneWeights() = bones; vg.getBoneWeights() = boneinfs;
vg.getVertices().push_back(vertexID); vg.getVertices().push_back(vertexID);
} }
} }
_uniqInfluenceSet2VertIDList.reserve(unifyBuffer.size()); _uniqVertexGroupList.reserve(unifyBuffer.size());
for (UnifyBoneGroup::const_iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it) for (UnifyBoneGroup::const_iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it)
_uniqInfluenceSet2VertIDList.push_back(it->second); _uniqVertexGroupList.push_back(it->second);
OSG_WARN << "uniq groups " << _uniqInfluenceSet2VertIDList.size() << " for " << rig.getName() << std::endl; OSG_INFO << "uniq groups " << _uniqVertexGroupList.size() << " for " << rig.getName() << std::endl;
} }
bool RigTransformSoftware::prepareData(RigGeometry&rig) { bool RigTransformSoftware::prepareData(RigGeometry&rig) {