mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-01-26 09:35:28 +00:00
Share RigGeometry node data
This commit is contained in:
parent
9f716b9ed8
commit
60c9806d62
@ -1120,7 +1120,7 @@ namespace NifOsg
|
|||||||
const Nif::NodeList &bones = skin->bones;
|
const Nif::NodeList &bones = skin->bones;
|
||||||
for(size_t i = 0;i < bones.length();i++)
|
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;
|
SceneUtil::RigGeometry::BoneInfluence influence;
|
||||||
const std::vector<Nif::NiSkinData::VertWeight> &weights = data->bones[i].weights;
|
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)
|
: Drawable(copy, copyop)
|
||||||
, mSkeleton(nullptr)
|
, mSkeleton(nullptr)
|
||||||
, mInfluenceMap(copy.mInfluenceMap)
|
, mInfluenceMap(copy.mInfluenceMap)
|
||||||
|
, mBone2VertexVector(copy.mBone2VertexVector)
|
||||||
|
, mBoneSphereVector(copy.mBoneSphereVector)
|
||||||
, mLastFrameNumber(0)
|
, mLastFrameNumber(0)
|
||||||
, mBoundsFirstFrame(true)
|
, mBoundsFirstFrame(true)
|
||||||
{
|
{
|
||||||
@ -134,37 +136,38 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef std::map<unsigned short, std::vector<BoneWeight> > Vertex2BoneMap;
|
mBoneNodesVector.clear();
|
||||||
Vertex2BoneMap vertex2BoneMap;
|
for (auto& bonePair : mBoneSphereVector->mData)
|
||||||
mBoneSphereVector.clear();
|
|
||||||
for (auto& influencePair : mInfluenceMap->mData)
|
|
||||||
{
|
{
|
||||||
Bone* bone = mSkeleton->getBone(influencePair.first);
|
const std::string& boneName = bonePair.first;
|
||||||
|
Bone* bone = mSkeleton->getBone(boneName);
|
||||||
if (!bone)
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const BoneInfluence& bi = influencePair.second;
|
mBoneNodesVector.push_back(bone);
|
||||||
mBoneSphereVector.emplace_back(bone, bi.mBoundSphere);
|
}
|
||||||
|
|
||||||
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,7 +204,8 @@ 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 : mBone2VertexVector)
|
int index = mBoneSphereVector->mData.size();
|
||||||
|
for (auto &pair : mBone2VertexVector->mData)
|
||||||
{
|
{
|
||||||
osg::Matrixf resultMat (0, 0, 0, 0,
|
osg::Matrixf resultMat (0, 0, 0, 0,
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
@ -210,7 +214,12 @@ void RigGeometry::cull(osg::NodeVisitor* nv)
|
|||||||
|
|
||||||
for (auto &weight : pair.first)
|
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)
|
if (mGeomToSkelMatrix)
|
||||||
@ -263,9 +272,15 @@ void RigGeometry::updateBounds(osg::NodeVisitor *nv)
|
|||||||
updateGeomToSkelMatrix(nv->getNodePath());
|
updateGeomToSkelMatrix(nv->getNodePath());
|
||||||
|
|
||||||
osg::BoundingBox box;
|
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;
|
osg::BoundingSpheref bs = boundPair.second;
|
||||||
if (mGeomToSkelMatrix)
|
if (mGeomToSkelMatrix)
|
||||||
transformBoundingSphere(bone->mMatrixInSkeletonSpace * (*mGeomToSkelMatrix), bs);
|
transformBoundingSphere(bone->mMatrixInSkeletonSpace * (*mGeomToSkelMatrix), bs);
|
||||||
@ -313,6 +328,34 @@ void RigGeometry::updateGeomToSkelMatrix(const osg::NodePath& nodePath)
|
|||||||
void RigGeometry::setInfluenceMap(osg::ref_ptr<InfluenceMap> influenceMap)
|
void RigGeometry::setInfluenceMap(osg::ref_ptr<InfluenceMap> influenceMap)
|
||||||
{
|
{
|
||||||
mInfluenceMap = 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)
|
void RigGeometry::accept(osg::NodeVisitor &nv)
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
namespace SceneUtil
|
namespace SceneUtil
|
||||||
{
|
{
|
||||||
|
|
||||||
class Skeleton;
|
class Skeleton;
|
||||||
class Bone;
|
class Bone;
|
||||||
|
|
||||||
@ -66,20 +65,26 @@ namespace SceneUtil
|
|||||||
|
|
||||||
osg::ref_ptr<InfluenceMap> mInfluenceMap;
|
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::pair<BoneBindMatrixPair, float> BoneWeight;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
struct BoneSphereVector : public osg::Referenced
|
||||||
|
{
|
||||||
BoneSphereVector mBoneSphereVector;
|
std::vector<std::pair<std::string, osg::BoundingSpheref>> mData;
|
||||||
|
};
|
||||||
|
osg::ref_ptr<BoneSphereVector> mBoneSphereVector;
|
||||||
|
std::vector<Bone*> mBoneNodesVector;
|
||||||
|
|
||||||
unsigned int mLastFrameNumber;
|
unsigned int mLastFrameNumber;
|
||||||
bool mBoundsFirstFrame;
|
bool mBoundsFirstFrame;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user