2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2001-09-20 05:19:47 +08:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning( disable : 4786 )
|
|
|
|
#endif
|
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
#include <osgSim/InsertImpostorsVisitor>
|
|
|
|
#include <osgSim/Impostor>
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace osg;
|
2005-05-02 03:48:49 +08:00
|
|
|
using namespace osgSim;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
InsertImpostorsVisitor::InsertImpostorsVisitor()
|
|
|
|
{
|
|
|
|
setTraversalMode(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN);
|
2002-04-10 00:09:19 +08:00
|
|
|
_impostorThresholdRatio = 30.0f;
|
2001-09-20 05:19:47 +08:00
|
|
|
_maximumNumNestedImpostors = 3;
|
|
|
|
_numNestedImpostors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertImpostorsVisitor::reset()
|
|
|
|
{
|
|
|
|
_groupList.clear();
|
|
|
|
_lodList.clear();
|
|
|
|
_numNestedImpostors = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertImpostorsVisitor::apply(Node& node)
|
|
|
|
{
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertImpostorsVisitor::apply(Group& node)
|
|
|
|
{
|
|
|
|
_groupList.push_back(&node);
|
|
|
|
|
|
|
|
++_numNestedImpostors;
|
|
|
|
if (_numNestedImpostors<_maximumNumNestedImpostors)
|
|
|
|
{
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
--_numNestedImpostors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InsertImpostorsVisitor::apply(LOD& node)
|
|
|
|
{
|
2005-05-02 03:48:49 +08:00
|
|
|
if (dynamic_cast<osgSim::Impostor*>(&node)==0)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
2005-05-02 03:48:49 +08:00
|
|
|
_lodList.push_back(&node);
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
2005-05-02 03:48:49 +08:00
|
|
|
|
2001-09-20 05:19:47 +08:00
|
|
|
++_numNestedImpostors;
|
|
|
|
if (_numNestedImpostors<_maximumNumNestedImpostors)
|
|
|
|
{
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
--_numNestedImpostors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert the required impostors into the scene graph.*/
|
|
|
|
void InsertImpostorsVisitor::insertImpostors()
|
|
|
|
{
|
|
|
|
|
|
|
|
bool _insertImpostorsAboveGroups = true;
|
|
|
|
bool _replaceLODsByImpostors = true;
|
|
|
|
|
|
|
|
// handle group's
|
|
|
|
if (_insertImpostorsAboveGroups)
|
|
|
|
{
|
|
|
|
std::sort(_groupList.begin(),_groupList.end());
|
|
|
|
|
|
|
|
Group* previousGroup = NULL;
|
|
|
|
for(GroupList::iterator itr=_groupList.begin();
|
|
|
|
itr!=_groupList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
Group* group = (*itr);
|
|
|
|
if (group!=previousGroup)
|
|
|
|
{
|
|
|
|
const BoundingSphere& bs = group->getBound();
|
2002-07-11 22:32:21 +08:00
|
|
|
if (bs.valid())
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// take a copy of the original parent list
|
|
|
|
// before we change it around by adding the group
|
|
|
|
// to an impostor.
|
|
|
|
Node::ParentList parentList = group->getParents();
|
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
Impostor* impostor = new Impostor;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// standard LOD settings
|
|
|
|
impostor->addChild(group);
|
2002-10-07 04:33:13 +08:00
|
|
|
impostor->setRange(0,0.0f,1e7f);
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// impostor specfic settings.
|
|
|
|
impostor->setImpostorThresholdToBound(_impostorThresholdRatio);
|
|
|
|
|
|
|
|
// now replace the group by the new impostor in all of the
|
|
|
|
// group's original parent list.
|
|
|
|
for(Node::ParentList::iterator pitr=parentList.begin();
|
|
|
|
pitr!=parentList.end();
|
|
|
|
++pitr)
|
|
|
|
{
|
|
|
|
(*pitr)->replaceChild(group,impostor);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// handle LOD's
|
|
|
|
if (_replaceLODsByImpostors)
|
|
|
|
{
|
|
|
|
std::sort(_lodList.begin(),_lodList.end());
|
|
|
|
|
|
|
|
LOD* previousLOD = NULL;
|
|
|
|
for(LODList::iterator itr=_lodList.begin();
|
|
|
|
itr!=_lodList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::LOD* lod = (*itr);
|
|
|
|
if (lod!=previousLOD)
|
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = lod->getBound();
|
2002-07-11 22:32:21 +08:00
|
|
|
if (bs.valid())
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// take a copy of the original parent list
|
|
|
|
// before we change it around by adding the lod
|
|
|
|
// to an impostor.
|
2005-05-02 03:48:49 +08:00
|
|
|
osg::Node::ParentList parentList = lod->getParents();
|
2001-09-20 05:19:47 +08:00
|
|
|
|
2005-05-02 03:48:49 +08:00
|
|
|
Impostor* impostor = new Impostor;
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// standard LOD settings
|
2002-04-26 16:16:14 +08:00
|
|
|
for(unsigned int ci=0;ci<lod->getNumChildren();++ci)
|
2001-09-20 05:19:47 +08:00
|
|
|
{
|
|
|
|
impostor->addChild(lod->getChild(ci));
|
2002-10-07 04:33:13 +08:00
|
|
|
impostor->setRange(ci,lod->getMinRange(ci),lod->getMaxRange(ci));
|
2001-09-20 05:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impostor->setCenter(lod->getCenter());
|
2002-10-07 04:33:13 +08:00
|
|
|
impostor->setCenterMode(lod->getCenterMode());
|
2001-09-20 05:19:47 +08:00
|
|
|
|
|
|
|
// impostor specfic settings.
|
|
|
|
impostor->setImpostorThresholdToBound(_impostorThresholdRatio);
|
|
|
|
|
|
|
|
// now replace the lod by the new impostor in all of the
|
|
|
|
// lod's original parent list.
|
|
|
|
for(Node::ParentList::iterator pitr=parentList.begin();
|
|
|
|
pitr!=parentList.end();
|
|
|
|
++pitr)
|
|
|
|
{
|
|
|
|
(*pitr)->replaceChild(lod,impostor);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|