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

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