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

Revision 12529, 10.4 kB (checked in by robert, 4 years ago)

Replaced .osg with .osgt file usage

  • 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
[9705]19#include <iostream>
20
[4333]21#include <osgUtil/Optimizer>
22#include <osgDB/ReadFile>
23
24#include <osg/Material>
25#include <osg/Geode>
26#include <osg/BlendFunc>
27#include <osg/Depth>
28#include <osg/Projection>
29#include <osg/PolygonOffset>
30#include <osg/MatrixTransform>
[5757]31#include <osg/Camera>
[4395]32#include <osg/FrontFace>
[4333]33
34#include <osgText/Text>
35
[5991]36#include <osgGA/TrackballManipulator>
37#include <osgGA/FlightManipulator>
[5992]38#include <osgGA/StateSetManipulator>
[7505]39#include <osgViewer/ViewerEventHandlers>
[4333]40
[5991]41#include <osgViewer/CompositeViewer>
[4333]42
[5998]43#include <osgFX/Scribe>
[5992]44
[5998]45#include <osg/io_utils>
46
47// class to handle events with a pick
48class PickHandler : public osgGA::GUIEventHandler {
[9564]49public:
[5998]50
51    PickHandler():
52        _mx(0.0f),
53        _my(0.0f) {}
[9564]54
[5998]55    ~PickHandler() {}
[9564]56
[5998]57    bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
58    {
59        osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
60        if (!view) return false;
[9564]61
[5998]62        switch(ea.getEventType())
63        {
64            case(osgGA::GUIEventAdapter::PUSH):
65            {
66                _mx = ea.getX();
67                _my = ea.getY();
68                break;
69            }
70            case(osgGA::GUIEventAdapter::RELEASE):
71            {
72                if (_mx==ea.getX() && _my==ea.getY())
73                {
74                    pick(view, ea.getX(), ea.getY());
75                }
76                break;
77            }
78            default:
79                break;
80        }
81        return false;
82    }
[9564]83
[5998]84    void pick(osgViewer::View* view, float x, float y)
85    {
86        osg::Node* node = 0;
87        osg::Group* parent = 0;
88
89        osgUtil::LineSegmentIntersector::Intersections intersections;
90        if (view->computeIntersections(x, y, intersections))
91        {
92            osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
93            osg::NodePath& nodePath = intersection.nodePath;
94            node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
95            parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
[9564]96        }
[5998]97
98        // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
99        if (parent && node)
100        {
101
102            osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
103            if (!parentAsScribe)
104            {
105                // node not already picked, so highlight it with an osgFX::Scribe
106                osgFX::Scribe* scribe = new osgFX::Scribe();
107                scribe->addChild(node);
108                parent->replaceChild(node,scribe);
109            }
110            else
111            {
112                // node already picked so we want to remove scribe to unpick it.
113                osg::Node::ParentList parentList = parentAsScribe->getParents();
114                for(osg::Node::ParentList::iterator itr=parentList.begin();
115                    itr!=parentList.end();
116                    ++itr)
117                {
118                    (*itr)->replaceChild(parentAsScribe,node);
119                }
120            }
121        }
122
123    }
[9564]124
[5998]125    float _mx, _my;
126
127};
128
129
[4333]130int main( int argc, char **argv )
131{
132
133    // use an ArgumentParser object to manage the program arguments.
134    osg::ArgumentParser arguments(&argc,argv);
[9564]135
[4333]136    // read the scene from the list of file specified commandline args.
137    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
138
[9705]139    if (!scene)
140    {
141        std::cout << argv[0] << ": requires filename argument." << std::endl;
142        return 1;
143    }
[5991]144
145    // construct the viewer.
[7536]146    osgViewer::CompositeViewer viewer(arguments);
[7505]147
[5991]148    if (arguments.read("-1"))
[5998]149    {
150        {
151            osgViewer::View* view = new osgViewer::View;
[9564]152            view->setName("Single view");
[12529]153            view->setSceneData(osgDB::readNodeFile("fountain.osgt"));
[9564]154
[7516]155            view->addEventHandler( new osgViewer::StatsHandler );
[9564]156
[5998]157            view->setUpViewAcrossAllScreens();
158            view->setCameraManipulator(new osgGA::TrackballManipulator);
159            viewer.addView(view);
160        }
161    }
162
163    if (arguments.read("-2"))
164    {
165
166        // view one
167        {
168            osgViewer::View* view = new osgViewer::View;
[9565]169            view->setName("View one");
[5998]170            viewer.addView(view);
171
172            view->setUpViewOnSingleScreen(0);
173            view->setSceneData(scene.get());
174            view->setCameraManipulator(new osgGA::TrackballManipulator);
175
176            // add the state manipulator
177            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
178            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
179
180            view->addEventHandler( statesetManipulator.get() );
181        }
[9564]182
[5998]183        // view two
184        {
185            osgViewer::View* view = new osgViewer::View;
[9565]186            view->setName("View two");
[5998]187            viewer.addView(view);
188
189            view->setUpViewOnSingleScreen(1);
190            view->setSceneData(scene.get());
191            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]192
[7516]193            view->addEventHandler( new osgViewer::StatsHandler );
[7505]194
[9564]195
[5998]196            // add the handler for doing the picking
197            view->addEventHandler(new PickHandler());
198        }
199    }
200
[9564]201
[5998]202    if (arguments.read("-3") || viewer.getNumViews()==0)
[9564]203    {
[5992]204
[5991]205        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
[9564]206        if (!wsi)
[5991]207        {
208            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
209            return 1;
210        }
[4333]211
[5991]212        unsigned int width, height;
213        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
[4660]214
[5991]215        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
[5998]216        traits->x = 100;
217        traits->y = 100;
218        traits->width = 1000;
219        traits->height = 800;
[5991]220        traits->windowDecoration = true;
221        traits->doubleBuffer = true;
222        traits->sharedContext = 0;
[4660]223
[5991]224        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
225        if (gc.valid())
226        {
227            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
[4333]228
[5991]229            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
230            // rather than just the parts of the window that are under the camera's viewports
231            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
232            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
233        }
234        else
235        {
236            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
237        }
238
[5992]239        // view one
240        {
241            osgViewer::View* view = new osgViewer::View;
[9565]242            view->setName("View one");
[5998]243            viewer.addView(view);
244
[5992]245            view->setSceneData(scene.get());
[9565]246            view->getCamera()->setName("Cam one");
[5998]247            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
[5992]248            view->getCamera()->setGraphicsContext(gc.get());
249            view->setCameraManipulator(new osgGA::TrackballManipulator);
[5991]250
[5992]251            // add the state manipulator
252            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
253            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
254
255            view->addEventHandler( statesetManipulator.get() );
[9564]256
[7515]257            view->addEventHandler( new osgViewer::StatsHandler );
258            view->addEventHandler( new osgViewer::HelpHandler );
[7516]259            view->addEventHandler( new osgViewer::WindowSizeHandler );
260            view->addEventHandler( new osgViewer::ThreadingHandler );
261            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
[5992]262        }
263
264        // view two
265        {
266            osgViewer::View* view = new osgViewer::View;
[9565]267            view->setName("View two");
[5998]268            viewer.addView(view);
269
[5992]270            view->setSceneData(scene.get());
[9565]271            view->getCamera()->setName("Cam two");
[5998]272            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
[5992]273            view->getCamera()->setGraphicsContext(gc.get());
274            view->setCameraManipulator(new osgGA::TrackballManipulator);
[9564]275
[5998]276            // add the handler for doing the picking
277            view->addEventHandler(new PickHandler());
[9564]278
[5992]279        }
280
[5999]281        // view three
[5992]282        {
283            osgViewer::View* view = new osgViewer::View;
[9565]284            view->setName("View three");
[5992]285            viewer.addView(view);
286
[12529]287            view->setSceneData(osgDB::readNodeFile("cessnafire.osgt"));
[5998]288
[9565]289            view->getCamera()->setName("Cam three");
[5998]290            view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
291            view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
[5992]292            view->getCamera()->setGraphicsContext(gc.get());
293            view->setCameraManipulator(new osgGA::TrackballManipulator);
294        }
295
296    }
[7515]297
[9564]298
[5991]299    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
[7536]300    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
301    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
[9564]302
[5999]303     // run the viewer's main frame loop
304     return viewer.run();
[4333]305}
Note: See TracBrowser for help on using the browser.