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

Revision 5911, 6.3 kB (checked in by robert, 8 years ago)

Ported osgbillboard over to osgViewer

  • 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 <osgViewer/Viewer>
5#include <osgGA/TrackballManipulator>
6#include <osgGA/AnimationPathManipulator>
7#include <iostream>
8
9void singleWindowMultipleCameras(osgViewer::Viewer& viewer)
10{
11    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
12    if (!wsi)
13    {
14        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
15        return;
16    }
17   
18    unsigned int width, height;
19    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
20
21    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
22    traits->x = 0;
23    traits->y = 0;
24    traits->width = width;
25    traits->height = height;
26#if 0
27    traits->windowDecoration = false;
28#else
29    traits->windowDecoration = true;
30#endif           
31    traits->doubleBuffer = true;
32    traits->sharedContext = 0;
33
34    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
35
36    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
37    if (gw)
38    {
39        osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl;
40
41        gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height );
42    }
43    else
44    {
45        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
46    }
47
48    unsigned int numCameras = 2;
49    double aspectRatioScale = 1.0;///(double)numCameras;
50    for(unsigned int i=0; i<numCameras;++i)
51    {
52        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
53        camera->setGraphicsContext(gc.get());
54        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
55        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
56        camera->setDrawBuffer(buffer);
57        camera->setReadBuffer(buffer);
58
59        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
60    }
61}
62
63void multipleWindowMultipleCameras(osgViewer::Viewer& viewer)
64{
65    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
66    if (!wsi)
67    {
68        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
69        return;
70    }
71   
72    unsigned int width, height;
73    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
74
75
76    unsigned int numCameras = 4;
77    double aspectRatioScale = (double)numCameras;
78    double translate_x = double(numCameras)-1;
79    for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0)
80    {
81        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
82        traits->screenNum = i / 2;
83        traits->x = (i*width)/numCameras;
84        traits->y = 0;
85        traits->width = width/numCameras-1;
86        traits->height = height;
87    #if 0
88        traits->windowDecoration = false;
89    #else
90        traits->windowDecoration = true;
91    #endif           
92        traits->doubleBuffer = true;
93        traits->sharedContext = 0;
94
95        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
96
97        osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
98        if (gw)
99        {
100            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl;
101
102            gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, traits->width, traits->height );
103        }
104        else
105        {
106            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
107        }
108
109        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
110        camera->setGraphicsContext(gc.get());
111        camera->setViewport(new osg::Viewport(0,0, width/numCameras, height));
112        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
113        camera->setDrawBuffer(buffer);
114        camera->setReadBuffer(buffer);
115
116        viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() );
117    }
118}
119
120int main( int argc, char **argv )
121{
122    // use an ArgumentParser object to manage the program arguments.
123    osg::ArgumentParser arguments(&argc,argv);
124
125    if (argc<2)
126    {
127        std::cout << argv[0] <<": requires filename argument." << std::endl;
128        return 1;
129    }
130
131
132    std::string pathfile;
133    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
134    while (arguments.read("-p",pathfile))
135    {
136        apm = new osgGA::AnimationPathManipulator(pathfile);
137        if(!apm.valid() || !(apm->valid()) )
138        {
139            apm = 0;
140        }
141    }
142
143    osgViewer::Viewer viewer;
144   
145    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
146    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); }
147    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); }
148   
149    bool limitNumberOfFrames = false;
150    unsigned int maxFrames = 10;
151    while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; }
152
153    // alternative viewer window setups.
154    while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); }
155    while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer); }
156
157
158    if (apm.valid()) viewer.setCameraManipulator(apm.get());
159    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
160
161    // load the scene.
162    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
163    if (!loadedModel)
164    {
165        std::cout << argv[0] <<": No data loaded." << std::endl;
166        return 1;
167    }
168
169    viewer.setSceneData(loadedModel.get());
170
171    // viewer.realize();
172
173    unsigned int numFrames = 0;
174    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
175    {
176        viewer.frame();
177        ++numFrames;
178    }
179
180    return 0;
181}
182#else
183
184#include <osgViewer/Viewer>
185#include <osgDB/ReadFile>
186
187int main( int, char **)
188{
189    osgViewer::Viewer viewer;
190    viewer.setSceneData(osgDB::readNodeFile("cow.osg"));
191    viewer.run();
192}
193#endif
Note: See TracBrowser for help on using the browser.