|
|
|
@ -5,8 +5,6 @@
|
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
|
|
#include <components/sceneutil/workqueue.hpp>
|
|
|
|
|
|
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
|
|
|
|
|
|
#include "skeleton.hpp"
|
|
|
|
@ -65,8 +63,6 @@ RigGeometry::RigGeometry()
|
|
|
|
|
, mFirstFrame(true)
|
|
|
|
|
, mBoundsFirstFrame(true)
|
|
|
|
|
{
|
|
|
|
|
initWorkQueue();
|
|
|
|
|
|
|
|
|
|
setCullCallback(new UpdateRigGeometry);
|
|
|
|
|
setUpdateCallback(new UpdateRigBounds);
|
|
|
|
|
setSupportsDisplayList(false);
|
|
|
|
@ -79,21 +75,9 @@ RigGeometry::RigGeometry(const RigGeometry ©, const osg::CopyOp ©op)
|
|
|
|
|
, mFirstFrame(copy.mFirstFrame)
|
|
|
|
|
, mBoundsFirstFrame(copy.mBoundsFirstFrame)
|
|
|
|
|
{
|
|
|
|
|
initWorkQueue();
|
|
|
|
|
|
|
|
|
|
setSourceGeometry(copy.mSourceGeometry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::initWorkQueue()
|
|
|
|
|
{
|
|
|
|
|
static int numCpu = OpenThreads::GetNumberOfProcessors();
|
|
|
|
|
if (numCpu > 1)
|
|
|
|
|
{
|
|
|
|
|
static WorkQueue sWorkQueue(1);
|
|
|
|
|
mWorkQueue = &sWorkQueue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::setSourceGeometry(osg::ref_ptr<osg::Geometry> sourceGeometry)
|
|
|
|
|
{
|
|
|
|
|
mSourceGeometry = sourceGeometry;
|
|
|
|
@ -214,31 +198,22 @@ void accummulateMatrix(const osg::Matrixf& invBindMatrix, const osg::Matrixf& ma
|
|
|
|
|
ptrresult[14] += ptr[14] * weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class UpdateSkinningWorkItem : public WorkItem
|
|
|
|
|
void RigGeometry::update(osg::NodeVisitor* nv)
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
UpdateSkinningWorkItem(RigGeometry* rig, const osg::Matrixf& geomToSkelMatrix, RigGeometry::BoneMatrixMap boneMatrices)
|
|
|
|
|
: mRig(rig)
|
|
|
|
|
, mGeomToSkelMatrix(geomToSkelMatrix)
|
|
|
|
|
, mBoneMatrices(boneMatrices)
|
|
|
|
|
if (!mSkeleton)
|
|
|
|
|
{
|
|
|
|
|
if (!initFromParentSkeleton(nv))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void doWork()
|
|
|
|
|
{
|
|
|
|
|
mRig->updateSkinning(mGeomToSkelMatrix, mBoneMatrices);
|
|
|
|
|
if (!mSkeleton->getActive() && !mFirstFrame)
|
|
|
|
|
return;
|
|
|
|
|
mFirstFrame = false;
|
|
|
|
|
|
|
|
|
|
mTicket->signalDone();
|
|
|
|
|
}
|
|
|
|
|
mSkeleton->updateBoneMatrices(nv);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
RigGeometry* mRig;
|
|
|
|
|
osg::Matrixf mGeomToSkelMatrix;
|
|
|
|
|
RigGeometry::BoneMatrixMap mBoneMatrices;
|
|
|
|
|
};
|
|
|
|
|
osg::Matrixf geomToSkel = getGeomToSkelMatrix(nv);
|
|
|
|
|
|
|
|
|
|
void RigGeometry::updateSkinning(const osg::Matrixf& geomToSkel, RigGeometry::BoneMatrixMap boneMatrices)
|
|
|
|
|
{
|
|
|
|
|
// skinning
|
|
|
|
|
osg::Vec3Array* positionSrc = static_cast<osg::Vec3Array*>(mSourceGeometry->getVertexArray());
|
|
|
|
|
osg::Vec3Array* normalSrc = static_cast<osg::Vec3Array*>(mSourceGeometry->getNormalArray());
|
|
|
|
@ -258,7 +233,7 @@ void RigGeometry::updateSkinning(const osg::Matrixf& geomToSkel, RigGeometry::Bo
|
|
|
|
|
Bone* bone = weightIt->first.first;
|
|
|
|
|
const osg::Matrix& invBindMatrix = weightIt->first.second;
|
|
|
|
|
float weight = weightIt->second;
|
|
|
|
|
const osg::Matrixf& boneMatrix = boneMatrices.at(bone);
|
|
|
|
|
const osg::Matrixf& boneMatrix = bone->mMatrixInSkeletonSpace;
|
|
|
|
|
accummulateMatrix(invBindMatrix, boneMatrix, weight, resultMat);
|
|
|
|
|
}
|
|
|
|
|
resultMat = resultMat * geomToSkel;
|
|
|
|
@ -275,45 +250,6 @@ void RigGeometry::updateSkinning(const osg::Matrixf& geomToSkel, RigGeometry::Bo
|
|
|
|
|
normalDst->dirty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::update(osg::NodeVisitor* nv)
|
|
|
|
|
{
|
|
|
|
|
if (!mSkeleton)
|
|
|
|
|
{
|
|
|
|
|
if (!initFromParentSkeleton(nv))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mSkeleton->getActive() && !mFirstFrame)
|
|
|
|
|
return;
|
|
|
|
|
mFirstFrame = false;
|
|
|
|
|
|
|
|
|
|
mSkeleton->updateBoneMatrices(nv);
|
|
|
|
|
|
|
|
|
|
BoneMatrixMap boneMatrices;
|
|
|
|
|
for (BoneSphereMap::const_iterator it = mBoneSphereMap.begin(); it != mBoneSphereMap.end(); ++it)
|
|
|
|
|
boneMatrices[it->first] = it->first->mMatrixInSkeletonSpace;
|
|
|
|
|
|
|
|
|
|
if (mWorkQueue)
|
|
|
|
|
{
|
|
|
|
|
// shouldn't happen, unless the CullCallback was a false positive, i.e. the Drawable's parent wasn't culled, but the Drawable *is* culled
|
|
|
|
|
if (mWorkTicket)
|
|
|
|
|
mWorkTicket->waitTillDone();
|
|
|
|
|
|
|
|
|
|
osg::Matrixf geomToSkel = getGeomToSkelMatrix(nv);
|
|
|
|
|
|
|
|
|
|
// actual skinning update moved to a background thread
|
|
|
|
|
WorkItem* item = new UpdateSkinningWorkItem(this, geomToSkel, boneMatrices);
|
|
|
|
|
// keep the work ticket so we can synchronize in drawImplementation()
|
|
|
|
|
mWorkTicket = item->getTicket();
|
|
|
|
|
|
|
|
|
|
mWorkQueue->addWorkItem(item);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
updateSkinning(getGeomToSkelMatrix(nv), boneMatrices);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::updateBounds(osg::NodeVisitor *nv)
|
|
|
|
|
{
|
|
|
|
|
if (!mSkeleton)
|
|
|
|
@ -343,46 +279,6 @@ void RigGeometry::updateBounds(osg::NodeVisitor *nv)
|
|
|
|
|
getParent(i)->dirtyBound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::drawImplementation(osg::RenderInfo &renderInfo) const
|
|
|
|
|
{
|
|
|
|
|
if (mWorkTicket)
|
|
|
|
|
{
|
|
|
|
|
mWorkTicket->waitTillDone();
|
|
|
|
|
mWorkTicket = NULL;
|
|
|
|
|
}
|
|
|
|
|
osg::Geometry::drawImplementation(renderInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::compileGLObjects(osg::RenderInfo &renderInfo) const
|
|
|
|
|
{
|
|
|
|
|
if (mWorkTicket)
|
|
|
|
|
{
|
|
|
|
|
mWorkTicket->waitTillDone();
|
|
|
|
|
mWorkTicket = NULL;
|
|
|
|
|
}
|
|
|
|
|
osg::Geometry::compileGLObjects(renderInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::accept(osg::PrimitiveFunctor &pf) const
|
|
|
|
|
{
|
|
|
|
|
if (mWorkTicket)
|
|
|
|
|
{
|
|
|
|
|
mWorkTicket->waitTillDone();
|
|
|
|
|
mWorkTicket = NULL;
|
|
|
|
|
}
|
|
|
|
|
osg::Geometry::accept(pf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RigGeometry::accept(osg::PrimitiveIndexFunctor &pf) const
|
|
|
|
|
{
|
|
|
|
|
if (mWorkTicket)
|
|
|
|
|
{
|
|
|
|
|
mWorkTicket->waitTillDone();
|
|
|
|
|
mWorkTicket = NULL;
|
|
|
|
|
}
|
|
|
|
|
osg::Geometry::accept(pf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
osg::Matrixf RigGeometry::getGeomToSkelMatrix(osg::NodeVisitor *nv)
|
|
|
|
|
{
|
|
|
|
|
osg::NodePath path;
|
|
|
|
|