OpenSceneGraph/src/osg/Group.cpp

403 lines
12 KiB
C++
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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/Group>
#include <osg/BoundingBox>
#include <osg/Transform>
#include <osg/OccluderNode>
#include <osg/Notify>
#include <stdio.h>
#include <math.h>
2001-01-11 00:32:10 +08:00
#include <algorithm>
using namespace osg;
Group::Group()
{
}
Group::Group(const Group& group,const CopyOp& copyop):
Node(group,copyop)
{
for(NodeList::const_iterator itr=group._children.begin();
itr!=group._children.end();
++itr)
{
Node* child = copyop(itr->get());
if (child) addChild(child);
}
}
2001-01-11 00:32:10 +08:00
Group::~Group()
{
// remove reference to this from children's parent lists.
for(NodeList::iterator itr=_children.begin();
itr!=_children.end();
++itr)
2001-01-11 00:32:10 +08:00
{
(*itr)->removeParent(this);
2001-01-11 00:32:10 +08:00
}
}
void Group::traverse(NodeVisitor& nv)
{
for(NodeList::iterator itr=_children.begin();
itr!=_children.end();
++itr)
2001-01-11 00:32:10 +08:00
{
(*itr)->accept(nv);
}
}
bool Group::addChild( Node *child )
2003-07-10 21:11:25 +08:00
{
return Group::insertChild( _children.size(), child );
}
bool Group::insertChild( unsigned int index, Node *child )
2001-01-11 00:32:10 +08:00
{
if (!child) return false;
#if ENSURE_CHILD_IS_UNIQUE
if (containsNode(child))
{
notify(WARN)<<"Adding non unique child to osg::Group, ignoring call"<<std::endl;
return false;
}
#endif
if (child)
2001-01-11 00:32:10 +08:00
{
// note ref_ptr<> automatically handles incrementing child's reference count.
2003-07-10 21:11:25 +08:00
if (index >= _children.size())
{
_children.push_back(child);
}
else
{
_children.insert(_children.begin()+index, child);
}
2001-01-11 00:32:10 +08:00
// register as parent of child.
child->addParent(this);
// tell any subclasses that a child has been inserted so that they can update themselves.
childInserted(index);
2001-01-11 00:32:10 +08:00
dirtyBound();
2001-09-22 10:42:08 +08:00
// could now require app traversal thanks to the new subgraph,
// so need to check and update if required.
if (child->getNumChildrenRequiringUpdateTraversal()>0 ||
child->getUpdateCallback())
2001-09-22 10:42:08 +08:00
{
setNumChildrenRequiringUpdateTraversal(
getNumChildrenRequiringUpdateTraversal()+1
2001-09-22 10:42:08 +08:00
);
}
// could now require app traversal thanks to the new subgraph,
// so need to check and update if required.
if (child->getNumChildrenRequiringEventTraversal()>0 ||
child->getEventCallback())
{
setNumChildrenRequiringEventTraversal(
getNumChildrenRequiringEventTraversal()+1
);
}
// could now require disabling of culling thanks to the new subgraph,
// so need to check and update if required.
if (child->getNumChildrenWithCullingDisabled()>0 ||
!child->getCullingActive())
{
setNumChildrenWithCullingDisabled(
getNumChildrenWithCullingDisabled()+1
);
}
if (child->getNumChildrenWithOccluderNodes()>0 ||
dynamic_cast<osg::OccluderNode*>(child))
{
setNumChildrenWithOccluderNodes(
getNumChildrenWithOccluderNodes()+1
);
}
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
bool Group::removeChild( Node *child )
{
return removeChild(getChildIndex(child));
}
bool Group::removeChild(unsigned int pos,unsigned int numChildrenToRemove)
{
if (pos<_children.size() && numChildrenToRemove>0)
{
unsigned int endOfRemoveRange = pos+numChildrenToRemove;
if (endOfRemoveRange>_children.size())
2001-09-22 10:42:08 +08:00
{
notify(DEBUG_INFO)<<"Warning: Group::removeChild(i,numChildrenToRemove) has been passed an excessive number"<<std::endl;
notify(DEBUG_INFO)<<" of chilren to remove, trimming just to end of child list."<<std::endl;
endOfRemoveRange=_children.size();
2001-09-22 10:42:08 +08:00
}
unsigned int updateCallbackRemoved = 0;
unsigned int eventCallbackRemoved = 0;
unsigned int numChildrenWithCullingDisabledRemoved = 0;
unsigned int numChildrenWithOccludersRemoved = 0;
for(unsigned i=pos;i<endOfRemoveRange;++i)
{
osg::Node* child = _children[i].get();
// remove this Geode from the child parent list.
child->removeParent(this);
if (child->getNumChildrenRequiringUpdateTraversal()>0 || child->getUpdateCallback()) ++updateCallbackRemoved;
if (child->getNumChildrenRequiringEventTraversal()>0 || child->getEventCallback()) ++eventCallbackRemoved;
if (child->getNumChildrenWithCullingDisabled()>0 || !child->getCullingActive()) ++numChildrenWithCullingDisabledRemoved;
if (child->getNumChildrenWithOccluderNodes()>0 || dynamic_cast<osg::OccluderNode*>(child)) ++numChildrenWithOccludersRemoved;
}
_children.erase(_children.begin()+pos,_children.begin()+endOfRemoveRange);
childRemoved(pos,endOfRemoveRange-pos);
if (updateCallbackRemoved)
{
setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()-updateCallbackRemoved);
}
if (eventCallbackRemoved)
{
setNumChildrenRequiringEventTraversal(getNumChildrenRequiringEventTraversal()-eventCallbackRemoved);
}
if (numChildrenWithCullingDisabledRemoved)
{
setNumChildrenWithCullingDisabled(getNumChildrenWithCullingDisabled()-numChildrenWithCullingDisabledRemoved);
}
if (numChildrenWithOccludersRemoved)
{
setNumChildrenWithOccluderNodes(getNumChildrenWithOccluderNodes()-numChildrenWithOccludersRemoved);
}
2001-01-11 00:32:10 +08:00
dirtyBound();
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
2001-01-11 00:32:10 +08:00
bool Group::replaceChild( Node *origNode, Node *newNode )
{
if (newNode==NULL || origNode==newNode) return false;
unsigned int pos = getChildIndex(origNode);
if (pos<_children.size())
{
return setChild(pos,newNode);
}
return false;
}
bool Group::setChild( unsigned int i, Node* newNode )
{
if (i<_children.size() && newNode)
2001-01-11 00:32:10 +08:00
{
ref_ptr<Node> origNode = _children[i];
// first remove for origNode's parent list.
origNode->removeParent(this);
2001-01-11 00:32:10 +08:00
// note ref_ptr<> automatically handles decrementing origNode's reference count,
// and inccrementing newNode's reference count.
_children[i] = newNode;
2001-01-11 00:32:10 +08:00
// register as parent of child.
newNode->addParent(this);
2001-01-11 00:32:10 +08:00
dirtyBound();
// could now require update traversal thanks to the new subgraph,
// so need to check and update if required.
int delta_numChildrenRequiringUpdateTraversal = 0;
if (origNode->getNumChildrenRequiringUpdateTraversal()>0 ||
origNode->getUpdateCallback())
{
--delta_numChildrenRequiringUpdateTraversal;
}
if (newNode->getNumChildrenRequiringUpdateTraversal()>0 ||
newNode->getUpdateCallback())
{
++delta_numChildrenRequiringUpdateTraversal;
}
if (delta_numChildrenRequiringUpdateTraversal!=0)
{
setNumChildrenRequiringUpdateTraversal(
getNumChildrenRequiringUpdateTraversal()+delta_numChildrenRequiringUpdateTraversal
);
}
// could now require event traversal thanks to the new subgraph,
// so need to check and Event if required.
int delta_numChildrenRequiringEventTraversal = 0;
if (origNode->getNumChildrenRequiringEventTraversal()>0 ||
origNode->getEventCallback())
{
--delta_numChildrenRequiringEventTraversal;
}
if (newNode->getNumChildrenRequiringEventTraversal()>0 ||
newNode->getEventCallback())
{
++delta_numChildrenRequiringEventTraversal;
}
if (delta_numChildrenRequiringEventTraversal!=0)
{
setNumChildrenRequiringEventTraversal(
getNumChildrenRequiringEventTraversal()+delta_numChildrenRequiringEventTraversal
);
}
// could now require disabling of culling thanks to the new subgraph,
// so need to check and update if required.
int delta_numChildrenWithCullingDisabled = 0;
if (origNode->getNumChildrenWithCullingDisabled()>0 ||
!origNode->getCullingActive())
{
--delta_numChildrenWithCullingDisabled;
}
if (newNode->getNumChildrenWithCullingDisabled()>0 ||
!newNode->getCullingActive())
{
++delta_numChildrenWithCullingDisabled;
}
if (delta_numChildrenWithCullingDisabled!=0)
{
setNumChildrenWithCullingDisabled(
getNumChildrenWithCullingDisabled()+delta_numChildrenWithCullingDisabled
);
}
// could now require disabling of culling thanks to the new subgraph,
// so need to check and update if required.
int delta_numChildrenWithOccluderNodes = 0;
if (origNode->getNumChildrenWithOccluderNodes()>0 ||
!origNode->getCullingActive())
{
--delta_numChildrenWithOccluderNodes;
}
if (newNode->getNumChildrenWithOccluderNodes()>0 ||
!newNode->getCullingActive())
{
++delta_numChildrenWithOccluderNodes;
}
if (delta_numChildrenWithOccluderNodes!=0)
{
setNumChildrenWithOccluderNodes(
getNumChildrenWithOccluderNodes()+delta_numChildrenWithOccluderNodes
);
}
2001-01-11 00:32:10 +08:00
return true;
}
else return false;
}
bool Group::computeBound() const
2001-01-11 00:32:10 +08:00
{
_bsphere.init();
if (_children.empty())
{
_bsphere_computed = true;
return false;
}
// note, special handling of the case when a child is an Transform,
// such that only Transforms which are relative to their parents coordinates frame (i.e this group)
// are handled, Transform relative to and absolute reference frame are ignored.
2001-01-11 00:32:10 +08:00
BoundingBox bb;
bb.init();
NodeList::const_iterator itr;
2001-01-11 00:32:10 +08:00
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
const osg::Transform* transform = (*itr)->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
bb.expandBy((*itr)->getBound());
}
}
2001-01-11 00:32:10 +08:00
if (!bb.valid())
{
_bsphere_computed = true;
return false;
}
2001-01-11 00:32:10 +08:00
_bsphere._center = bb.center();
_bsphere._radius = 0.0f;
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
const osg::Transform* transform = (*itr)->asTransform();
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_RF)
{
_bsphere.expandRadiusBy((*itr)->getBound());
}
}
2001-01-11 00:32:10 +08:00
_bsphere_computed = true;
2001-01-11 00:32:10 +08:00
return true;
}
void Group::releaseGLObjects(osg::State* state) const
{
Node::releaseGLObjects(state);
for(NodeList::const_iterator itr=_children.begin();
itr!=_children.end();
++itr)
{
(*itr)->releaseGLObjects(state);
}
}