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

Revision 7516, 10.0 kB (checked in by robert, 7 years ago)

Added support for CompositeViewer? to WindowSizeHandler?, ThreadingHandler?, RecordCameraPathManipulator? and HelpHandler?.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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.
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>
29#include <osg/Camera>
30#include <osg/FrontFace>
31
32#include <osgText/Text>
33
34#include <osgGA/TrackballManipulator>
35#include <osgGA/FlightManipulator>
36#include <osgGA/StateSetManipulator>
37#include <osgViewer/ViewerEventHandlers>
38
39#include <osgViewer/CompositeViewer>
40
41#include <osgFX/Scribe>
42
43#include <osg/io_utils>
44
45// class to handle events with a pick
46class PickHandler : public osgGA::GUIEventHandler {
47public:
48
49    PickHandler():
50        _mx(0.0f),
51        _my(0.0f) {}
52       
53    ~PickHandler() {}
54   
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;
59       
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    }
81   
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;
94        }       
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    }
122   
123    float _mx, _my;
124
125};
126
127
128int main( int argc, char **argv )
129{
130
131    // use an ArgumentParser object to manage the program arguments.
132    osg::ArgumentParser arguments(&argc,argv);
133   
134    // read the scene from the list of file specified commandline args.
135    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
136
137    if (!scene) return 1;
138
139    // construct the viewer.
140    osgViewer::CompositeViewer viewer;
141   
142   
143
144   
145    if (arguments.read("-1"))
146    {
147        {
148            osgViewer::View* view = new osgViewer::View;
149            view->setSceneData(osgDB::readNodeFile("fountain.osg"));
150           
151            view->addEventHandler( new osgViewer::StatsHandler );
152           
153            view->setUpViewAcrossAllScreens();
154            view->setCameraManipulator(new osgGA::TrackballManipulator);
155            viewer.addView(view);
156        }
157    }
158
159    if (arguments.read("-2"))
160    {
161
162        // view one
163        {
164            osgViewer::View* view = new osgViewer::View;
165            viewer.addView(view);
166
167            view->setUpViewOnSingleScreen(0);
168            view->setSceneData(scene.get());
169            view->setCameraManipulator(new osgGA::TrackballManipulator);
170
171            // add the state manipulator
172            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
173            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
174
175            view->addEventHandler( statesetManipulator.get() );
176        }
177       
178        // view two
179        {
180            osgViewer::View* view = new osgViewer::View;
181            viewer.addView(view);
182
183            view->setUpViewOnSingleScreen(1);
184            view->setSceneData(scene.get());
185            view->setCameraManipulator(new osgGA::TrackballManipulator);
186           
187            view->addEventHandler( new osgViewer::StatsHandler );
188
189           
190            // add the handler for doing the picking
191            view->addEventHandler(new PickHandler());
192        }
193    }
194   
195
196    if (arguments.read("-3") || viewer.getNumViews()==0)
197    {   
198
199        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
200        if (!wsi)
201        {
202            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
203            return 1;
204        }
205
206        unsigned int width, height;
207        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
208
209        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
210        traits->x = 100;
211        traits->y = 100;
212        traits->width = 1000;
213        traits->height = 800;
214        traits->windowDecoration = true;
215        traits->doubleBuffer = true;
216        traits->sharedContext = 0;
217
218        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
219        if (gc.valid())
220        {
221            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
222
223            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
224            // rather than just the parts of the window that are under the camera's viewports
225            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
226            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
227        }
228        else
229        {
230            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
231        }
232
233        // view one
234        {
235            osgViewer::View* view = new osgViewer::View;
236            viewer.addView(view);
237
238            view->setSceneData(scene.get());
239            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
240            view->getCamera()->setGraphicsContext(gc.get());
241            view->setCameraManipulator(new osgGA::TrackballManipulator);
242
243            // add the state manipulator
244            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
245            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
246
247            view->addEventHandler( statesetManipulator.get() );
248           
249            view->addEventHandler( new osgViewer::StatsHandler );
250            view->addEventHandler( new osgViewer::HelpHandler );
251            view->addEventHandler( new osgViewer::WindowSizeHandler );
252            view->addEventHandler( new osgViewer::ThreadingHandler );
253            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
254        }
255
256        // view two
257        {
258            osgViewer::View* view = new osgViewer::View;
259            viewer.addView(view);
260
261            view->setSceneData(scene.get());
262            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
263            view->getCamera()->setGraphicsContext(gc.get());
264            view->setCameraManipulator(new osgGA::TrackballManipulator);
265           
266            // add the handler for doing the picking
267            view->addEventHandler(new PickHandler());
268           
269        }
270
271        // view three
272        {
273            osgViewer::View* view = new osgViewer::View;
274            viewer.addView(view);
275
276            view->setSceneData(osgDB::readNodeFile("cessnafire.osg"));
277
278            view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
279            view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
280            view->getCamera()->setGraphicsContext(gc.get());
281            view->setCameraManipulator(new osgGA::TrackballManipulator);
282        }
283
284    }
285
286   
287    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
288    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::ThreadPerContext); }
289    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::ThreadPerCamera); }
290 
291     // run the viewer's main frame loop
292     return viewer.run();
293}
Note: See TracBrowser for help on using the browser.