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

Revision 10831, 12.0 kB (checked in by robert, 5 years ago)

For testing purposes added code path and options for testing viewer creation in series, including with enabling of VBO's. Options are:

osgcamera -r 5 --vbo cow.osg

Which repeats construction of the viewer 5 times in a row, and enables VBO, and on each repeat a new model is loaded.

osgcamera -r 2 --vbo --shared cow.osg

Which repeats construction of the viewer 2 times in a row, and enables VBO, and on each loads the model once and shares it between each instance of the viewer.

  • 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
176class EnableVBOVisitor : public osg::NodeVisitor
177{
178public:
179    EnableVBOVisitor():
180        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
181
182    void apply(osg::Geode& geode)
183    {
184        for(unsigned int i=0; i<geode.getNumDrawables();++i)
185        {
186            osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
187            osg::notify(osg::NOTICE)<<"Enabling VBO"<<std::endl;
188            geom->setUseVertexBufferObjects(true);
189        }
190    }
191};
192
193int main( int argc, char **argv )
194{
195    // use an ArgumentParser object to manage the program arguments.
196    osg::ArgumentParser arguments(&argc,argv);
197
198    if (argc<2)
199    {
200        std::cout << argv[0] <<": requires filename argument." << std::endl;
201        return 1;
202    }
203
204    unsigned int numRepeats = 2;
205    if (arguments.read("--repeat",numRepeats) || arguments.read("-r",numRepeats) || arguments.read("--repeat") || arguments.read("-r"))
206    {
207
208        bool sharedModel = arguments.read("--shared");
209        bool enableVBO = arguments.read("--vbo");
210
211        osg::ref_ptr<osg::Node> model;
212        if (sharedModel)
213        {
214            model = osgDB::readNodeFiles(arguments);
215            if (!model) return 0;
216
217            if (enableVBO)
218            {
219                EnableVBOVisitor enableVBOs;
220                model->accept(enableVBOs);
221            }
222        }
223
224        osgViewer::Viewer::ThreadingModel threadingModel = osgViewer::Viewer::AutomaticSelection;
225        while (arguments.read("-s")) { threadingModel = osgViewer::Viewer::SingleThreaded; }
226        while (arguments.read("-g")) { threadingModel = osgViewer::Viewer::CullDrawThreadPerContext; }
227        while (arguments.read("-d")) { threadingModel = osgViewer::Viewer::DrawThreadPerContext; }
228        while (arguments.read("-c")) { threadingModel = osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext; }
229
230        for(unsigned int i=0; i<numRepeats; ++i)
231        {
232            osg::notify(osg::NOTICE)<<"+++++++++++++ New viewer ++++++++++++"<<std::endl;
233
234            osgViewer::Viewer viewer;
235
236            viewer.setThreadingModel(threadingModel);
237
238            if (sharedModel) viewer.setSceneData(model.get());
239            else
240            {
241                osg::ref_ptr<osg::Node> node = osgDB::readNodeFiles(arguments);
242                if (!node) return 0;
243
244                if (enableVBO)
245                {
246                    EnableVBOVisitor enableVBOs;
247                    node->accept(enableVBOs);
248                }
249
250                viewer.setSceneData(node.get());
251            }
252
253            viewer.run();
254
255            osg::notify(osg::NOTICE)<<"------------ Viewer ended ----------"<<std::endl<<std::endl;
256        }
257        return 0;
258    }
259
260
261    std::string pathfile;
262    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
263    while (arguments.read("-p",pathfile))
264    {
265        apm = new osgGA::AnimationPathManipulator(pathfile);
266        if (!apm.valid() || !(apm->valid()) )
267        {
268            apm = 0;
269        }
270    }
271
272    osgViewer::Viewer viewer(arguments);
273   
274    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
275    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::CullDrawThreadPerContext); }
276    while (arguments.read("-d")) { viewer.setThreadingModel(osgViewer::Viewer::DrawThreadPerContext); }
277    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::CullThreadPerCameraDrawThreadPerContext); }
278   
279    bool limitNumberOfFrames = false;
280    unsigned int maxFrames = 10;
281    while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; }
282
283    // alternative viewer window setups.
284    while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); }
285    while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer, false); }
286    while (arguments.read("-3")) { multipleWindowMultipleCameras(viewer, true); }
287
288    if (apm.valid()) viewer.setCameraManipulator(apm.get());
289    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
290   
291    viewer.addEventHandler(new osgViewer::StatsHandler);
292    viewer.addEventHandler(new osgViewer::ThreadingHandler);
293
294    std::string configfile;
295    while (arguments.read("--config", configfile))
296    {
297        osg::notify(osg::NOTICE)<<"Trying to read config file "<<configfile<<std::endl;
298        osg::ref_ptr<osg::Object> object = osgDB::readObjectFile(configfile);
299        osgViewer::View* view = dynamic_cast<osgViewer::View*>(object.get());
300        if (view)
301        {
302            osg::notify(osg::NOTICE)<<"Read config file succesfully"<<std::endl;
303        }
304        else
305        {
306            osg::notify(osg::NOTICE)<<"Failed to read config file : "<<configfile<<std::endl;
307            return 1;
308        }
309    }
310
311    while (arguments.read("--write-config", configfile)) { osgDB::writeObjectFile(viewer, configfile); }
312
313
314    if (arguments.read("-m"))
315    {
316        ModelHandler* modelHandler = new ModelHandler;
317        for(int i=1; i<arguments.argc();++i)
318        {
319            modelHandler->add(arguments[i]);
320        }
321
322        viewer.addEventHandler(modelHandler);
323    }
324    else
325    {
326        // load the scene.
327        osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
328
329        if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
330
331        if (!loadedModel)
332        {
333            std::cout << argv[0] <<": No data loaded." << std::endl;
334            return 1;
335        }
336
337        viewer.setSceneData(loadedModel.get());
338    }
339   
340    viewer.realize();
341
342    unsigned int numFrames = 0;
343    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
344    {
345        viewer.frame();
346        ++numFrames;
347    }
348
349    return 0;
350}
Note: See TracBrowser for help on using the browser.