root/OpenSceneGraph/trunk/examples/osgmultiviewpaging/osgmultiviewpaging.cpp @ 11978

Revision 11978, 7.0 kB (checked in by robert, 4 years ago)

Ran dos2unix to fix line endings

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/TerrainManipulator>
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
47class MyPager : public osgDB::DatabasePager
48{
49public:
50 virtual void updateSceneGraph(const osg::FrameStamp& frameStamp)
51 {
52   if (frameStamp.getFrameNumber() % 60 == 0)
53   {
54     osg::Timer_t start = osg::Timer::instance()->tick();
55     osgDB::DatabasePager::updateSceneGraph(frameStamp);
56     double d = osg::Timer::instance()->delta_m(start, osg::Timer::instance()->tick());
57     std::cout << "DatabasePager update took " << d << " ms. Length of active nodes = " << _activePagedLODList->size() << std::endl;
58   }
59 }
60};
61
62int main( int argc, char **argv )
63{
64
65    // use an ArgumentParser object to manage the program arguments.
66    osg::ArgumentParser arguments(&argc,argv);
67
68    // read the scene from the list of file specified commandline args.
69    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
70
71    if (!scene)
72    {
73        scene = osgDB::readNodeFile("http://www.openscenegraph.org/data/earth_bayarea/earth.ive");
74    }
75
76    if (!scene)
77    {
78        std::cout << argv[0] << ": requires filename argument." << std::endl;
79        return 1;
80    }
81
82    // construct the viewer.
83    osgViewer::CompositeViewer viewer(arguments);
84
85    if (viewer.getNumViews()==0)
86    {
87
88        osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
89        if (!wsi)
90        {
91            osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
92            return 1;
93        }
94
95        unsigned int width, height;
96        wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
97
98        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
99        traits->x = 100;
100        traits->y = 100;
101        traits->width = 1000;
102        traits->height = 800;
103        traits->windowDecoration = true;
104        traits->doubleBuffer = true;
105        traits->sharedContext = 0;
106
107        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
108        if (gc.valid())
109        {
110            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
111
112            // need to ensure that the window is cleared make sure that the complete window is set the correct colour
113            // rather than just the parts of the window that are under the camera's viewports
114            gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
115            gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116        }
117        else
118        {
119            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
120        }
121
122        // view one
123        {
124            osgViewer::View* view = new osgViewer::View;
125            view->setName("View one");
126            viewer.addView(view);
127
128            view->setSceneData(scene.get());
129            view->getCamera()->setName("Cam one");
130            view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2));
131            view->getCamera()->setGraphicsContext(gc.get());
132
133            // add the state manipulator
134            osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator;
135            statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet());
136
137            view->setCameraManipulator(new osgGA::TerrainManipulator);
138            view->addEventHandler( statesetManipulator.get() );
139
140            view->addEventHandler( new osgViewer::StatsHandler );
141            view->addEventHandler( new osgViewer::HelpHandler );
142            view->addEventHandler( new osgViewer::WindowSizeHandler );
143            view->addEventHandler( new osgViewer::ThreadingHandler );
144            view->addEventHandler( new osgViewer::RecordCameraPathHandler );
145        }
146
147        // view two
148        {
149            osgViewer::View* view = new osgViewer::View;
150            view->setName("View two");
151            viewer.addView(view);
152
153            view->setSceneData(scene.get());
154            view->getCamera()->setName("Cam two");
155            view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2));
156            view->getCamera()->setGraphicsContext(gc.get());
157            view->setCameraManipulator(new osgGA::TerrainManipulator);
158
159        }
160
161        // view three
162        {
163            osgViewer::View* view = new osgViewer::View;
164            view->setName("View three");
165            viewer.addView(view);
166
167            view->setSceneData(scene.get());
168
169            view->getCamera()->setName("Cam three");
170            view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0);
171            view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2));
172            view->getCamera()->setGraphicsContext(gc.get());
173            view->setCameraManipulator(new osgGA::TerrainManipulator);
174
175            // attach custom database pager
176            view->setDatabasePager(new MyPager);
177            view->getDatabasePager()->setTargetMaximumNumberOfPageLOD(1);
178        }
179
180    }
181
182
183    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); }
184    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullDrawThreadPerContext); }
185    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::CompositeViewer::CullThreadPerCameraDrawThreadPerContext); }
186
187     // run the viewer's main frame loop
188     return viewer.run();
189}
Note: See TracBrowser for help on using the browser.