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

Revision 5878, 6.5 kB (checked in by robert, 10 years ago)

Added setting of default backgroud colour of View's master colour to be
consistent with the previous default colour used by SceneView?

  • 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::NOTICE)<<"  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    viewer.setUpRenderingSupport();
67    viewer.assignSceneDataToCameras();
68}
69
70void multipleWindowMultipleCameras(osgViewer::Viewer& viewer)
71{
72    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
73    if (!wsi)
74    {
75        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
76        return;
77    }
78   
79    unsigned int width, height;
80    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
81
82
83    unsigned int numCameras = 2;
84    double aspectRatioScale = (double)numCameras;
85    double translate_x = double(numCameras)-1;
86    for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0)
87    {
88        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
89        traits->x = (i*width)/numCameras;
90        traits->y = 0;
91        traits->width = width/numCameras-1;
92        traits->height = height;
93    #if 1
94        traits->windowDecoration = false;
95    #else
96        traits->windowDecoration = true;
97    #endif           
98        traits->doubleBuffer = true;
99        traits->sharedContext = 0;
100
101        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
102
103        osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
104        if (gw)
105        {
106            osg::notify(osg::NOTICE)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl;
107
108            gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, traits->width, traits->height );
109        }
110        else
111        {
112            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
113        }
114
115        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
116        camera->setGraphicsContext(gc.get());
117        camera->setViewport(new osg::Viewport(0,0, width/numCameras, height));
118        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
119        camera->setDrawBuffer(buffer);
120        camera->setReadBuffer(buffer);
121
122        viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() );
123    }
124
125    viewer.setUpRenderingSupport();
126    viewer.assignSceneDataToCameras();
127}
128
129int main( int argc, char **argv )
130{
131    // use an ArgumentParser object to manage the program arguments.
132    osg::ArgumentParser arguments(&argc,argv);
133
134    if (argc<2)
135    {
136        std::cout << argv[0] <<": requires filename argument." << std::endl;
137        return 1;
138    }
139
140
141    std::string pathfile;
142    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
143    while (arguments.read("-p",pathfile))
144    {
145        apm = new osgGA::AnimationPathManipulator(pathfile);
146        if(!apm.valid() || !(apm->valid()) )
147        {
148            apm = 0;
149        }
150    }
151
152    osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2);
153    osg::Referenced::setThreadSafeReferenceCounting(true);
154
155    // load the scene.
156    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
157    if (!loadedModel)
158    {
159        std::cout << argv[0] <<": No data loaded." << std::endl;
160        return 1;
161    }
162
163    osgViewer::Viewer viewer;
164   
165    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
166    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); }
167    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); }
168
169    viewer.setSceneData(loadedModel.get());
170
171    if (apm.valid()) viewer.setCameraManipulator(apm.get());
172    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
173
174#if 0
175
176    // singleWindowMultipleCameras(viewer);
177   
178    multipleWindowMultipleCameras(viewer);
179   
180   
181#else
182
183    viewer.setUpViewAcrossAllScreens();
184
185#endif   
186   
187    viewer.realize();
188
189    bool limitNumberOfFrames = false;
190    unsigned int numFrames = 0;
191    unsigned int maxFrames = 10;
192
193    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
194    {
195        viewer.frame();
196        ++numFrames;
197    }
198
199    return 0;
200}
Note: See TracBrowser for help on using the browser.