1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-23 18:09:43 +00:00

Merge pull request #2123 from akortunov/skinning

Optimize skinning, part 2
This commit is contained in:
Bret Curtis 2019-01-10 18:56:52 +01:00 committed by GitHub
commit 590b393c8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 25 deletions

View file

@ -1092,13 +1092,12 @@ namespace NifOsg
const std::vector<Nif::NiSkinData::VertWeight> &weights = data->bones[i].weights; const std::vector<Nif::NiSkinData::VertWeight> &weights = data->bones[i].weights;
for(size_t j = 0;j < weights.size();j++) for(size_t j = 0;j < weights.size();j++)
{ {
std::pair<unsigned short, float> indexWeight = std::make_pair(weights[j].vertex, weights[j].weight); influence.mWeights.emplace_back(weights[j].vertex, weights[j].weight);
influence.mWeights.insert(indexWeight);
} }
influence.mInvBindMatrix = data->bones[i].trafo.toMatrix(); influence.mInvBindMatrix = data->bones[i].trafo.toMatrix();
influence.mBoundSphere = osg::BoundingSpheref(data->bones[i].boundSphereCenter, data->bones[i].boundSphereRadius); influence.mBoundSphere = osg::BoundingSpheref(data->bones[i].boundSphereCenter, data->bones[i].boundSphereRadius);
map->mMap.insert(std::make_pair(boneName, influence)); map->mData.emplace_back(boneName, influence);
} }
rig->setInfluenceMap(map); rig->setInfluenceMap(map);

View file

@ -136,35 +136,35 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv)
typedef std::map<unsigned short, std::vector<BoneWeight> > Vertex2BoneMap; typedef std::map<unsigned short, std::vector<BoneWeight> > Vertex2BoneMap;
Vertex2BoneMap vertex2BoneMap; Vertex2BoneMap vertex2BoneMap;
for (std::map<std::string, BoneInfluence>::const_iterator it = mInfluenceMap->mMap.begin(); it != mInfluenceMap->mMap.end(); ++it) mBoneSphereVector.clear();
for (auto& influencePair : mInfluenceMap->mData)
{ {
Bone* bone = mSkeleton->getBone(it->first); Bone* bone = mSkeleton->getBone(influencePair.first);
if (!bone) if (!bone)
{ {
Log(Debug::Error) << "Error: RigGeometry did not find bone " << it->first ; Log(Debug::Error) << "Error: RigGeometry did not find bone " << influencePair.first;
continue; continue;
} }
mBoneSphereMap[bone] = it->second.mBoundSphere; const BoneInfluence& bi = influencePair.second;
mBoneSphereVector.emplace_back(bone, bi.mBoundSphere);
const BoneInfluence& bi = it->second; for (auto& weightPair: bi.mWeights)
const std::map<unsigned short, float>& weights = it->second.mWeights;
for (std::map<unsigned short, float>::const_iterator weightIt = weights.begin(); weightIt != weights.end(); ++weightIt)
{ {
std::vector<BoneWeight>& vec = vertex2BoneMap[weightIt->first]; std::vector<BoneWeight>& vec = vertex2BoneMap[weightPair.first];
BoneWeight b = std::make_pair(std::make_pair(bone, bi.mInvBindMatrix), weightIt->second); vec.emplace_back(std::make_pair(bone, bi.mInvBindMatrix), weightPair.second);
vec.push_back(b);
} }
} }
Bone2VertexMap bone2VertexMap;
for (Vertex2BoneMap::iterator it = vertex2BoneMap.begin(); it != vertex2BoneMap.end(); ++it) for (Vertex2BoneMap::iterator it = vertex2BoneMap.begin(); it != vertex2BoneMap.end(); ++it)
{ {
mBone2VertexMap[it->second].push_back(it->first); bone2VertexMap[it->second].push_back(it->first);
} }
mBone2VertexVector.assign(bone2VertexMap.begin(), bone2VertexMap.end());
return true; return true;
} }
@ -201,7 +201,7 @@ void RigGeometry::cull(osg::NodeVisitor* nv)
osg::Vec3Array* normalDst = static_cast<osg::Vec3Array*>(geom.getNormalArray()); osg::Vec3Array* normalDst = static_cast<osg::Vec3Array*>(geom.getNormalArray());
osg::Vec4Array* tangentDst = static_cast<osg::Vec4Array*>(geom.getTexCoordArray(7)); osg::Vec4Array* tangentDst = static_cast<osg::Vec4Array*>(geom.getTexCoordArray(7));
for (auto &pair : mBone2VertexMap) for (auto &pair : mBone2VertexVector)
{ {
osg::Matrixf resultMat (0, 0, 0, 0, osg::Matrixf resultMat (0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
@ -263,10 +263,10 @@ void RigGeometry::updateBounds(osg::NodeVisitor *nv)
updateGeomToSkelMatrix(nv->getNodePath()); updateGeomToSkelMatrix(nv->getNodePath());
osg::BoundingBox box; osg::BoundingBox box;
for (BoneSphereMap::const_iterator it = mBoneSphereMap.begin(); it != mBoneSphereMap.end(); ++it) for (auto& boundPair : mBoneSphereVector)
{ {
Bone* bone = it->first; Bone* bone = boundPair.first;
osg::BoundingSpheref bs = it->second; osg::BoundingSpheref bs = boundPair.second;
if (mGeomToSkelMatrix) if (mGeomToSkelMatrix)
transformBoundingSphere(bone->mMatrixInSkeletonSpace * (*mGeomToSkelMatrix), bs); transformBoundingSphere(bone->mMatrixInSkeletonSpace * (*mGeomToSkelMatrix), bs);
else else

View file

@ -31,12 +31,12 @@ namespace SceneUtil
osg::Matrixf mInvBindMatrix; osg::Matrixf mInvBindMatrix;
osg::BoundingSpheref mBoundSphere; osg::BoundingSpheref mBoundSphere;
// <vertex index, weight> // <vertex index, weight>
std::map<unsigned short, float> mWeights; std::vector<std::pair<unsigned short, float>> mWeights;
}; };
struct InfluenceMap : public osg::Referenced struct InfluenceMap : public osg::Referenced
{ {
std::map<std::string, BoneInfluence> mMap; std::vector<std::pair<std::string, BoneInfluence>> mData;
}; };
void setInfluenceMap(osg::ref_ptr<InfluenceMap> influenceMap); void setInfluenceMap(osg::ref_ptr<InfluenceMap> influenceMap);
@ -73,12 +73,13 @@ namespace SceneUtil
typedef std::vector<unsigned short> VertexList; typedef std::vector<unsigned short> VertexList;
typedef std::map<std::vector<BoneWeight>, VertexList> Bone2VertexMap; typedef std::map<std::vector<BoneWeight>, VertexList> Bone2VertexMap;
typedef std::vector<std::pair<std::vector<BoneWeight>, VertexList>> Bone2VertexVector;
Bone2VertexMap mBone2VertexMap; Bone2VertexVector mBone2VertexVector;
typedef std::map<Bone*, osg::BoundingSpheref> BoneSphereMap; typedef std::vector<std::pair<Bone*, osg::BoundingSpheref>> BoneSphereVector;
BoneSphereMap mBoneSphereMap; BoneSphereVector mBoneSphereVector;
unsigned int mLastFrameNumber; unsigned int mLastFrameNumber;
bool mBoundsFirstFrame; bool mBoundsFirstFrame;