Added support for automatically finding CoordinateSystemNode's in the

scene graph and then using them to set up the CoordinateFrame used
by the camera manipulators.
This commit is contained in:
Robert Osfield 2004-05-03 12:04:25 +00:00
parent 04798b4e63
commit b7746ff56e
6 changed files with 100 additions and 27 deletions

View File

@ -85,23 +85,9 @@ int main( int argc, char **argv )
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
#if 0
osg::CoordinateSystemNode* csn = new osg::CoordinateSystemNode;
csn->addChild(loadedModel.get());
csn->setEllipsoidModel(new osg::EllipsoidModel());
osg::NodePath nodepath;
nodepath.push_back(csn);
viewer.setCoordindateSystemNodePath(nodepath);
// set the scene to render
viewer.setSceneData(csn);
#else
// pass the loaded scene graph to the viewer.
viewer.setSceneData(loadedModel.get());
#endif
// create the windows and run the threads.
viewer.realize();

View File

@ -89,7 +89,7 @@ class SG_EXPORT CoordinateSystemNode : public Group
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
CoordinateSystemNode(const CoordinateSystemNode&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osg,CoordinateSystemNode);
META_Node(osg,CoordinateSystemNode);
/** Set the CoordinateSystem reference string, should be stored in OpenGIS Well Know Text form.*/

View File

@ -78,7 +78,11 @@ class OSGPRODUCER_EXPORT OsgCameraGroup : public Producer::CameraGroup
const osg::Node* getTopMostSceneData() const;
/** update internal structures w.r.t updated scene data.*/
virtual void updatedSceneData();
void setDisplaySettings( osg::DisplaySettings *ds ) { _ds = ds; }
osg::DisplaySettings *getDisplaySettings() { return _ds.get(); }
@ -165,7 +169,8 @@ class OSGPRODUCER_EXPORT OsgCameraGroup : public Producer::CameraGroup
protected :
void setUpSceneViewsWithData();
virtual void setUpSceneViewsWithData();
osg::ApplicationUsage* _applicationUsage;

View File

@ -100,9 +100,13 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction
const osg::NodeVisitor* getUpdateVisitor() const { return _updateVisitor.get(); }
void setCoordindateSystemNodePath(const osg::NodePath& nodePath) { _coordinateSystemNodePath = nodePath; }
typedef std::vector< osg::ref_ptr<osg::Node> > RefNodePath;
const osg::NodePath& getCoordindateSystemNodePath() { return _coordinateSystemNodePath; }
void setCoordindateSystemNodePath(const RefNodePath& nodePath) { _coordinateSystemNodePath = nodePath; }
void setCoordindateSystemNodePath(const osg::NodePath& nodePath);
const RefNodePath& getCoordindateSystemNodePath() { return _coordinateSystemNodePath; }
/** Dispatch the cull and draw for each of the Camera's for this frame.*/
virtual void frame();
@ -166,7 +170,9 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
/** update internal structures w.r.t updated scene data.*/
virtual void updatedSceneData();
protected :
@ -183,7 +189,8 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction
osg::ref_ptr<osg::NodeVisitor> _updateVisitor;
osg::NodePath _coordinateSystemNodePath;
RefNodePath _coordinateSystemNodePath;
bool _recordingAnimationPath;
double _recordingStartTime;

View File

@ -166,7 +166,7 @@ void OsgCameraGroup::setSceneData( osg::Node *scene )
_scene_decorator->addChild(scene);
}
setUpSceneViewsWithData();
updatedSceneData();
}
void OsgCameraGroup::setSceneDecorator( osg::Group* decorator)
@ -179,10 +179,15 @@ void OsgCameraGroup::setSceneDecorator( osg::Group* decorator)
{
decorator->addChild(_scene_data.get());
}
setUpSceneViewsWithData();
updatedSceneData();
}
void OsgCameraGroup::updatedSceneData()
{
setUpSceneViewsWithData();
}
void OsgCameraGroup::setUpSceneViewsWithData()
{
for(SceneHandlerList::iterator p = _shvec.begin(); p != _shvec.end(); p++ )

View File

@ -135,6 +135,40 @@ private:
osgUtil::IntersectVisitor::HitList _PIVsegHitList;
};
class CollectedCoordinateSystemNodesVisitor : public osg::NodeVisitor
{
public:
CollectedCoordinateSystemNodesVisitor():
NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
virtual void apply(osg::Node& node)
{
traverse(node);
}
virtual void apply(osg::CoordinateSystemNode& node)
{
if (_pathToCoordinateSystemNode.empty())
{
osg::notify(osg::NOTICE)<<"Found CoordianteSystemNode node"<<std::endl;
osg::notify(osg::NOTICE)<<" CoordinateSystem = "<<node.getCoordinateSystem()<<std::endl;
_pathToCoordinateSystemNode = getNodePath();
}
else
{
osg::notify(osg::NOTICE)<<"Found additional CoordianteSystemNode node, but ignoring"<<std::endl;
osg::notify(osg::NOTICE)<<" CoordinateSystem = "<<node.getCoordinateSystem()<<std::endl;
}
traverse(node);
}
NodePath _pathToCoordinateSystemNode;
};
//////////////////////////////////////////////////////////////////////////////
//
// osgProducer::Viewer implemention
@ -213,6 +247,32 @@ Viewer::~Viewer()
osgDB::Registry::instance()->setDatabasePager(0);
}
void Viewer::setCoordindateSystemNodePath(const osg::NodePath& nodePath)
{
std::copy(nodePath.begin(),
nodePath.end(),
std::back_inserter(_coordinateSystemNodePath));
}
void Viewer::updatedSceneData()
{
OsgCameraGroup::updatedSceneData();
// now search for CoordinateSystemNode's for which we want to track.
osg::Node* subgraph = getTopMostSceneData();
if (subgraph)
{
CollectedCoordinateSystemNodesVisitor ccsnv;
subgraph->accept(ccsnv);
if (!ccsnv._pathToCoordinateSystemNode.empty())
{
setCoordindateSystemNodePath(ccsnv._pathToCoordinateSystemNode);
}
}
}
void Viewer::setKeyboardMouse(Producer::KeyboardMouse* kbm)
{
_kbm = kbm;
@ -492,14 +552,24 @@ void Viewer::update()
{
osg::Matrixd coordinateFrame;
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(_coordinateSystemNodePath.back());
// have to crete a copy of the RefNodePath to create an osg::NodePath
// to allow it to be used along with the computeLocalToWorld call.
osg::NodePath tmpPath;
for(RefNodePath::iterator itr=_coordinateSystemNodePath.begin();
itr!=_coordinateSystemNodePath.end();
++itr)
{
tmpPath.push_back(itr->get());
}
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(_coordinateSystemNodePath.back().get());
if (csn)
{
coordinateFrame = csn->computeLocalCoordinateFrame(_position[0],_position[1],_position[2])* osg::computeLocalToWorld(_coordinateSystemNodePath);
coordinateFrame = csn->computeLocalCoordinateFrame(_position[0],_position[1],_position[2])* osg::computeLocalToWorld(tmpPath);
}
else
{
coordinateFrame = osg::computeLocalToWorld(_coordinateSystemNodePath);
coordinateFrame = osg::computeLocalToWorld(tmpPath);
}
getKeySwitchMatrixManipulator()->setCoordinateFrame(coordinateFrame);