root/OpenSceneGraph/trunk/examples/osgcompositeviewer/osgcompositeviewer.cpp @ 9564

Revision 9564, 10.0 kB (checked in by robert, 5 years ago)

From Paul Melis, remove redundent spaces

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[6941]1/* OpenSceneGraph example, osgcompositeviewer.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
[4333]17*/
18
19#include <osgUtil/Optimizer>
20#include <osgDB/ReadFile>
21
22#include <osg/Material>
23#include <osg/Geode>
24#include <osg/BlendFunc>
25#include <osg/Depth>
26#include <osg/Projection>
27#include <osg/PolygonOffset>
28#include <osg/MatrixTransform>
[5757]29#include <osg/Camera>
[4395]30#include <osg/FrontFace>
[4333]31
32#include <osgText/Text>
33
[5991]34#include <osgGA/TrackballManipulator>
35#include <osgGA/FlightManipulator>
[5992]36#include <osgGA/StateSetManipulator>
[7505]37#include <osgViewer/ViewerEventHandlers>
[4333]38
[5991]39#include <osgViewer/CompositeViewer>
[4333]40
[5998]41#include <osgFX/Scribe>
[5992]42
[5998]43#include <osg/io_utils>
44
45// class to handle events with a pick
46class PickHandler : public osgGA::GUIEventHandler {
[9564]47public:
[5998]48
49    PickHandler():
50        _mx(0.0f),
51        _my(0.0f) {}
[9564]52
[5998]53    ~PickHandler() {}
[9564]54
[5998]55    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
56    {
57        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
58        if (!view) return false;
[9564]59
[5998]60        switch(ea.getEventType())
61        {
62            case(osgGA::GUIEventAdapter::PUSH):
63            {
64                _mx = ea.getX();
65                _my = ea.getY();
66                break;
67            }
68            case(osgGA::GUIEventAdapter::RELEASE):
69            {
70                if (_mx==ea.getX() && _my==ea.getY())
71                {
72                    pick(view, ea.getX(), ea.getY());
73                }
74                break;
75            }
76            default:
77                break;
78        }
79        return false;
80    }
[9564]81
[5998]82    void pick(osgViewer::View* view, float x, float y)
83    {
84        osg::Node* node = 0;
85        osg::Group* parent = 0;
86
87        osgUtil::LineSegmentIntersector::Intersections intersections;
88        if (view->computeIntersections(x, y, intersections))
89        {
90            osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
91            osg::NodePath& nodePath = intersection.nodePath;
92            node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
93            parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
[9564]94        }
[5998]95
96        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
97        if (parent && node)
98        {
99
100            osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
101            if (!parentAsScribe)
102            {
103                // node not already picked, so highlight it with an osgFX::Scribe
104                osgFX::Scribe* scribe = new osgFX::Scribe();
105                scribe->addChild(node);
106                parent->replaceChild(node,scribe);
107            }
108            else
109            {
110                // node already picked so we want to remove scribe to unpick it.
111                osg::Node::ParentList parentList = parentAsScribe->getParents();
112                for(osg::Node::ParentList::iterator itr=parentList.begin();
113                    itr!=parentList.end();
114                    ++itr)
115                {
116                    (*itr)->replaceChild(parentAsScribe,node);
117                }
118            }
119        }
120
121    }
[9564]122
[5998]123    float _mx, _my;
124
125};
126
127
[4333]128int main( int argc, char **argv )
129{
130
131    // use an ArgumentParser object to manage the program arguments.
132    osg::ArgumentParser arguments(&argc,argv);
[9564]133
[4333]134    // read the scene from the list of file specified commandline args.
135    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
136
[5991]137    if (!scene) return 1;
138
139    // construct the viewer.
[7536]140    osgViewer::CompositeViewer viewer(arguments);
[7505]141
[9564]142
143
144
[5991]145    if (arguments.read("-1"))
[5998]146    {
147        {
148            osgViewer::View* view = new osgViewer::View;
[9564]149            view->setName("Single view");
[5998]150            view->setSceneData(osgDB::readNodeFile("fountain.osg"));
[9564]151
[7516]152            view->addEventHandler( new osgViewer::StatsHandler );
[9564]153
[5998]154            view->setUpViewAcrossAllScreens();
155            view->setCameraManipulator(new osgGA::TrackballManipulator);
156            viewer.addView(view);
157        }
158    }
159
160    if (arguments.read("-2"))
161    {
162
163        // view one
164        {
165            osgViewer::View* view = new osgViewer::View;
166            viewer.addView(view);
167
168            view->setUpViewOnSingleScreen(0);
169            view->setSceneData(scene.get());
170            view->setCameraManipulator(new osgGA::TrackballManipulator);
171
172            // add the state manipulator
173            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
174            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
175
176            view->addEventHandler( statesetManipulator.get() );
177        }
[9564]178
[5998]179        // view two
180        {
181            osgViewer::View* view = new osgViewer::View;
182            viewer.addView(view);
183
184            view->setUpViewOnSingleScreen(1);
185            view->setSceneData(scene.get());
186            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]187
[7516]188            view->addEventHandler( new osgViewer::StatsHandler );
[7505]189
[9564]190
[5998]191            // add the handler for doing the picking
192            view->addEventHandler(new PickHandler());
193        }
194    }
195
[9564]196
[5998]197    if (arguments.read("-3") || viewer.getNumViews()==0)
[9564]198    {
[5992]199
[5991]200        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
[9564]201        if (!wsi)
[5991]202        {
203            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
204            return 1;
205        }
[4333]206
[5991]207        unsigned int width, height;
208        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
[4660]209
[5991]210        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
[5998]211        traits->x = 100;
212        traits->y = 100;
213        traits->width = 1000;
214        traits->height = 800;
[5991]215        traits->windowDecoration = true;
216        traits->doubleBuffer = true;
217        traits->sharedContext = 0;
[4660]218
[5991]219        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
220        if (gc.valid())
221        {
222            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
[4333]223
[5991]224            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
225            // rather than just the parts of the window that are under the camera's viewports
226            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
227            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
228        }
229        else
230        {
231            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
232        }
233
[5992]234        // view one
235        {
236            osgViewer::View* view = new osgViewer::View;
[5998]237            viewer.addView(view);
238
[5992]239            view->setSceneData(scene.get());
[5998]240            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
[5992]241            view->getCamera()->setGraphicsContext(gc.get());
242            view->setCameraManipulator(new osgGA::TrackballManipulator);
[5991]243
[5992]244            // add the state manipulator
245            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
246            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
247
248            view->addEventHandler( statesetManipulator.get() );
[9564]249
[7515]250            view->addEventHandler( new osgViewer::StatsHandler );
251            view->addEventHandler( new osgViewer::HelpHandler );
[7516]252            view->addEventHandler( new osgViewer::WindowSizeHandler );
253            view->addEventHandler( new osgViewer::ThreadingHandler );
254            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
[5992]255        }
256
257        // view two
258        {
259            osgViewer::View* view = new osgViewer::View;
[5998]260            viewer.addView(view);
261
[5992]262            view->setSceneData(scene.get());
[5998]263            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
[5992]264            view->getCamera()->setGraphicsContext(gc.get());
265            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]266
[5998]267            // add the handler for doing the picking
268            view->addEventHandler(new PickHandler());
[9564]269
[5992]270        }
271
[5999]272        // view three
[5992]273        {
274            osgViewer::View* view = new osgViewer::View;
275            viewer.addView(view);
276
[5998]277            view->setSceneData(osgDB::readNodeFile("cessnafire.osg"));
278
279            view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
280            view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
[5992]281            view->getCamera()->setGraphicsContext(gc.get());
282            view->setCameraManipulator(new osgGA::TrackballManipulator);
283        }
284
285    }
[7515]286
[9564]287
[5991]288    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
[7536]289    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
290    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
[9564]291
[5999]292     // run the viewer's main frame loop
293     return viewer.run();
[4333]294}
Note: See TracBrowser for help on using the browser.