osg::Node源码
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/ #include <osg/Node>
#include <osg/Group>
#include <osg/NodeVisitor>
#include <osg/Notify>
#include <osg/OccluderNode>
#include <osg/Transform>
#include <osg/UserDataContainer> #include <algorithm> using namespace osg; namespace osg
{
/// Helper class for generating NodePathList.
class CollectParentPaths : public NodeVisitor
{
public:
CollectParentPaths(const osg::Node* haltTraversalAtNode=) :
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_PARENTS),
_haltTraversalAtNode(haltTraversalAtNode)
{
setNodeMaskOverride(0xffffffff);
} virtual void apply(osg::Node& node)
{
if (node.getNumParents()== || &node==_haltTraversalAtNode)
{
_nodePaths.push_back(getNodePath());
}
else
{
traverse(node);
}
} const Node* _haltTraversalAtNode;
NodePath _nodePath;
NodePathList _nodePaths;
};
} Node::Node()
:Object(true)
{
_boundingSphereComputed = false;
_nodeMask = 0xffffffff; _numChildrenRequiringUpdateTraversal = ; _numChildrenRequiringEventTraversal = ; _cullingActive = true;
_numChildrenWithCullingDisabled = ; _numChildrenWithOccluderNodes = ;
} Node::Node(const Node& node,const CopyOp& copyop):
Object(node,copyop),
_initialBound(node._initialBound),
_boundingSphere(node._boundingSphere),
_boundingSphereComputed(node._boundingSphereComputed),
_parents(), // leave empty as parentList is managed by Group.
_updateCallback(copyop(node._updateCallback.get())),
_numChildrenRequiringUpdateTraversal(), // assume no children yet.
_numChildrenRequiringEventTraversal(), // assume no children yet.
_cullCallback(copyop(node._cullCallback.get())),
_cullingActive(node._cullingActive),
_numChildrenWithCullingDisabled(), // assume no children yet.
_numChildrenWithOccluderNodes(),
_nodeMask(node._nodeMask)
{
setStateSet(copyop(node._stateset.get()));
} Node::~Node()
{
// cleanly detach any associated stateset (include remove parent links)
setStateSet();
} void Node::addParent(osg::Group* parent)
{
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex()); _parents.push_back(parent);
} void Node::removeParent(osg::Group* parent)
{
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex()); ParentList::iterator pitr = std::find(_parents.begin(), _parents.end(), parent);
if (pitr!=_parents.end()) _parents.erase(pitr);
} void Node::accept(NodeVisitor& nv)
{
if (nv.validNodeMask(*this))
{
nv.pushOntoNodePath(this);
nv.apply(*this);
nv.popFromNodePath();
}
} void Node::ascend(NodeVisitor& nv)
{
std::for_each(_parents.begin(),_parents.end(),NodeAcceptOp(nv));
} void Node::setStateSet(osg::StateSet* stateset)
{
// do nothing if nothing changed.
if (_stateset==stateset) return; // track whether we need to account for the need to do a update or event traversal.
int delta_update = ;
int delta_event = ; // remove this node from the current statesets parent list
if (_stateset.valid())
{
_stateset->removeParent(this);
if (_stateset->requiresUpdateTraversal()) --delta_update;
if (_stateset->requiresEventTraversal()) --delta_event;
} // set the stateset.
_stateset = stateset; // add this node to the new stateset to the parent list.
if (_stateset.valid())
{
_stateset->addParent(this);
if (_stateset->requiresUpdateTraversal()) ++delta_update;
if (_stateset->requiresEventTraversal()) ++delta_event;
} if (delta_update!=)
{
setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+delta_update);
} if (delta_event!=)
{
setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()+delta_event);
}
} osg::StateSet* Node::getOrCreateStateSet()
{
if (!_stateset) setStateSet(new StateSet);
return _stateset.get();
} NodePathList Node::getParentalNodePaths(osg::Node* haltTraversalAtNode) const
{
CollectParentPaths cpp(haltTraversalAtNode);
const_cast<Node*>(this)->accept(cpp);
return cpp._nodePaths;
} MatrixList Node::getWorldMatrices(const osg::Node* haltTraversalAtNode) const
{
CollectParentPaths cpp(haltTraversalAtNode);
const_cast<Node*>(this)->accept(cpp); MatrixList matrices; for(NodePathList::iterator itr = cpp._nodePaths.begin();
itr != cpp._nodePaths.end();
++itr)
{
NodePath& nodePath = *itr;
if (nodePath.empty())
{
matrices.push_back(osg::Matrix::identity());
}
else
{
matrices.push_back(osg::computeLocalToWorld(nodePath));
}
} return matrices;
} void Node::setUpdateCallback(Callback* nc)
{
// if no changes just return.
if (_updateCallback==nc) return; // updated callback has been changed, will need to update
// both _updateCallback and possibly the numChildrenRequiringAppTraversal
// if the number of callbacks changes. // update the parents numChildrenRequiringAppTraversal
// note, if _numChildrenRequiringUpdateTraversal!=0 then the
// parents won't be affected by any app callback change,
// so no need to inform them.
if (_numChildrenRequiringUpdateTraversal== && !_parents.empty())
{
int delta = ;
if (_updateCallback.valid()) --delta;
if (nc) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringUpdateTraversal(
(*itr)->getNumChildrenRequiringUpdateTraversal()+delta );
} }
} // set the app callback itself.
_updateCallback = nc; } void Node::setNumChildrenRequiringUpdateTraversal(unsigned int num)
{
// if no changes just return.
if (_numChildrenRequiringUpdateTraversal==num) return; // note, if _updateCallback is set then the
// parents won't be affected by any changes to
// _numChildrenRequiringUpdateTraversal so no need to inform them.
if (!_updateCallback && !_parents.empty())
{ // need to pass on changes to parents.
int delta = ;
if (_numChildrenRequiringUpdateTraversal>) --delta;
if (num>) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringUpdateTraversal(
(*itr)->getNumChildrenRequiringUpdateTraversal()+delta
);
} }
} // finally update this objects value.
_numChildrenRequiringUpdateTraversal=num; } void Node::setEventCallback(Callback* nc)
{
// if no changes just return.
if (_eventCallback==nc) return; // event callback has been changed, will need to Event
// both _EventCallback and possibly the numChildrenRequiringAppTraversal
// if the number of callbacks changes. // Event the parents numChildrenRequiringAppTraversal
// note, if _numChildrenRequiringEventTraversal!=0 then the
// parents won't be affected by any app callback change,
// so no need to inform them.
if (_numChildrenRequiringEventTraversal== && !_parents.empty())
{
int delta = ;
if (_eventCallback.valid()) --delta;
if (nc) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal(
(*itr)->getNumChildrenRequiringEventTraversal()+delta );
} }
} // set the app callback itself.
_eventCallback = nc; } void Node::setNumChildrenRequiringEventTraversal(unsigned int num)
{
// if no changes just return.
if (_numChildrenRequiringEventTraversal==num) return; // note, if _EventCallback is set then the
// parents won't be affected by any changes to
// _numChildrenRequiringEventTraversal so no need to inform them.
if (!_eventCallback && !_parents.empty())
{ // need to pass on changes to parents.
int delta = ;
if (_numChildrenRequiringEventTraversal>) --delta;
if (num>) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal(
(*itr)->getNumChildrenRequiringEventTraversal()+delta
);
} }
} // finally Event this objects value.
_numChildrenRequiringEventTraversal=num; } void Node::setCullingActive(bool active)
{
// if no changes just return.
if (_cullingActive == active) return; // culling active has been changed, will need to update
// both _cullActive and possibly the parents numChildrenWithCullingDisabled
// if culling disabled changes. // update the parents _numChildrenWithCullingDisabled
// note, if _numChildrenWithCullingDisabled!=0 then the
// parents won't be affected by any app callback change,
// so no need to inform them.
if (_numChildrenWithCullingDisabled== && !_parents.empty())
{
int delta = ;
if (!_cullingActive) --delta;
if (!active) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenWithCullingDisabled(
(*itr)->getNumChildrenWithCullingDisabled()+delta );
} }
} // set the cullingActive itself.
_cullingActive = active;
} void Node::setNumChildrenWithCullingDisabled(unsigned int num)
{
// if no changes just return.
if (_numChildrenWithCullingDisabled==num) return; // note, if _cullingActive is false then the
// parents won't be affected by any changes to
// _numChildrenWithCullingDisabled so no need to inform them.
if (_cullingActive && !_parents.empty())
{ // need to pass on changes to parents.
int delta = ;
if (_numChildrenWithCullingDisabled>) --delta;
if (num>) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenWithCullingDisabled(
(*itr)->getNumChildrenWithCullingDisabled()+delta
);
} }
} // finally update this objects value.
_numChildrenWithCullingDisabled=num;
} void Node::setNumChildrenWithOccluderNodes(unsigned int num)
{
// if no changes just return.
if (_numChildrenWithOccluderNodes==num) return; // note, if this node is a OccluderNode then the
// parents won't be affected by any changes to
// _numChildrenWithOccluderNodes so no need to inform them.
if (!dynamic_cast<OccluderNode*>(this) && !_parents.empty())
{ // need to pass on changes to parents.
int delta = ;
if (_numChildrenWithOccluderNodes>) --delta;
if (num>) ++delta;
if (delta!=)
{
// the number of callbacks has changed, need to pass this
// on to parents so they know whether app traversal is
// required on this subgraph.
for(ParentList::iterator itr =_parents.begin();
itr != _parents.end();
++itr)
{
(*itr)->setNumChildrenWithOccluderNodes(
(*itr)->getNumChildrenWithOccluderNodes()+delta
);
} }
} // finally update this objects value.
_numChildrenWithOccluderNodes=num; } bool Node::containsOccluderNodes() const
{
return _numChildrenWithOccluderNodes> || dynamic_cast<const OccluderNode*>(this);
} void Node::setDescriptions(const DescriptionList& descriptions)
{
// only assign a description list (and associated UseDataContainer) if we need to.
if (!descriptions.empty() || getUserDataContainer())
{
getOrCreateUserDataContainer()->setDescriptions(descriptions);
}
} Node::DescriptionList& Node::getDescriptions()
{
return getOrCreateUserDataContainer()->getDescriptions();
} static OpenThreads::Mutex s_mutex_StaticDescriptionList;
static const Node::DescriptionList& getStaticDescriptionList()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_mutex_StaticDescriptionList);
static Node::DescriptionList s_descriptionList;
return s_descriptionList;
} const Node::DescriptionList& Node::getDescriptions() const
{
if (_userDataContainer) return _userDataContainer->getDescriptions();
else return getStaticDescriptionList();
} std::string& Node::getDescription(unsigned int i)
{
return getOrCreateUserDataContainer()->getDescriptions()[i];
} const std::string& Node::getDescription(unsigned int i) const
{
if (_userDataContainer) return _userDataContainer->getDescriptions()[i];
else return getStaticDescriptionList()[i];
} unsigned int Node::getNumDescriptions() const
{
return _userDataContainer ? _userDataContainer->getDescriptions().size() : ;
} void Node::addDescription(const std::string& desc)
{
getOrCreateUserDataContainer()->getDescriptions().push_back(desc);
} BoundingSphere Node::computeBound() const
{
return BoundingSphere();
} void Node::dirtyBound()
{
if (_boundingSphereComputed)
{
_boundingSphereComputed = false; // dirty parent bounding sphere's to ensure that all are valid.
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->dirtyBound();
} }
} void Node::setThreadSafeRefUnref(bool threadSafe)
{
Object::setThreadSafeRefUnref(threadSafe); if (_stateset.valid()) _stateset->setThreadSafeRefUnref(threadSafe);
if (_updateCallback.valid()) _updateCallback->setThreadSafeRefUnref(threadSafe);
if (_eventCallback.valid()) _eventCallback->setThreadSafeRefUnref(threadSafe);
if (_cullCallback.valid()) _cullCallback->setThreadSafeRefUnref(threadSafe);
} void Node::resizeGLObjectBuffers(unsigned int maxSize)
{
if (_stateset.valid()) _stateset->resizeGLObjectBuffers(maxSize);
if (_updateCallback.valid()) _updateCallback->resizeGLObjectBuffers(maxSize);
if (_eventCallback.valid()) _eventCallback->resizeGLObjectBuffers(maxSize);
if (_cullCallback.valid()) _cullCallback->resizeGLObjectBuffers(maxSize);
} void Node::releaseGLObjects(osg::State* state) const
{
if (_stateset.valid()) _stateset->releaseGLObjects(state);
if (_updateCallback.valid()) _updateCallback->releaseGLObjects(state);
if (_eventCallback.valid()) _eventCallback->releaseGLObjects(state);
if (_cullCallback.valid()) _cullCallback->releaseGLObjects(state);
}
osg::Node源码的更多相关文章
- osg::Group源码
osg::Group源码 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library i ...
- node源码详解(二 )—— 运行机制 、整体流程
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource2 本博客同步在https://cnodejs.o ...
- mac下编译node源码
看过一篇win7 64x下面编译node的文章,链接地址:编译nodejs及其源码研究 下面学习一下在mac下面如何编译node源码. 过程也挺简单. 1.下载源码. > mkdir nodes ...
- This Node源码分析
看军哥博客有Rtos的源码分析,手痒耍宝把自己读的源码笔记分享出来.愿与众君互相讨论学习 namespace ros { namespace names { void init(const M_str ...
- node源码详解(四) —— js代码如何调用C++的函数
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource4 本博客同步在https://cnodejs.o ...
- node源码详解(三)—— js代码在node中的位置,process、require、module、exports的由来
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource3 本博客同步在https://cnodejs.o ...
- node源码详解 (一)
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource1 本博客同步在https://cnodejs.o ...
- 放一个Dynamicinputs corresponding to Dynamicknobs的Node源码
static const char* const CLASS = "AddInputsSol"; static const char* const HELP = "Add ...
- node源码详解(三)
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource3 本博客同步在https://cnodejs.o ...
随机推荐
- 静态Pod
静态Pod kubernetes 除了我们常用的普通Pod外,还有一种特殊的Pod,叫静态Pod. 概念 静态Pod是直接由节点kubelet进程来管理的,不能通过apiserver的master节点 ...
- HTML&CSS基础-文档声明
HTML&CSS基础-文档声明 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML的发展 1993年6月: HTML第一个版本 1995年11月: HTML2.0 ...
- [AI] 切换cuda版本的万金油
1. 环境 ubuntu16.04 GTX1080Ti x 4 nvidia-418 cuda-10.1 pytorch1.0.0 目标:在最新的显卡驱动下,使用不同版本的cuda和深度学习框架来执行 ...
- HDU - 3311: Dig The Wells (斯坦纳树)
题意:给你n个寺庙,m个村庄,p条路,现在你要在这n+m个位置中选出若干个位置打井,每个位置打井的费用会告诉你,同时p条路也有修建费用,现在每个寺庙都住着一个和尚,问你最小的费用让这n个和尚都能喝上水 ...
- Uva11762 Race to 1——有向无环图&&记忆化搜索
题意 给出一个整数 $N$,每次可以在不超过 $N$ 的素数中等概率随机选择一个 $P$,如果 $P$ 是 $N$ 的约数,则把 $N$ 变成 $N/P$,否则 $N$ 不变.问平均情况下需要多少次随 ...
- 使用docker 实现MySQL主从同步/读写分离
1. 利用 docker 实现 mysql 主从同步 / 读写分离 为了保证数据的完整和安全,mysql 设计了主从同步,一个挂掉还可以用另个.最近重构论坛,想来改成主从吧.担心失误,就先拿 dock ...
- java如何判断溢出
public int reverse2(int x) { double ans=0; int flag=1; if(x<0){ flag=-1; } x=x*flag; while(x>0 ...
- 洛谷P3534 [POI2012] STU
题目 二分好题 首先用二分找最小的绝对值差,对于每个a[i]都两个方向扫一遍,先都改成差满足的形式,然后再找a[k]等于0的情况,发现如果a[k]要变成0,则从他到左右两个方向上必会有两个连续的区间也 ...
- BMP文件格式详解(BMP file format)
BMP文件格式,又称为Bitmap(位图),或是DIB(Device-Independent Device,设备无关图),是windows系统中广泛使用的图片文件格式,由于它可以不作任何变换地址保存图 ...
- (10)Go结构体struct
结构体 Go语言中的基础数据类型可以表示一些事物的基本属性,但是当我们想表达一个事物的全部或部分属性时,这时候再用单一的基本数据类型明显就无法满足需求了,Go语言提供了一种自定义数据类型,可以封装多个 ...