This commit is contained in:
Julien Valentin
2017-09-04 02:27:54 +02:00
parent 041a2a6e72
commit 7da072b433
3 changed files with 101 additions and 90 deletions

View File

@@ -94,18 +94,23 @@ unsigned int createVertexAttribList(const PerVertexInfList & perVertexInfluences
return 0;
///create vertex attrib arrays
boneWeightAttribArrays.reserve(nbArray);
boneWeightAttribArrays.resize(nbArray);
for(unsigned int j=0; j< nbArray; ++j)
for(unsigned int j = 0; j< nbArray; ++j)
{
boneWeightAttribArrays[j] = new osg::Vec4Array(osg::Array::BIND_PER_VERTEX);
boneWeightAttribArrays[j]->resize(perVertexInfluences.size());
osg::Vec4Array* vecattr = new osg::Vec4Array(osg::Array::BIND_PER_VERTEX);
vecattr->reserve(perVertexInfluences.size());
vecattr->resize(perVertexInfluences.size());
boneWeightAttribArrays[j] = vecattr;
}
///populate vertex attrib arrays
for(PerVertexInfList::const_iterator vertinfit=perVertexInfluences.begin(); vertinfit != perVertexInfluences.end(); ++vertinfit,++vertid)
for(PerVertexInfList::const_iterator vertinfit = perVertexInfluences.begin();
vertinfit != perVertexInfluences.end();
++vertinfit, ++vertid)
{
//sum for normalization
float sum=0;
float sum = 0;
for(IndexWeightList::const_iterator iwit = vertinfit->begin(); iwit != vertinfit->end(); ++iwit)
sum+=iwit->second;
@@ -116,10 +121,10 @@ unsigned int createVertexAttribList(const PerVertexInfList & perVertexInfluences
}
else
{
sum=1.0f/sum;
sum = 1.0f/sum;
for (unsigned int j = 0; j < nbArray; ++j)
{
osg::Vec4& dest=(* boneWeightAttribArrays[j])[vertid];
osg::Vec4& dest = (* boneWeightAttribArrays[j])[vertid];
for (unsigned int b = 0; b < 2; ++b)
{
boneIndexInVec4 = b*2;
@@ -148,9 +153,10 @@ bool RigTransformHardware::prepareData(RigGeometry& rig)
{
_nbVertices = rig.getSourceGeometry()->getVertexArray()->getNumElements();
const VertexInfluenceMap &vertexInfluenceMap = *rig.getInfluenceMap();
_perVertexInfluences.reserve(_nbVertices);
_perVertexInfluences.resize(_nbVertices);
unsigned int localboneid=0;
unsigned int localboneid = 0;
for (VertexInfluenceMap::const_iterator boneinflistit = vertexInfluenceMap.begin();
boneinflistit != vertexInfluenceMap.end();
++boneinflistit, ++localboneid)
@@ -179,7 +185,7 @@ bool RigTransformHardware::prepareData(RigGeometry& rig)
}
bool RigTransformHardware::buildPalette(const BoneMap&boneMap,const RigGeometry&rig)
bool RigTransformHardware::buildPalette(const BoneMap& boneMap, const RigGeometry& rig)
{
typedef std::map<std::string, int> BoneNameCountMap;
@@ -187,7 +193,7 @@ bool RigTransformHardware::buildPalette(const BoneMap&boneMap,const RigGeometry&
_bonePalette.clear();
_boneNameToPalette.clear();
IndexWeightList::size_type maxBonePerVertex=0;
IndexWeightList::size_type maxBonePerVertex = 0;
BoneNameCountMap boneNameCountMap;
const VertexInfluenceMap &vertexInfluenceMap = *rig.getInfluenceMap();
@@ -214,37 +220,37 @@ bool RigTransformHardware::buildPalette(const BoneMap&boneMap,const RigGeometry&
localid2bone.push_back(-1);
continue;
}
if ((boneName2PaletteIndex= _boneNameToPalette.find(bonename)) != _boneNameToPalette.end())
if ( (boneName2PaletteIndex = _boneNameToPalette.find(bonename)) != _boneNameToPalette.end())
{
boneNameCountMap[bonename]++;
paletteindex= boneName2PaletteIndex->second ;
paletteindex = boneName2PaletteIndex->second ;
}
else
{
boneNameCountMap[bonename] = 1; // for stats
_boneNameToPalette[bonename] = _bonePalette.size() ;
paletteindex= _bonePalette.size() ;
paletteindex = _bonePalette.size() ;
_bonePalette.push_back(bmit->second);
}
localid2bone.push_back(paletteindex);
}
OSG_INFO << "RigTransformHardware::buildPalette matrix palette has " << boneNameCountMap.size() << " entries" << std::endl;
for (BoneNameCountMap::iterator it = boneNameCountMap.begin(); it != boneNameCountMap.end(); ++it)
{
OSG_INFO << "RigTransformHardware::buildPalette Bone " << it->first << " is used " << it->second << " times" << std::endl;
}
OSG_INFO << "RigTransformHardware::buildPalette will use " << boneNameCountMap.size() * 4 << " uniforms" << std::endl;
///set paletteindices
for( std::vector<IndexWeightList>::iterator idwlistit=_perVertexInfluences.begin(); idwlistit!=_perVertexInfluences.end(); ++idwlistit)
for( std::vector<IndexWeightList>::iterator idwlistit = _perVertexInfluences.begin(); idwlistit!=_perVertexInfluences.end(); ++idwlistit)
{
for( IndexWeightList::iterator idwit=idwlistit->begin(); idwit!=idwlistit->end();)
for( IndexWeightList::iterator idwit = idwlistit->begin(); idwit!=idwlistit->end();)
{
if(localid2bone[idwit->first]<0)idwit=idwlistit->erase(idwit);
else{
idwit->first=localid2bone[idwit->first];
if(localid2bone[idwit->first]<0)
idwit = idwlistit->erase(idwit);
else
{
idwit->first = localid2bone[idwit->first];
++idwit;
}
}
@@ -277,6 +283,7 @@ bool RigTransformHardware::init(RigGeometry& rig)
osg::Geometry& source = *rig.getSourceGeometry();
osg::Vec3Array* positionSrc = dynamic_cast<osg::Vec3Array*>(source.getVertexArray());
if (!positionSrc)
{
OSG_WARN << "RigTransformHardware no vertex array in the geometry " << rig.getName() << std::endl;
@@ -293,10 +300,10 @@ bool RigTransformHardware::init(RigGeometry& rig)
//grab geom source program and vertex shader if _shader is not setted
if(!_shader.valid() && (program = (osg::Program*)stateset->getAttribute(osg::StateAttribute::PROGRAM)))
{
for(unsigned int i=0; i<program->getNumShaders(); ++i)
if(program->getShader(i)->getType()==osg::Shader::VERTEX)
for(unsigned int i = 0; i<program->getNumShaders(); ++i)
if(program->getShader(i)->getType() == osg::Shader::VERTEX)
{
vertexshader=program->getShader(i);
vertexshader = program->getShader(i);
program->removeShader(vertexshader);
}
}
@@ -310,7 +317,7 @@ bool RigTransformHardware::init(RigGeometry& rig)
{
if (!_shader.valid())
vertexshader = osg::Shader::readShaderFile(osg::Shader::VERTEX,"skinning.vert");
else vertexshader=_shader;
else vertexshader = _shader;
}
if (!vertexshader.valid())

View File

@@ -36,14 +36,14 @@ RigTransformSoftware::RigTransformSoftware(const RigTransformSoftware& rts,const
}
typedef std::vector<RigTransformSoftware::BonePtrWeight> BonePtrWeightList;
void RigTransformSoftware::buildMinimumUpdateSet( const RigGeometry&rig )
{
///1 Create Index2Vec<BoneWeight>
const VertexInfluenceMap &vertexInfluenceMap=*rig.getInfluenceMap();
unsigned int nbVertices=rig.getSourceGeometry()->getVertexArray()->getNumElements();
const VertexInfluenceMap &vertexInfluenceMap = *rig.getInfluenceMap();
std::vector<BonePtrWeightList> perVertexInfluences;
perVertexInfluences.resize(rig.getSourceGeometry()->getVertexArray()->getNumElements());
perVertexInfluences.reserve(nbVertices);
perVertexInfluences.resize(nbVertices);
unsigned int vimapBoneID = 0;
for (osgAnimation::VertexInfluenceMap::const_iterator perBoneinfit = vertexInfluenceMap.begin();
@@ -57,7 +57,7 @@ void RigTransformSoftware::buildMinimumUpdateSet( const RigGeometry&rig )
{
OSG_WARN << "RigTransformSoftware::VertexInfluenceMap contains unamed bone IndexWeightList" << std::endl;
}
for(IndexWeightList::const_iterator infit=inflist.begin(); infit!=inflist.end(); ++infit)
for(IndexWeightList::const_iterator infit = inflist.begin(); infit!=inflist.end(); ++infit)
{
const VertexIndexWeight &iw = *infit;
const unsigned int &index = iw.first;
@@ -67,12 +67,14 @@ void RigTransformSoftware::buildMinimumUpdateSet( const RigGeometry&rig )
}
// normalize _vertex2Bones weight per vertex
unsigned vertexID=0;
for (std::vector<BonePtrWeightList>::iterator it = perVertexInfluences.begin(); it != perVertexInfluences.end(); ++it, ++vertexID)
unsigned vertexID = 0;
for (std::vector<BonePtrWeightList>::iterator it = perVertexInfluences.begin();
it != perVertexInfluences.end();
++it, ++vertexID)
{
BonePtrWeightList& bones = *it;
float sum = 0;
for(BonePtrWeightList::iterator bwit=bones.begin(); bwit!=bones.end(); ++bwit)
for(BonePtrWeightList::iterator bwit = bones.begin(); bwit!=bones.end(); ++bwit)
sum += bwit->getWeight();
if (sum < 1e-4)
{
@@ -81,7 +83,7 @@ void RigTransformSoftware::buildMinimumUpdateSet( const RigGeometry&rig )
else
{
float mult = 1.0/sum;
for(BonePtrWeightList::iterator bwit=bones.begin(); bwit!=bones.end(); ++bwit)
for(BonePtrWeightList::iterator bwit = bones.begin(); bwit != bones.end(); ++bwit)
bwit->setWeight(bwit->getWeight() * mult);
}
}
@@ -92,8 +94,10 @@ void RigTransformSoftware::buildMinimumUpdateSet( const RigGeometry&rig )
typedef std::map<BonePtrWeightList, VertexGroup> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
vertexID=0;
for (std::vector<BonePtrWeightList>::iterator perVertinfit = perVertexInfluences.begin(); perVertinfit!=perVertexInfluences.end(); ++perVertinfit,++vertexID)
vertexID = 0;
for (std::vector<BonePtrWeightList>::iterator perVertinfit = perVertexInfluences.begin();
perVertinfit!=perVertexInfluences.end();
++perVertinfit,++vertexID)
{
BonePtrWeightList &boneinfs = *perVertinfit;
// sort the vector to have a consistent key
@@ -127,20 +131,20 @@ bool RigTransformSoftware::prepareData(RigGeometry&rig)
if(!(positionSrc) || positionSrc->empty() )
return false;
if(normalSrc&& normalSrc->size()!=positionSrc->size())
if(normalSrc && normalSrc->size() != positionSrc->size())
return false;
/// setup Vertex and Normal arrays with copy of sources
rig.setVertexArray(new osg::Vec3Array);
osg::Vec3Array* positionDst =new osg::Vec3Array;
osg::Vec3Array* positionDst = new osg::Vec3Array;
rig.setVertexArray(positionDst);
*positionDst=*positionSrc;
*positionDst = *positionSrc;
positionDst->setDataVariance(osg::Object::DYNAMIC);
if(normalSrc)
{
osg::Vec3Array* normalDst =new osg::Vec3Array;
*normalDst=*normalSrc;
osg::Vec3Array* normalDst = new osg::Vec3Array;
*normalDst = *normalSrc;
rig.setNormalArray(normalDst, osg::Array::BIND_PER_VERTEX);
normalDst->setDataVariance(osg::Object::DYNAMIC);
}
@@ -166,7 +170,7 @@ bool RigTransformSoftware::init(RigGeometry&rig)
BoneMapVisitor mapVisitor;
rig.getSkeleton()->accept(mapVisitor);
BoneMap boneMap = mapVisitor.getBoneMap();
VertexInfluenceMap & vertexInfluenceMap= *rig.getInfluenceMap();
VertexInfluenceMap & vertexInfluenceMap = *rig.getInfluenceMap();
///create local bonemap
std::vector<Bone*> localid2bone;
@@ -198,14 +202,14 @@ bool RigTransformSoftware::init(RigGeometry&rig)
}
///fill bone ptr in the _uniqVertexGroupList
for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
for(VertexGroupList::iterator itvg = _uniqVertexGroupList.begin(); itvg != _uniqVertexGroupList.end(); ++itvg)
{
VertexGroup& uniq = *itvg;
for(BonePtrWeightList::iterator bwit= uniq.getBoneWeights().begin(); bwit!=uniq.getBoneWeights().end(); )
for(BonePtrWeightList::iterator bwit= uniq.getBoneWeights().begin(); bwit != uniq.getBoneWeights().end(); )
{
Bone * b=localid2bone[bwit->getBoneID()];
Bone * b = localid2bone[bwit->getBoneID()];
if(!b)
bwit=uniq.getBoneWeights().erase(bwit);
bwit = uniq.getBoneWeights().erase(bwit);
else
bwit++->setBonePtr(b);
}

View File

@@ -29,7 +29,7 @@ struct invweight_ordered
{
if (bw1.second > bw2.second)return true;
if (bw1.second < bw2.second)return false;
return(bw1.first<bw2.first);
return(bw1.first < bw2.first);
}
};
@@ -39,21 +39,23 @@ void VertexInfluenceMap::normalize(unsigned int numvert)
typedef std::pair<float, std::vector<float*> > PerVertWeights;
std::vector<PerVertWeights > localstore;
localstore.resize(numvert);
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
for(VertexInfluenceMap::iterator mapit = this->begin(); mapit != this->end(); ++mapit)
{
IndexWeightList &curvecinf=mapit->second;
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf)
IndexWeightList &curvecinf = mapit->second;
for(IndexWeightList::iterator curinf = curvecinf.begin(); curinf != curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf=*curinf;
localstore[inf.first].first+=inf.second;
VertexIndexWeight& inf = *curinf;
localstore[inf.first].first += inf.second;
localstore[inf.first].second.push_back(&inf.second);
}
}
unsigned int vertid=0;
for(std::vector<PerVertWeights >::iterator itvert=localstore.begin(); itvert!=localstore.end(); ++itvert, ++vertid)
unsigned int vertid = 0;
for(std::vector<PerVertWeights >::iterator itvert = localstore.begin();
itvert != localstore.end();
++itvert, ++vertid)
{
PerVertWeights & weights=*itvert;
PerVertWeights & weights = *itvert;
if(weights.first< 1e-4)
{
OSG_WARN << "VertexInfluenceMap::normalize warning the vertex " <<vertid << " seems to have 0 weight, skip normalize for this vertex" << std::endl;
@@ -61,8 +63,8 @@ void VertexInfluenceMap::normalize(unsigned int numvert)
else
{
float mult = 1.0/weights.first;
for (std::vector<float*>::iterator itf =weights.second.begin(); itf!=weights.second.end(); ++itf)
**itf*=mult;
for (std::vector<float*>::iterator itf = weights.second.begin(); itf != weights.second.end(); ++itf)
**itf *= mult;
}
}
@@ -73,13 +75,13 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert
typedef std::set<BoneWeight, invweight_ordered > BoneWeightOrdered;
std::map<int, BoneWeightOrdered > tempVec2Bones;
for(VertexInfluenceMap::iterator mapit=this->begin(); mapit!=this->end(); ++mapit)
for(VertexInfluenceMap::iterator mapit = this->begin(); mapit != this->end(); ++mapit)
{
const std::string& bonename=mapit->first;
IndexWeightList &curvecinf=mapit->second;
for(IndexWeightList::iterator curinf=curvecinf.begin(); curinf!=curvecinf.end(); ++curinf)
const std::string& bonename = mapit->first;
IndexWeightList &curvecinf = mapit->second;
for(IndexWeightList::iterator curinf = curvecinf.begin(); curinf != curvecinf.end(); ++curinf)
{
VertexIndexWeight& inf=*curinf;
VertexIndexWeight& inf = *curinf;
if( bonename.empty())
{
OSG_WARN << "VertexInfluenceSet::cullInfluenceCountPerVertex warning vertex " << inf.first << " is not assigned to a bone" << std::endl;
@@ -88,22 +90,22 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert
}
}
this->clear();
for( std::map<int,BoneWeightOrdered >::iterator mapit=tempVec2Bones.begin(); mapit!=tempVec2Bones.end(); ++mapit)
for( std::map<int,BoneWeightOrdered >::iterator mapit = tempVec2Bones.begin(); mapit != tempVec2Bones.end(); ++mapit)
{
BoneWeightOrdered& bwset=mapit->second;
unsigned int newsize=numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
float sum=0;
BoneWeightOrdered& bwset = mapit->second;
unsigned int newsize = numbonepervertex<bwset.size()?numbonepervertex:bwset.size();
float sum = 0.0f;
while(bwset.size()>newsize)bwset.erase(*bwset.rbegin());
if(renormalize)
{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
sum+=bwit->second;
if(sum>1e-4)
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit != bwset.end(); ++bwit)
sum += bwit->second;
if(sum > 1e-4)
{
sum=1.0f/sum;
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
sum = 1.0f/sum;
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit != bwset.end(); ++bwit)
{
VertexInfluence & inf= (*this)[bwit->first];
VertexInfluence & inf = (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first, bwit->second*sum));
inf.setName(bwit->first);
}
@@ -111,9 +113,9 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert
}
else
{
for(BoneWeightOrdered::iterator bwit=bwset.begin(); bwit!=bwset.end(); ++bwit)
for(BoneWeightOrdered::iterator bwit = bwset.begin(); bwit != bwset.end(); ++bwit)
{
VertexInfluence & inf= (*this)[bwit->first];
VertexInfluence & inf = (*this)[bwit->first];
inf.push_back(VertexIndexWeight(mapit->first,bwit->second));
inf.setName(bwit->first);
}
@@ -125,16 +127,14 @@ void VertexInfluenceMap::cullInfluenceCountPerVertex(unsigned int numbonepervert
void VertexInfluenceMap::computePerVertexInfluenceList(std::vector<BoneWeightList>& vertex2Bones,unsigned int numvert)const
{
vertex2Bones.resize(numvert);
for (osgAnimation::VertexInfluenceMap::const_iterator it = begin();
it != end();
++it)
for (osgAnimation::VertexInfluenceMap::const_iterator it = begin(); it != end(); ++it)
{
const IndexWeightList& inflist = it->second;
if (it->first.empty())
{
OSG_WARN << "VertexInfluenceMap::computePerVertexInfluenceList contains unamed bone IndexWeightList" << std::endl;
}
for(IndexWeightList::const_iterator infit=inflist.begin(); infit!=inflist.end(); ++infit)
for(IndexWeightList::const_iterator infit = inflist.begin(); infit != inflist.end(); ++infit)
{
const VertexIndexWeight &iw = *infit;
const unsigned int &index = iw.first;
@@ -179,7 +179,7 @@ struct SortByBoneWeightList : public std::less<BoneWeightList>
return false;
}
};
void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>& uniqVertexGroupList, unsigned int numvert)const
void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>& uniqVertexGroupList, unsigned int numvert) const
{
uniqVertexGroupList.clear();
std::vector<BoneWeightList> vertex2Bones;
@@ -187,7 +187,7 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
typedef std::map<BoneWeightList,VertexGroup, SortByBoneWeightList> UnifyBoneGroup;
UnifyBoneGroup unifyBuffer;
unsigned int vertexID=0;
unsigned int vertexID = 0;
for (std::vector<BoneWeightList>::iterator it = vertex2Bones.begin(); it != vertex2Bones.end(); ++it,++vertexID)
{
BoneWeightList &boneweightlist = *it;
@@ -199,7 +199,7 @@ void VertexInfluenceMap::computeMinimalVertexGroupList(std::vector<VertexGroup>&
unifyBuffer[boneweightlist].setBoneWeights(boneweightlist);
unifyBuffer[boneweightlist].vertIDs().push_back(vertexID);
}
if(vertex2Bones.size()==unifyBuffer.size())
if(vertex2Bones.size() == unifyBuffer.size())
{
OSG_WARN << "VertexInfluenceMap::computeMinimalVertexGroupList is useless no duplicate VertexGroup" << std::endl;
}
@@ -255,32 +255,32 @@ void VertexInfluenceMap::removeUnexpressedBones(Skeleton &skel) const
CollectRigVisitor rigvis;
skel.accept(rigvis);
RigList rigs=rigvis.getRigList();
RigList rigs = rigvis.getRigList();
BoneMap boneMap = mapVisitor.getBoneMap();
Bone* child,*par;
for(BoneMap::iterator bmit=boneMap.begin(); bmit!=boneMap.end();)
for(BoneMap::iterator bmit = boneMap.begin(); bmit != boneMap.end();)
{
if( this->find(bmit->first) == this->end())
{
bool isusless=true;
for(RigList::iterator rigit=rigs.begin(); rigit != rigs.end(); ++rigit)
bool isusless = true;
for(RigList::iterator rigit = rigs.begin(); rigit != rigs.end(); ++rigit)
{
if( ((*rigit)->getInfluenceMap()->find(bmit->first) !=(*rigit)->getInfluenceMap()->end()))
if( ((*rigit)->getInfluenceMap()->find(bmit->first) != (*rigit)->getInfluenceMap()->end()))
{
isusless=false;
isusless = false;
break;
}
}
if(!isusless||!(par=bmit->second->getBoneParent()))
if(!isusless || !(par = bmit->second->getBoneParent()))
{
++bmit;
continue;
}
///Bone can be removed
Bone * bone2rm=bmit->second;
for(unsigned int numchild=0; numchild<bone2rm->getNumChildren(); numchild++)
Bone * bone2rm = bmit->second;
for(unsigned int numchild = 0; numchild < bone2rm->getNumChildren(); numchild++)
{
if( (child = dynamic_cast<Bone*>(bone2rm->getChild(numchild))) )
{
@@ -292,7 +292,7 @@ void VertexInfluenceMap::removeUnexpressedBones(Skeleton &skel) const
///rebuild bonemap after bone removal
skel.accept(mapVisitor);
boneMap = mapVisitor.getBoneMap();
bmit=boneMap.begin();
bmit = boneMap.begin();
}
else ++bmit;
}