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

Revision 5895, 6.3 kB (checked in by robert, 7 years ago)

Changed compile paths into runtime selected paths using command line parameters

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