2015-04-14 15:29:12 +00:00
|
|
|
#include "statesetupdater.hpp"
|
2015-04-14 13:55:56 +00:00
|
|
|
|
2022-04-04 20:51:23 +00:00
|
|
|
#include <components/stereo/stereomanager.hpp>
|
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
#include <osg/Node>
|
2015-06-07 21:51:54 +00:00
|
|
|
#include <osg/NodeVisitor>
|
2020-05-12 13:37:00 +00:00
|
|
|
#include <osgUtil/CullVisitor>
|
2015-04-14 13:55:56 +00:00
|
|
|
|
|
|
|
namespace SceneUtil
|
|
|
|
{
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
void StateSetUpdater::operator()(osg::Node* node, osg::NodeVisitor* nv)
|
2015-04-14 13:55:56 +00:00
|
|
|
{
|
2020-05-12 13:37:00 +00:00
|
|
|
bool isCullVisitor = nv->getVisitorType() == osg::NodeVisitor::CULL_VISITOR;
|
2021-01-24 09:34:33 +00:00
|
|
|
|
|
|
|
if (isCullVisitor)
|
|
|
|
return applyCull(node, static_cast<osgUtil::CullVisitor*>(nv));
|
|
|
|
else
|
|
|
|
return applyUpdate(node, nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateSetUpdater::applyUpdate(osg::Node* node, osg::NodeVisitor* nv)
|
|
|
|
{
|
|
|
|
if (!mStateSetsUpdate[0])
|
2015-04-14 13:55:56 +00:00
|
|
|
{
|
2021-01-24 09:34:33 +00:00
|
|
|
for (int i = 0; i < 2; ++i)
|
2015-04-14 13:55:56 +00:00
|
|
|
{
|
2021-01-24 09:34:33 +00:00
|
|
|
mStateSetsUpdate[i] = new osg::StateSet(*node->getOrCreateStateSet(),
|
|
|
|
osg::CopyOp::SHALLOW_COPY); // Using SHALLOW_COPY for StateAttributes, if users want to modify it is
|
|
|
|
// their responsibility to set a non-shared one first in setDefaults
|
|
|
|
setDefaults(mStateSetsUpdate[i]);
|
2015-04-14 13:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-24 09:34:33 +00:00
|
|
|
osg::ref_ptr<osg::StateSet> stateset = mStateSetsUpdate[nv->getTraversalNumber() % 2];
|
2015-12-03 22:44:15 +00:00
|
|
|
apply(stateset, nv);
|
2021-01-24 09:34:33 +00:00
|
|
|
node->setStateSet(stateset);
|
2015-04-14 13:55:56 +00:00
|
|
|
traverse(node, nv);
|
2021-01-24 09:34:33 +00:00
|
|
|
}
|
2022-09-22 18:26:05 +00:00
|
|
|
|
2021-01-24 09:34:33 +00:00
|
|
|
void StateSetUpdater::applyCull(osg::Node* node, osgUtil::CullVisitor* cv)
|
|
|
|
{
|
|
|
|
auto stateset = getCvDependentStateset(cv);
|
|
|
|
apply(stateset, cv);
|
2022-09-25 08:31:56 +00:00
|
|
|
auto* sm = &Stereo::Manager::instance();
|
|
|
|
if (sm != nullptr)
|
|
|
|
{
|
|
|
|
if (sm->getEye(cv) == Stereo::Eye::Left)
|
|
|
|
applyLeft(stateset, cv);
|
|
|
|
if (sm->getEye(cv) == Stereo::Eye::Right)
|
|
|
|
applyRight(stateset, cv);
|
|
|
|
}
|
2022-04-04 20:51:23 +00:00
|
|
|
|
2021-01-24 09:34:33 +00:00
|
|
|
cv->pushStateSet(stateset);
|
|
|
|
traverse(node, cv);
|
|
|
|
cv->popStateSet();
|
|
|
|
}
|
2020-05-12 13:37:00 +00:00
|
|
|
|
2021-01-24 09:34:33 +00:00
|
|
|
osg::StateSet* StateSetUpdater::getCvDependentStateset(osgUtil::CullVisitor* cv)
|
|
|
|
{
|
|
|
|
auto it = mStateSetsCull.find(cv);
|
|
|
|
if (it == mStateSetsCull.end())
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet;
|
|
|
|
mStateSetsCull.emplace(cv, stateset);
|
|
|
|
setDefaults(stateset);
|
|
|
|
return stateset;
|
|
|
|
}
|
|
|
|
return it->second;
|
2015-04-14 13:55:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 15:02:44 +00:00
|
|
|
void StateSetUpdater::reset()
|
|
|
|
{
|
2021-01-24 09:34:33 +00:00
|
|
|
mStateSetsUpdate[0] = nullptr;
|
|
|
|
mStateSetsUpdate[1] = nullptr;
|
|
|
|
mStateSetsCull.clear();
|
2015-06-01 15:02:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
StateSetUpdater::StateSetUpdater() {}
|
2015-04-14 14:41:06 +00:00
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
StateSetUpdater::StateSetUpdater(const StateSetUpdater& copy, const osg::CopyOp& copyop)
|
2021-10-05 12:21:12 +00:00
|
|
|
: SceneUtil::NodeCallback<StateSetUpdater>(copy, copyop)
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
void CompositeStateSetUpdater::apply(osg::StateSet* stateset, osg::NodeVisitor* nv)
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < mCtrls.size(); ++i)
|
|
|
|
mCtrls[i]->apply(stateset, nv);
|
|
|
|
}
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
void CompositeStateSetUpdater::setDefaults(osg::StateSet* stateset)
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < mCtrls.size(); ++i)
|
|
|
|
mCtrls[i]->setDefaults(stateset);
|
|
|
|
}
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
CompositeStateSetUpdater::CompositeStateSetUpdater() {}
|
2015-04-14 14:41:06 +00:00
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
CompositeStateSetUpdater::CompositeStateSetUpdater(const CompositeStateSetUpdater& copy, const osg::CopyOp& copyop)
|
|
|
|
: StateSetUpdater(copy, copyop)
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
2015-04-14 15:29:12 +00:00
|
|
|
for (unsigned int i = 0; i < copy.mCtrls.size(); ++i)
|
2020-10-17 08:26:35 +00:00
|
|
|
mCtrls.emplace_back(osg::clone(copy.mCtrls[i].get(), copyop));
|
2015-04-14 14:41:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
unsigned int CompositeStateSetUpdater::getNumControllers()
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
|
|
|
return mCtrls.size();
|
|
|
|
}
|
|
|
|
|
2015-04-14 15:29:12 +00:00
|
|
|
StateSetUpdater* CompositeStateSetUpdater::getController(int i)
|
|
|
|
{
|
|
|
|
return mCtrls[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositeStateSetUpdater::addController(StateSetUpdater* ctrl)
|
2015-04-14 14:41:06 +00:00
|
|
|
{
|
2020-10-17 08:26:35 +00:00
|
|
|
mCtrls.emplace_back(ctrl);
|
2015-04-14 14:41:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 13:55:56 +00:00
|
|
|
}
|