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

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

Made Refernced::setThreadSafeReferenceCounting(bool) a virtual then overrode
this in various scene graph classes to ensure that the scene graph gets
updated as well as the objects that the initialial call is made from.

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