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

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
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 <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
48class PickHandler : public osgGA::GUIEventHandler {
49public:
50
51    PickHandler():
52        _mx(0.0f),
53        _my(0.0f) {}
54
55    ~PickHandler() {}
56
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.getX(), ea.getY());
75                }
76                break;
77            }
78            default:
79                break;
80        }
81        return false;
82    }
83
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;
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
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    }
124
125    float _mx, _my;
126
127};
128
129
130int main( int argc, char **argv )
131{
132
133    // use an ArgumentParser object to manage the program arguments.
134    osg::ArgumentParser arguments(&argc,argv);
135
136    // read the scene from the list of file specified commandline args.
137    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
138
139    if (!scene)
140    {
141        std::cout << argv[0] << ": requires filename argument." << std::endl;
142        return 1;
143    }
144
145    // construct the viewer.
146    osgViewer::CompositeViewer viewer(arguments);
147
148    if (arguments.read("-1"))
149    {
150        {
151            osgViewer::View* view = new osgViewer::View;
152            view->setName("Single view");
153            view->setSceneData(osgDB::readNodeFile("fountain.osgt"));
154
155            view->addEventHandler( new osgViewer::StatsHandler );
156
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;
169            view->setName("View one");
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        }
182
183        // view two
184        {
185            osgViewer::View* view = new osgViewer::View;
186            view->setName("View two");
187            viewer.addView(view);
188
189            view->setUpViewOnSingleScreen(1);
190            view->setSceneData(scene.get());
191            view->setCameraManipulator(new osgGA::TrackballManipulator);
192
193            view->addEventHandler( new osgViewer::StatsHandler );
194
195
196            // add the handler for doing the picking
197            view->addEventHandler(new PickHandler());
198        }
199    }
200
201
202    if (arguments.read("-3") || viewer.getNumViews()==0)
203    {
204
205        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
206        if (!wsi)
207        {
208            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
209            return 1;
210        }
211
212        unsigned int width, height;
213        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
214
215        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
216        traits->x = 100;
217        traits->y = 100;
218        traits->width = 1000;
219        traits->height = 800;
220        traits->windowDecoration = true;
221        traits->doubleBuffer = true;
222        traits->sharedContext = 0;
223
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;
228
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
239        // view one
240        {
241            osgViewer::View* view = new osgViewer::View;
242            view->setName("View one");
243            viewer.addView(view);
244
245            view->setSceneData(scene.get());
246            view->getCamera()->setName("Cam one");
247            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
248            view->getCamera()->setGraphicsContext(gc.get());
249            view->setCameraManipulator(new osgGA::TrackballManipulator);
250
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() );
256
257            view->addEventHandler( new osgViewer::StatsHandler );
258            view->addEventHandler( new osgViewer::HelpHandler );
259            view->addEventHandler( new osgViewer::WindowSizeHandler );
260            view->addEventHandler( new osgViewer::ThreadingHandler );
261            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
262        }
263
264        // view two
265        {
266            osgViewer::View* view = new osgViewer::View;
267            view->setName("View two");
268            viewer.addView(view);
269
270            view->setSceneData(scene.get());
271            view->getCamera()->setName("Cam two");
272            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
273            view->getCamera()->setGraphicsContext(gc.get());
274            view->setCameraManipulator(new osgGA::TrackballManipulator);
275
276            // add the handler for doing the picking
277            view->addEventHandler(new PickHandler());
278
279        }
280
281        // view three
282        {
283            osgViewer::View* view = new osgViewer::View;
284            view->setName("View three");
285            viewer.addView(view);
286
287            view->setSceneData(osgDB::readNodeFile("cessnafire.osgt"));
288
289            view->getCamera()->setName("Cam three");
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));
292            view->getCamera()->setGraphicsContext(gc.get());
293            view->setCameraManipulator(new osgGA::TrackballManipulator);
294        }
295
296    }
297
298
299    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
300    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
301    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
302
303     // run the viewer's main frame loop
304     return viewer.run();
305}
Note: See TracBrowser for help on using the browser.