Skip to content

Commit

Permalink
move rig update in update thread
Browse files Browse the repository at this point in the history
  • Loading branch information
juval committed Jul 9, 2019
1 parent 1c545c8 commit 3f618fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
41 changes: 32 additions & 9 deletions components/sceneutil/riggeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,31 @@ bool RigGeometry::initFromParentSkeleton(osg::NodeVisitor* nv)
return true;
}

void RigGeometry::cull(osg::NodeVisitor* nv)
{
void RigGeometry::update(osg::NodeVisitor* nv){

if (!mSkeleton)
{
Log(Debug::Error) << "Error: RigGeometry rendering with no skeleton, should have been initialized by UpdateVisitor";
// try to recover anyway, though rendering is likely to be incorrect.
if (!initFromParentSkeleton(nv))
if (!initFromParentSkeleton(nv))
return;
}

//update next frame geom in order not to interfere with draw thread
mLastFrameMutex.lock();
unsigned int traversalNumber = nv->getTraversalNumber();

if (mLastFrameNumber == traversalNumber || (mLastFrameNumber != 0 && !mSkeleton->getActive()))
{
osg::Geometry& geom = *getGeometry(mLastFrameNumber);
osg::Geometry& geom = *getGeometry(mLastFrameNumber+1);
mLastFrameMutex.unlock();
nv->pushOntoNodePath(&geom);
nv->apply(geom);
nv->popFromNodePath();
return;
}
mLastFrameNumber = traversalNumber;
osg::Geometry& geom = *getGeometry(mLastFrameNumber);
osg::Geometry& geom = *getGeometry(mLastFrameNumber+1);

mSkeleton->updateBoneMatrices(traversalNumber);
mLastFrameMutex.unlock();
mSkeleton->updateBoneMatrices(mLastFrameNumber+1);

// skinning
const osg::Vec3Array* positionSrc = static_cast<osg::Vec3Array*>(mSourceGeometry->getVertexArray());
Expand Down Expand Up @@ -255,6 +257,24 @@ void RigGeometry::cull(osg::NodeVisitor* nv)
nv->popFromNodePath();
}

void RigGeometry::cull(osg::NodeVisitor* nv)
{
if (!mSkeleton)
{
Log(Debug::Error) << "Error: RigGeometry rendering with no skeleton, should have been initialized by UpdateVisitor";
// try to recover anyway, though rendering is likely to be incorrect.
if (!initFromParentSkeleton(nv))
return;
}

osg::Geometry& geom = *getGeometry(mLastFrameNumber);

nv->pushOntoNodePath(&geom);
nv->apply(geom);
nv->popFromNodePath();

}

void RigGeometry::updateBounds(osg::NodeVisitor *nv)
{
if (!mSkeleton)
Expand Down Expand Up @@ -376,7 +396,10 @@ void RigGeometry::accept(osg::NodeVisitor &nv)
if (nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR)
cull(&nv);
else if (nv.getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
{
update(&nv);
updateBounds(&nv);
}
else
nv.apply(*this);

Expand Down
2 changes: 2 additions & 0 deletions components/sceneutil/riggeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace SceneUtil

private:
void cull(osg::NodeVisitor* nv);
void update(osg::NodeVisitor* nv);
void updateBounds(osg::NodeVisitor* nv);

osg::ref_ptr<osg::Geometry> mGeometry[2];
Expand Down Expand Up @@ -101,6 +102,7 @@ namespace SceneUtil
std::vector<Bone*> mBoneNodesVector;

unsigned int mLastFrameNumber;
OpenThreads::Mutex mLastFrameMutex;
bool mBoundsFirstFrame;

bool initFromParentSkeleton(osg::NodeVisitor* nv);
Expand Down

0 comments on commit 3f618fe

Please sign in to comment.