1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-03 20:39:40 +00:00

RigGeometry optimization: vector iteration is more cheap than map iteration

This commit is contained in:
Andrei Kortunov 2019-01-08 20:42:08 +04:00
parent 88edc1a120
commit 254f01b89d
3 changed files with 21 additions and 16 deletions

View file

@ -1098,7 +1098,7 @@ namespace NifOsg
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,20 +136,21 @@ 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; mBoneSphereVector.emplace_back(bone, influencePair.second.mBoundSphere);
const BoneInfluence& bi = it->second; const BoneInfluence& bi = influencePair.second;
const std::map<unsigned short, float>& weights = it->second.mWeights; const std::map<unsigned short, float>& weights = influencePair.second.mWeights;
for (std::map<unsigned short, float>::const_iterator weightIt = weights.begin(); weightIt != weights.end(); ++weightIt) 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[weightIt->first];
@ -160,11 +161,14 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv)
} }
} }
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 +205,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 +267,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

@ -36,7 +36,7 @@ namespace SceneUtil
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;