2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgcompositeviewer.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
2005-06-15 18:59:10 +08:00
|
|
|
*/
|
|
|
|
|
2009-02-08 23:56:35 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2005-06-15 18:59:10 +08:00
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osg/Material>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/BlendFunc>
|
|
|
|
#include <osg/Depth>
|
|
|
|
#include <osg/Projection>
|
|
|
|
#include <osg/PolygonOffset>
|
|
|
|
#include <osg/MatrixTransform>
|
2006-11-27 22:52:07 +08:00
|
|
|
#include <osg/Camera>
|
2005-07-20 00:30:55 +08:00
|
|
|
#include <osg/FrontFace>
|
2005-06-15 18:59:10 +08:00
|
|
|
|
|
|
|
#include <osgText/Text>
|
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
2007-01-16 22:13:00 +08:00
|
|
|
#include <osgGA/StateSetManipulator>
|
2007-09-29 19:41:57 +08:00
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
2005-06-15 18:59:10 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
#include <osgViewer/CompositeViewer>
|
2005-06-15 18:59:10 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
#include <osgFX/Scribe>
|
|
|
|
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
|
|
|
// class to handle events with a pick
|
|
|
|
class PickHandler : public osgGA::GUIEventHandler {
|
2009-01-28 17:26:44 +08:00
|
|
|
public:
|
2007-01-17 01:10:15 +08:00
|
|
|
|
|
|
|
PickHandler():
|
|
|
|
_mx(0.0f),
|
|
|
|
_my(0.0f) {}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
~PickHandler() {}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2013-05-04 03:26:27 +08:00
|
|
|
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
|
2007-01-17 01:10:15 +08:00
|
|
|
{
|
|
|
|
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
|
|
|
|
if (!view) return false;
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
switch(ea.getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::PUSH):
|
|
|
|
{
|
|
|
|
_mx = ea.getX();
|
|
|
|
_my = ea.getY();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case(osgGA::GUIEventAdapter::RELEASE):
|
|
|
|
{
|
|
|
|
if (_mx==ea.getX() && _my==ea.getY())
|
|
|
|
{
|
2013-05-04 03:26:27 +08:00
|
|
|
pick(view, ea);
|
2007-01-17 01:10:15 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2013-05-04 03:26:27 +08:00
|
|
|
void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& event)
|
2007-01-17 01:10:15 +08:00
|
|
|
{
|
|
|
|
osg::Node* node = 0;
|
|
|
|
osg::Group* parent = 0;
|
|
|
|
|
|
|
|
osgUtil::LineSegmentIntersector::Intersections intersections;
|
2013-05-04 03:26:27 +08:00
|
|
|
if (view->computeIntersections(event, intersections))
|
2007-01-17 01:10:15 +08:00
|
|
|
{
|
|
|
|
osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
|
|
|
|
osg::NodePath& nodePath = intersection.nodePath;
|
|
|
|
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
|
|
|
|
parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
|
2009-01-28 17:26:44 +08:00
|
|
|
}
|
2007-01-17 01:10:15 +08:00
|
|
|
|
|
|
|
// now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
|
|
|
|
if (parent && node)
|
|
|
|
{
|
|
|
|
osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
|
|
|
|
if (!parentAsScribe)
|
|
|
|
{
|
|
|
|
// node not already picked, so highlight it with an osgFX::Scribe
|
|
|
|
osgFX::Scribe* scribe = new osgFX::Scribe();
|
|
|
|
scribe->addChild(node);
|
|
|
|
parent->replaceChild(node,scribe);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// node already picked so we want to remove scribe to unpick it.
|
|
|
|
osg::Node::ParentList parentList = parentAsScribe->getParents();
|
|
|
|
for(osg::Node::ParentList::iterator itr=parentList.begin();
|
|
|
|
itr!=parentList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
(*itr)->replaceChild(parentAsScribe,node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
float _mx, _my;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-01-16 22:13:00 +08:00
|
|
|
|
2005-06-15 18:59:10 +08:00
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2005-06-15 18:59:10 +08:00
|
|
|
// read the scene from the list of file specified commandline args.
|
2015-10-22 22:14:53 +08:00
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readRefNodeFiles(arguments);
|
2005-06-15 18:59:10 +08:00
|
|
|
|
2009-02-08 23:56:35 +08:00
|
|
|
if (!scene)
|
|
|
|
{
|
|
|
|
std::cout << argv[0] << ": requires filename argument." << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2005-06-15 18:59:10 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
// construct the viewer.
|
2007-10-03 05:24:23 +08:00
|
|
|
osgViewer::CompositeViewer viewer(arguments);
|
2007-09-29 19:41:57 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
if (arguments.read("-1"))
|
2007-01-17 01:10:15 +08:00
|
|
|
{
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:26:44 +08:00
|
|
|
view->setName("Single view");
|
2015-10-22 22:14:53 +08:00
|
|
|
view->setSceneData(osgDB::readRefNodeFile("fountain.osgt"));
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-10-01 03:27:31 +08:00
|
|
|
view->addEventHandler( new osgViewer::StatsHandler );
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
view->setUpViewAcrossAllScreens();
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
viewer.addView(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.read("-2"))
|
|
|
|
{
|
|
|
|
|
|
|
|
// view one
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:31:43 +08:00
|
|
|
view->setName("View one");
|
2007-01-17 01:10:15 +08:00
|
|
|
viewer.addView(view);
|
|
|
|
|
|
|
|
view->setUpViewOnSingleScreen(0);
|
|
|
|
view->setSceneData(scene.get());
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
// add the state manipulator
|
|
|
|
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
|
|
|
|
statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
|
|
|
|
|
|
|
|
view->addEventHandler( statesetManipulator.get() );
|
|
|
|
}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
// view two
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:31:43 +08:00
|
|
|
view->setName("View two");
|
2007-01-17 01:10:15 +08:00
|
|
|
viewer.addView(view);
|
|
|
|
|
|
|
|
view->setUpViewOnSingleScreen(1);
|
|
|
|
view->setSceneData(scene.get());
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-10-01 03:27:31 +08:00
|
|
|
view->addEventHandler( new osgViewer::StatsHandler );
|
2007-09-29 19:41:57 +08:00
|
|
|
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
// add the handler for doing the picking
|
|
|
|
view->addEventHandler(new PickHandler());
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
|
2018-04-04 19:26:53 +08:00
|
|
|
|
|
|
|
if (arguments.read("-4"))
|
|
|
|
{
|
|
|
|
|
|
|
|
// view one
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
|
|
|
view->setName("View one");
|
|
|
|
viewer.addView(view);
|
|
|
|
|
|
|
|
view->setUpViewInWindow(0, 0, 800, 600);
|
|
|
|
view->setSceneData(scene.get());
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
// add the state manipulator
|
|
|
|
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
|
|
|
|
statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
|
|
|
|
|
|
|
|
view->addEventHandler( statesetManipulator.get() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// view two
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
|
|
|
view->setName("View two");
|
|
|
|
viewer.addView(view);
|
|
|
|
|
|
|
|
view->setUpViewInWindow(1000, 0, 800, 600);
|
|
|
|
view->setSceneData(scene.get());
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
view->addEventHandler( new osgViewer::StatsHandler );
|
|
|
|
|
|
|
|
|
|
|
|
// add the handler for doing the picking
|
|
|
|
view->addEventHandler(new PickHandler());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
if (arguments.read("-3") || viewer.getNumViews()==0)
|
2009-01-28 17:26:44 +08:00
|
|
|
{
|
2007-01-16 22:13:00 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
|
2009-01-28 17:26:44 +08:00
|
|
|
if (!wsi)
|
2007-01-16 16:56:33 +08:00
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int width, height;
|
|
|
|
wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
2007-01-17 01:10:15 +08:00
|
|
|
traits->x = 100;
|
|
|
|
traits->y = 100;
|
|
|
|
traits->width = 1000;
|
|
|
|
traits->height = 800;
|
2007-01-16 16:56:33 +08:00
|
|
|
traits->windowDecoration = true;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
if (gc.valid())
|
|
|
|
{
|
|
|
|
osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<<std::endl;
|
|
|
|
|
|
|
|
// need to ensure that the window is cleared make sure that the complete window is set the correct colour
|
|
|
|
// rather than just the parts of the window that are under the camera's viewports
|
|
|
|
gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
|
|
|
|
gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<<std::endl;
|
|
|
|
}
|
|
|
|
|
2007-01-16 22:13:00 +08:00
|
|
|
// view one
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:31:43 +08:00
|
|
|
view->setName("View one");
|
2007-01-17 01:10:15 +08:00
|
|
|
viewer.addView(view);
|
|
|
|
|
2007-01-16 22:13:00 +08:00
|
|
|
view->setSceneData(scene.get());
|
2009-01-28 17:31:43 +08:00
|
|
|
view->getCamera()->setName("Cam one");
|
2007-01-17 01:10:15 +08:00
|
|
|
view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
|
2007-01-16 22:13:00 +08:00
|
|
|
view->getCamera()->setGraphicsContext(gc.get());
|
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
|
|
|
|
// add the state manipulator
|
|
|
|
osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
|
|
|
|
statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
|
|
|
|
|
|
|
|
view->addEventHandler( statesetManipulator.get() );
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-10-01 03:15:32 +08:00
|
|
|
view->addEventHandler( new osgViewer::StatsHandler );
|
|
|
|
view->addEventHandler( new osgViewer::HelpHandler );
|
2007-10-01 03:27:31 +08:00
|
|
|
view->addEventHandler( new osgViewer::WindowSizeHandler );
|
|
|
|
view->addEventHandler( new osgViewer::ThreadingHandler );
|
|
|
|
view->addEventHandler( new osgViewer::RecordCameraPathHandler );
|
2007-01-16 22:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// view two
|
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:31:43 +08:00
|
|
|
view->setName("View two");
|
2007-01-16 22:13:00 +08:00
|
|
|
viewer.addView(view);
|
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
view->setSceneData(scene.get());
|
2009-01-28 17:31:43 +08:00
|
|
|
view->getCamera()->setName("Cam two");
|
2007-01-17 01:10:15 +08:00
|
|
|
view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
|
2007-01-16 22:13:00 +08:00
|
|
|
view->getCamera()->setGraphicsContext(gc.get());
|
2007-01-17 01:10:15 +08:00
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 01:10:15 +08:00
|
|
|
// add the handler for doing the picking
|
|
|
|
view->addEventHandler(new PickHandler());
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-16 22:13:00 +08:00
|
|
|
}
|
|
|
|
|
2007-01-17 03:18:32 +08:00
|
|
|
// view three
|
2007-01-16 22:13:00 +08:00
|
|
|
{
|
|
|
|
osgViewer::View* view = new osgViewer::View;
|
2009-01-28 17:31:43 +08:00
|
|
|
view->setName("View three");
|
2007-01-16 22:13:00 +08:00
|
|
|
viewer.addView(view);
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
view->setSceneData(osgDB::readRefNodeFile("cessnafire.osgt"));
|
2007-01-16 22:13:00 +08:00
|
|
|
|
2009-01-28 17:31:43 +08:00
|
|
|
view->getCamera()->setName("Cam three");
|
2007-01-17 01:10:15 +08:00
|
|
|
view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
|
|
|
|
view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
|
|
|
|
view->getCamera()->setGraphicsContext(gc.get());
|
2007-01-16 22:13:00 +08:00
|
|
|
view->setCameraManipulator(new osgGA::TrackballManipulator);
|
|
|
|
}
|
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
}
|
2007-10-01 03:15:32 +08:00
|
|
|
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-16 16:56:33 +08:00
|
|
|
while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
|
2007-10-03 05:24:23 +08:00
|
|
|
while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
|
|
|
|
while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
|
2009-01-28 17:26:44 +08:00
|
|
|
|
2007-01-17 03:18:32 +08:00
|
|
|
// run the viewer's main frame loop
|
|
|
|
return viewer.run();
|
2005-06-15 18:59:10 +08:00
|
|
|
}
|
2009-02-08 23:56:35 +08:00
|
|
|
|