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/Skeleton>
#include <osgAnimation/RigTransformSoftware>
#include <osgAnimation/RigTransform>
#include <osgAnimation/VertexInfluence>
#include <osg/Geometry>

View File

@ -38,36 +38,32 @@ namespace osgAnimation
META_Object(osgAnimation,RigTransformHardware);
typedef osg::Matrix MatrixType;
typedef std::vector<osg::ref_ptr<osg::Vec4Array> > BoneWeightAttribList;
typedef std::vector<osg::ref_ptr<Bone> > BonePalette;
typedef std::map<std::string, unsigned int> BoneNamePaletteIndex;
typedef std::vector<osg::Matrix> MatrixPalette;
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);
unsigned int getNumBonesPerVertex() const;
unsigned int getNumVertexes() const;
virtual void operator()(RigGeometry&);
virtual bool prepareData(RigGeometry& );
void setShader(osg::Shader*);
const BoneNamePaletteIndex& getBoneNameToPalette() {
return _boneNameToPalette;
}
protected:
osg::Uniform* createVertexUniform();
unsigned int _bonesPerVertex;
unsigned int _nbVertexes;
@ -79,7 +75,7 @@ namespace osgAnimation
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)
{
// 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;
uniq.computeMatrixForVertexSet();
@ -148,7 +148,7 @@ namespace osgAnimation
template <class V>
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;
uniq.computeMatrixForVertexSet();
@ -170,7 +170,7 @@ namespace osgAnimation
typedef std::vector<VertexGroup> VertexGroupList;
VertexGroupList _uniqInfluenceSet2VertIDList;
VertexGroupList _uniqVertexGroupList;
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();
}
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)
{
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;}
unsigned int RigTransformHardware::getNumVertexes() const { return _nbVertexes;}
typedef std::vector<std::vector<IndexWeight> > VertexIndexWeightList;
void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightList&_vertexIndexMatrixWeightList,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
void createVertexAttribList(RigTransformHardware& rig,const std::vector<std::vector<IndexWeight> > &perVertexInfluences,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
//
// create vertex attribute by 2 bones
@ -89,11 +74,15 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
// 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 maxbonepervertex=rig.getNumBonesPerVertex();
unsigned int nbArray = static_cast<unsigned int>(ceilf( ((float)maxbonepervertex) * 0.5f));
if (!nbArray)
return ;
@ -105,7 +94,6 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
array->resize( nbVertices);
for (unsigned int j = 0; j < nbVertices; j++)
{
for (unsigned int b = 0; b < 2; b++)
{
// 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;
if (boneIndexInList < maxbonepervertex)
{
float boneIndex = static_cast<float>(_vertexIndexMatrixWeightList[j][boneIndexInList].getIndex());
float boneWeight = _vertexIndexMatrixWeightList[j][boneIndexInList].getWeight();
float boneIndex = static_cast<float>(perVertexInfluences[j][boneIndexInList].getIndex());
float boneWeight = perVertexInfluences[j][boneIndexInList].getWeight();
// fill the vec4
(*array)[j][0 + boneIndexInVec4] = boneIndex;
(*array)[j][1 + boneIndexInVec4] = boneWeight;
@ -125,19 +113,7 @@ void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightLis
}
}
}
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;
return;
}
bool RigTransformHardware::prepareData(RigGeometry& rig)
@ -252,37 +228,32 @@ bool RigTransformHardware::prepareData(RigGeometry& rig)
_needInit = false;
return true;
}
void createVertexAttribList(RigTransformHardware& rig,const VertexIndexWeightList&_vertexIndexMatrixWeightList,RigTransformHardware::BoneWeightAttribList & boneWeightAttribArrays);
bool RigTransformHardware::buildPalette(BoneMap&boneMap ,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);
bool RigTransformHardware::buildPalette(const BoneMap&boneMap ,const RigGeometry&rig) {
typedef std::map<std::string, int> BoneNameCountMap;
_nbVertexes = rig.getVertexArray()->getNumElements();
_boneWeightAttribArrays.resize(0);
_bonePalette.clear();
_boneNameToPalette.clear();
IndexWeightList::size_type maxBonePerVertex=0;
BoneNameCountMap boneNameCountMap;
VertexInfluenceMap *vertexInfluenceMap=rig.getInfluenceMap();
const VertexInfluenceMap &vertexInfluenceMap=*rig.getInfluenceMap();
BoneNamePaletteIndex::iterator boneName2PaletteIndex;
_boneWeightAttribArrays.resize(0);
// init temp vertex attribute data
VertexIndexWeightList vertexIndexWeight;
vertexIndexWeight.resize(_nbVertexes);
std::vector<std::vector<IndexWeight> > perVertexInfluences;
perVertexInfluences.resize(_nbVertexes);
unsigned int paletteindex;
for (osgAnimation::VertexInfluenceMap::iterator mapit = vertexInfluenceMap->begin();
mapit != vertexInfluenceMap->end();
++mapit)
for (osgAnimation::VertexInfluenceMap::const_iterator boneinflistit = vertexInfluenceMap.begin();
boneinflistit != vertexInfluenceMap.end();
++boneinflistit)
{
const IndexWeightList& boneinflist = mapit->second;
const std::string& bonename = mapit->first;
const IndexWeightList& boneinflist = boneinflistit->second;
const std::string& bonename = boneinflistit->first;
BoneMap::const_iterator bonebyname;
if ((bonebyname=boneMap.find(bonename)) == boneMap.end())
{
@ -307,7 +278,7 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
const IndexWeight& iw = *infit;
const unsigned int &index = iw.getIndex();
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
{
@ -315,7 +286,7 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
}
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());
@ -334,11 +305,11 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
}
///normalize
/*unsigned int vertid=0;
for(VertexIndexWeightList::iterator it=vertexIndexWeight.begin();it!=vertexIndexWeight.end();++it,++vertid)
unsigned int vertid=0;
for(PerVertexInfList::iterator vertinfit=perVertexInfluences.begin(); vertinfit != perVertexInfluences.end(); ++vertinfit,++vertid)
{
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;
if(sum< 1e-4){
@ -347,15 +318,15 @@ bool RigTransformHardware::buildPalette(BoneMap&boneMap ,RigGeometry&rig) {
else
{
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;
}
}*/
}
_bonesPerVertex = maxBonePerVertex;
_uniformMatrixPalette = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "matrixPalette", _bonePalette.size());
createVertexAttribList(*this,vertexIndexWeight,this->_boneWeightAttribArrays);
createVertexAttribList(*this,perVertexInfluences,this->_boneWeightAttribArrays);
return true;
}

View File

@ -38,19 +38,19 @@ RigTransformSoftware::RigTransformSoftware(const RigTransformSoftware& rts,const
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>
std::vector<BonePtrWeightList> _vertex2Bones;
_vertex2Bones.resize(rig.getSourceGeometry()->getVertexArray()->getNumElements());
const VertexInfluenceMap &vertexInfluenceMap=*rig.getInfluenceMap();
std::vector<BonePtrWeightList> perVertexInfluences;
perVertexInfluences.resize(rig.getSourceGeometry()->getVertexArray()->getNumElements());
const VertexInfluenceMap *_vertexInfluenceMap=rig.getInfluenceMap();
for (osgAnimation::VertexInfluenceMap::const_iterator it = _vertexInfluenceMap->begin();
it != _vertexInfluenceMap->end();
++it)
for (osgAnimation::VertexInfluenceMap::const_iterator perBoneinfit = vertexInfluenceMap.begin();
perBoneinfit != vertexInfluenceMap.end();
++perBoneinfit)
{
const IndexWeightList& inflist = it->second;
const std::string& bonename = it->first;
const IndexWeightList& inflist = perBoneinfit->second;
const std::string& bonename = perBoneinfit->first;
if (bonename.empty()) {
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();
float weight = iw.getWeight();
_vertex2Bones[index].push_back(BonePtrWeight(bone, weight));
perVertexInfluences[index].push_back(BonePtrWeight(bone, weight));
}
}
// normalize _vertex2Bones weight per vertex
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;
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>
///in order to minimize weighted matrices computation on update
_uniqInfluenceSet2VertIDList.clear();
_uniqVertexGroupList.clear();
typedef std::map<BonePtrWeightList, VertexGroup> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
vertexID=0;
;
for (std::vector<BonePtrWeightList>::iterator it = _vertex2Bones.begin(); it != _vertex2Bones.end(); ++it,++vertexID)
for (std::vector<BonePtrWeightList>::iterator perVertinfit = perVertexInfluences.begin(); perVertinfit!=perVertexInfluences.end(); ++perVertinfit,++vertexID)
{
BonePtrWeightList &bones = *it;
BonePtrWeightList &boneinfs = *perVertinfit;
// 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
UnifyBoneGroup::iterator result = unifyBuffer.find(bones);
UnifyBoneGroup::iterator result = unifyBuffer.find(boneinfs);
if (result != unifyBuffer.end())
result->second.getVertices().push_back(vertexID);
else
{
VertexGroup& vg = unifyBuffer[bones];
vg.getBoneWeights() = bones;
VertexGroup& vg = unifyBuffer[boneinfs];
vg.getBoneWeights() = boneinfs;
vg.getVertices().push_back(vertexID);
}
}
_uniqInfluenceSet2VertIDList.reserve(unifyBuffer.size());
_uniqVertexGroupList.reserve(unifyBuffer.size());
for (UnifyBoneGroup::const_iterator it = unifyBuffer.begin(); it != unifyBuffer.end(); ++it)
_uniqInfluenceSet2VertIDList.push_back(it->second);
OSG_WARN << "uniq groups " << _uniqInfluenceSet2VertIDList.size() << " for " << rig.getName() << std::endl;
_uniqVertexGroupList.push_back(it->second);
OSG_INFO << "uniq groups " << _uniqVertexGroupList.size() << " for " << rig.getName() << std::endl;
}
bool RigTransformSoftware::prepareData(RigGeometry&rig) {