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

Revision 6941, 9.5 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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