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

Revision 6941, 10.1 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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