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