mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 22:23:51 +00:00
Merge pull request #2125 from akortunov/skinning
Share RigGeometry node data
This commit is contained in:
commit
538a2b97e7
3 changed files with 79 additions and 31 deletions
|
@ -1120,7 +1120,7 @@ namespace NifOsg
|
|||
const Nif::NodeList &bones = skin->bones;
|
||||
for(size_t i = 0;i < bones.length();i++)
|
||||
{
|
||||
std::string boneName = bones[i].getPtr()->name;
|
||||
std::string boneName = Misc::StringUtils::lowerCase(bones[i].getPtr()->name);
|
||||
|
||||
SceneUtil::RigGeometry::BoneInfluence influence;
|
||||
const std::vector<Nif::NiSkinData::VertWeight> &weights = data->bones[i].weights;
|
||||
|
|
|
@ -51,6 +51,8 @@ RigGeometry::RigGeometry(const RigGeometry ©, const osg::CopyOp ©op)
|
|||
: Drawable(copy, copyop)
|
||||
, mSkeleton(nullptr)
|
||||
, mInfluenceMap(copy.mInfluenceMap)
|
||||
, mBone2VertexVector(copy.mBone2VertexVector)
|
||||
, mBoneSphereVector(copy.mBoneSphereVector)
|
||||
, mLastFrameNumber(0)
|
||||
, mBoundsFirstFrame(true)
|
||||
{
|
||||
|
@ -134,37 +136,38 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv)
|
|||
return false;
|
||||
}
|
||||
|
||||
typedef std::map<unsigned short, std::vector<BoneWeight> > Vertex2BoneMap;
|
||||
Vertex2BoneMap vertex2BoneMap;
|
||||
mBoneSphereVector.clear();
|
||||
for (auto& influencePair : mInfluenceMap->mData)
|
||||
mBoneNodesVector.clear();
|
||||
for (auto& bonePair : mBoneSphereVector->mData)
|
||||
{
|
||||
Bone* bone = mSkeleton->getBone(influencePair.first);
|
||||
const std::string& boneName = bonePair.first;
|
||||
Bone* bone = mSkeleton->getBone(boneName);
|
||||
if (!bone)
|
||||
{
|
||||
Log(Debug::Error) << "Error: RigGeometry did not find bone " << influencePair.first;
|
||||
mBoneNodesVector.push_back(nullptr);
|
||||
Log(Debug::Error) << "Error: RigGeometry did not find bone " << boneName;
|
||||
continue;
|
||||
}
|
||||
|
||||
const BoneInfluence& bi = influencePair.second;
|
||||
mBoneSphereVector.emplace_back(bone, bi.mBoundSphere);
|
||||
mBoneNodesVector.push_back(bone);
|
||||
}
|
||||
|
||||
for (auto& weightPair: bi.mWeights)
|
||||
for (auto& pair : mBone2VertexVector->mData)
|
||||
{
|
||||
for (auto &weight : pair.first)
|
||||
{
|
||||
std::vector<BoneWeight>& vec = vertex2BoneMap[weightPair.first];
|
||||
const std::string& boneName = weight.first.first;
|
||||
Bone* bone = mSkeleton->getBone(boneName);
|
||||
if (!bone)
|
||||
{
|
||||
mBoneNodesVector.push_back(nullptr);
|
||||
Log(Debug::Error) << "Error: RigGeometry did not find bone " << boneName;
|
||||
continue;
|
||||
}
|
||||
|
||||
vec.emplace_back(std::make_pair(bone, bi.mInvBindMatrix), weightPair.second);
|
||||
mBoneNodesVector.push_back(bone);
|
||||
}
|
||||
}
|
||||
|
||||
Bone2VertexMap bone2VertexMap;
|
||||
for (Vertex2BoneMap::iterator it = vertex2BoneMap.begin(); it != vertex2BoneMap.end(); ++it)
|
||||
{
|
||||
bone2VertexMap[it->second].push_back(it->first);
|
||||
}
|
||||
|
||||
mBone2VertexVector.assign(bone2VertexMap.begin(), bone2VertexMap.end());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -201,7 +204,8 @@ void RigGeometry::cull(osg::NodeVisitor* nv)
|
|||
osg::Vec3Array* normalDst = static_cast<osg::Vec3Array*>(geom.getNormalArray());
|
||||
osg::Vec4Array* tangentDst = static_cast<osg::Vec4Array*>(geom.getTexCoordArray(7));
|
||||
|
||||
for (auto &pair : mBone2VertexVector)
|
||||
int index = mBoneSphereVector->mData.size();
|
||||
for (auto &pair : mBone2VertexVector->mData)
|
||||
{
|
||||
osg::Matrixf resultMat (0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
|
@ -210,7 +214,12 @@ void RigGeometry::cull(osg::NodeVisitor* nv)
|
|||
|
||||
for (auto &weight : pair.first)
|
||||
{
|
||||
accumulateMatrix(weight.first.second, weight.first.first->mMatrixInSkeletonSpace, weight.second, resultMat);
|
||||
Bone* bone = mBoneNodesVector[index];
|
||||
if (bone == nullptr)
|
||||
continue;
|
||||
|
||||
accumulateMatrix(weight.first.second, bone->mMatrixInSkeletonSpace, weight.second, resultMat);
|
||||
index++;
|
||||
}
|
||||
|
||||
if (mGeomToSkelMatrix)
|
||||
|
@ -263,9 +272,15 @@ void RigGeometry::updateBounds(osg::NodeVisitor *nv)
|
|||
updateGeomToSkelMatrix(nv->getNodePath());
|
||||
|
||||
osg::BoundingBox box;
|
||||
for (auto& boundPair : mBoneSphereVector)
|
||||
|
||||
int index = 0;
|
||||
for (auto& boundPair : mBoneSphereVector->mData)
|
||||
{
|
||||
Bone* bone = boundPair.first;
|
||||
Bone* bone = mBoneNodesVector[index];
|
||||
if (bone == nullptr)
|
||||
continue;
|
||||
|
||||
index++;
|
||||
osg::BoundingSpheref bs = boundPair.second;
|
||||
if (mGeomToSkelMatrix)
|
||||
transformBoundingSphere(bone->mMatrixInSkeletonSpace * (*mGeomToSkelMatrix), bs);
|
||||
|
@ -313,6 +328,34 @@ void RigGeometry::updateGeomToSkelMatrix(const osg::NodePath& nodePath)
|
|||
void RigGeometry::setInfluenceMap(osg::ref_ptr<InfluenceMap> influenceMap)
|
||||
{
|
||||
mInfluenceMap = influenceMap;
|
||||
|
||||
typedef std::map<unsigned short, std::vector<BoneWeight> > Vertex2BoneMap;
|
||||
Vertex2BoneMap vertex2BoneMap;
|
||||
mBoneSphereVector = new BoneSphereVector;
|
||||
mBoneSphereVector->mData.reserve(mInfluenceMap->mData.size());
|
||||
mBone2VertexVector = new Bone2VertexVector;
|
||||
for (auto& influencePair : mInfluenceMap->mData)
|
||||
{
|
||||
const std::string& boneName = influencePair.first;
|
||||
const BoneInfluence& bi = influencePair.second;
|
||||
mBoneSphereVector->mData.emplace_back(boneName, bi.mBoundSphere);
|
||||
|
||||
for (auto& weightPair: bi.mWeights)
|
||||
{
|
||||
std::vector<BoneWeight>& vec = vertex2BoneMap[weightPair.first];
|
||||
|
||||
vec.emplace_back(std::make_pair(boneName, bi.mInvBindMatrix), weightPair.second);
|
||||
}
|
||||
}
|
||||
|
||||
Bone2VertexMap bone2VertexMap;
|
||||
for (auto& vertexPair : vertex2BoneMap)
|
||||
{
|
||||
bone2VertexMap[vertexPair.second].emplace_back(vertexPair.first);
|
||||
}
|
||||
|
||||
mBone2VertexVector->mData.reserve(bone2VertexMap.size());
|
||||
mBone2VertexVector->mData.assign(bone2VertexMap.begin(), bone2VertexMap.end());
|
||||
}
|
||||
|
||||
void RigGeometry::accept(osg::NodeVisitor &nv)
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
namespace SceneUtil
|
||||
{
|
||||
|
||||
class Skeleton;
|
||||
class Bone;
|
||||
|
||||
|
@ -66,20 +65,26 @@ namespace SceneUtil
|
|||
|
||||
osg::ref_ptr<InfluenceMap> mInfluenceMap;
|
||||
|
||||
typedef std::pair<Bone*, osg::Matrixf> BoneBindMatrixPair;
|
||||
typedef std::pair<std::string, osg::Matrixf> BoneBindMatrixPair;
|
||||
|
||||
typedef std::pair<BoneBindMatrixPair, float> BoneWeight;
|
||||
|
||||
typedef std::vector<unsigned short> VertexList;
|
||||
|
||||
typedef std::map<std::vector<BoneWeight>, VertexList> Bone2VertexMap;
|
||||
typedef std::vector<std::pair<std::vector<BoneWeight>, VertexList>> Bone2VertexVector;
|
||||
|
||||
Bone2VertexVector mBone2VertexVector;
|
||||
struct Bone2VertexVector : public osg::Referenced
|
||||
{
|
||||
std::vector<std::pair<std::vector<BoneWeight>, VertexList>> mData;
|
||||
};
|
||||
osg::ref_ptr<Bone2VertexVector> mBone2VertexVector;
|
||||
|
||||
typedef std::vector<std::pair<Bone*, osg::BoundingSpheref>> BoneSphereVector;
|
||||
|
||||
BoneSphereVector mBoneSphereVector;
|
||||
struct BoneSphereVector : public osg::Referenced
|
||||
{
|
||||
std::vector<std::pair<std::string, osg::BoundingSpheref>> mData;
|
||||
};
|
||||
osg::ref_ptr<BoneSphereVector> mBoneSphereVector;
|
||||
std::vector<Bone*> mBoneNodesVector;
|
||||
|
||||
unsigned int mLastFrameNumber;
|
||||
bool mBoundsFirstFrame;
|
||||
|
|
Loading…
Reference in a new issue