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

Revision 6927, 9.2 kB (checked in by robert, 7 years ago)

Improved the set up of static build, added freetype plugin to plugins that support static usage

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