root/OpenSceneGraph/trunk/examples/osgcamera/osgcamera.cpp @ 10830

Revision 10830, 11.0 kB (checked in by robert, 4 years ago)

Added support for repeating viewer construction

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgcamera.
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 <osgDB/ReadFile>
20#include <osgDB/WriteFile>
21#include <osgViewer/Viewer>
22#include <osgViewer/ViewerEventHandlers>
23#include <osgGA/TrackballManipulator>
24#include <osgGA/FlightManipulator>
25#include <osgGA/AnimationPathManipulator>
26#include <iostream>
27
28class ModelHandler : public osgGA::GUIEventHandler
29{
30public:
31
32    ModelHandler():
33        _position(0) {}
34   
35    typedef std::vector<std::string> Filenames;
36    Filenames _filenames;
37    unsigned int _position;
38   
39    void add(const std::string& filename) { _filenames.push_back(filename); }
40       
41    bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
42    {
43        osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
44        if (!viewer) return false;
45   
46        if (_filenames.empty()) return false;
47   
48        switch(ea.getEventType())
49        {
50            case(osgGA::GUIEventAdapter::KEYUP):
51            {
52                if (ea.getKey()=='l')
53                {                   
54                    osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( _filenames[_position] );
55                    ++_position;
56                    if (_position>=_filenames.size()) _position = 0;
57                   
58                    if (model.valid())
59                    {
60                        viewer->setSceneData(model.get());
61                    }
62                   
63                    return true;
64                }
65            }
66            default: break;
67        }
68       
69        return false;
70    }
71   
72    bool _done;
73};
74
75
76void singleWindowMultipleCameras(osgViewer::Viewer& viewer)
77{
78    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
79    if (!wsi)
80    {
81        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
82        return;
83    }
84   
85    unsigned int width, height;
86    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
87
88    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
89    traits->x = 0;
90    traits->y = 0;
91    traits->width = width;
92    traits->height = height;
93    traits->windowDecoration = true;
94    traits->doubleBuffer = true;
95    traits->sharedContext = 0;
96
97    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
98    if (gc.valid())
99    {
100        osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
101
102        // need to ensure that the window is cleared make sure that the complete window is set the correct colour
103        // rather than just the parts of the window that are under the camera's viewports
104        gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
105        gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
106    }
107    else
108    {
109        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
110    }
111
112    unsigned int numCameras = 2;
113    double aspectRatioScale = 1.0;///(double)numCameras;
114    for(unsigned int i=0; i<numCameras;++i)
115    {
116        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
117        camera->setGraphicsContext(gc.get());
118        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
119        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
120        camera->setDrawBuffer(buffer);
121        camera->setReadBuffer(buffer);
122
123        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
124    }
125}
126
127void multipleWindowMultipleCameras(osgViewer::Viewer& viewer, bool multipleScreens)
128{
129    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
130    if (!wsi)
131    {
132        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
133        return;
134    }
135   
136    unsigned int width, height;
137    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
138
139
140    unsigned int numCameras = 6;
141    double aspectRatioScale = (double)numCameras;
142    double translate_x = double(numCameras)-1;
143    for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0)
144    {
145        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
146        traits->screenNum = multipleScreens ? i / 3 : 0;
147        traits->x = (i*width)/numCameras;
148        traits->y = 0;
149        traits->width = width/numCameras-1;
150        traits->height = height;
151        traits->windowDecoration = true;
152        traits->doubleBuffer = true;
153        traits->sharedContext = 0;
154
155        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
156        if (gc.valid())
157        {
158            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
159        }
160        else
161        {
162            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
163        }
164
165        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
166        camera->setGraphicsContext(gc.get());
167        camera->setViewport(new osg::Viewport(0,0, width/numCameras, height));
168        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
169        camera->setDrawBuffer(buffer);
170        camera->setReadBuffer(buffer);
171
172        viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() );
173    }
174}
175
176int main( int argc, char **argv )
177{
178    // use an ArgumentParser object to manage the program arguments.
179    osg::ArgumentParser arguments(&argc,argv);
180
181    if (argc<2)
182    {
183        std::cout << argv[0] <<": requires filename argument." << std::endl;
184        return 1;
185    }
186
187    unsigned int numRepeats = 2;
188    if (arguments.read("--repeat",numRepeats) || arguments.read("-r",numRepeats) || arguments.read("--repeat") || arguments.read("-r"))
189    {
190        bool sharedModel = arguments.read("--shared");
191        osg::ref_ptr<osg::Node> model;
192        if (sharedModel) model = osgDB::readNodeFiles(arguments);
193
194        osgViewer::Viewer::ThreadingModel threadingModel = osgViewer::Viewer::AutomaticSelection;
195        while (arguments.read("-s")) { threadingModel = osgViewer::Viewer::SingleThreaded; }
196        while (arguments.read("-g")) { threadingModel = osgViewer::Viewer::CullDrawThreadPerContext; }
197        while (arguments.read("-d")) { threadingModel = osgViewer::Viewer::DrawThreadPerContext; }
198        while (arguments.read("-c")) { threadingModel = osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext; }
199
200        for(unsigned int i=0; i<numRepeats; ++i)
201        {
202            osg::notify(osg::NOTICE)<<"+++++++++++++ New viewer ++++++++++++"<<std::endl;
203
204            osgViewer::Viewer viewer;
205
206            viewer.setThreadingModel(threadingModel);
207
208            if (sharedModel) viewer.setSceneData(model.get());
209            else viewer.setSceneData(osgDB::readNodeFiles(arguments));
210
211            viewer.run();
212
213            osg::notify(osg::NOTICE)<<"------------ Viewer ended ----------"<<std::endl<<std::endl;
214        }
215        return 0;
216    }
217
218
219    std::string pathfile;
220    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
221    while (arguments.read("-p",pathfile))
222    {
223        apm = new osgGA::AnimationPathManipulator(pathfile);
224        if (!apm.valid() || !(apm->valid()) )
225        {
226            apm = 0;
227        }
228    }
229
230    osgViewer::Viewer viewer(arguments);
231   
232    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
233    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); }
234    while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); }
235    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); }
236   
237    bool limitNumberOfFrames = false;
238    unsigned int maxFrames = 10;
239    while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; }
240
241    // alternative viewer window setups.
242    while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); }
243    while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer, false); }
244    while (arguments.read("-3")) { multipleWindowMultipleCameras(viewer, true); }
245
246    if (apm.valid()) viewer.setCameraManipulator(apm.get());
247    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
248   
249    viewer.addEventHandler(new osgViewer::StatsHandler);
250    viewer.addEventHandler(new osgViewer::ThreadingHandler);
251
252    std::string configfile;
253    while (arguments.read("--config", configfile))
254    {
255        osg::notify(osg::NOTICE)<<"Trying to read config file "<<configfile<<std::endl;
256        osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(configfile);
257        osgViewer::View* view = dynamic_cast<osgViewer::View*>(object.get());
258        if (view)
259        {
260            osg::notify(osg::NOTICE)<<"Read config file succesfully"<<std::endl;
261        }
262        else
263        {
264            osg::notify(osg::NOTICE)<<"Failed to read config file : "<<configfile<<std::endl;
265            return 1;
266        }
267    }
268
269    while (arguments.read("--write-config", configfile)) { osgDB::writeObjectFile(viewer, configfile); }
270
271
272    if (arguments.read("-m"))
273    {
274        ModelHandler* modelHandler = new ModelHandler;
275        for(int i=1; i<arguments.argc();++i)
276        {
277            modelHandler->add(arguments[i]);
278        }
279
280        viewer.addEventHandler(modelHandler);
281    }
282    else
283    {
284        // load the scene.
285        osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
286
287        if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
288
289        if (!loadedModel)
290        {
291            std::cout << argv[0] <<": No data loaded." << std::endl;
292            return 1;
293        }
294
295        viewer.setSceneData(loadedModel.get());
296    }
297   
298    viewer.realize();
299
300    unsigned int numFrames = 0;
301    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
302    {
303        viewer.frame();
304        ++numFrames;
305    }
306
307    return 0;
308}
Note: See TracBrowser for help on using the browser.