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

Revision 5916, 6.1 kB (checked in by robert, 7 years ago)

Cleaned up graphics window setup and added clear graphics window to single window,
multiple camera setup.

  • 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    traits->windowDecoration = true;
27    traits->doubleBuffer = true;
28    traits->sharedContext = 0;
29
30    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
31    if (gc.valid())
32    {
33        osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
34
35        // need to ensure that the window is cleared make sure that the complete window is set the correct colour
36        // rather than just the parts of the window that are under the camera's viewports
37        gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
38        gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
39    }
40    else
41    {
42        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
43    }
44
45    unsigned int numCameras = 2;
46    double aspectRatioScale = 1.0;///(double)numCameras;
47    for(unsigned int i=0; i<numCameras;++i)
48    {
49        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
50        camera->setGraphicsContext(gc.get());
51        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
52        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
53        camera->setDrawBuffer(buffer);
54        camera->setReadBuffer(buffer);
55
56        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
57    }
58}
59
60void multipleWindowMultipleCameras(osgViewer::Viewer& viewer)
61{
62    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
63    if (!wsi)
64    {
65        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : 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
73    unsigned int numCameras = 4;
74    double aspectRatioScale = (double)numCameras;
75    double translate_x = double(numCameras)-1;
76    for(unsigned int i=0; i<numCameras;++i, translate_x -= 2.0)
77    {
78        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
79        traits->screenNum = i / 2;
80        traits->x = (i*width)/numCameras;
81        traits->y = 0;
82        traits->width = width/numCameras-1;
83        traits->height = height;
84        traits->windowDecoration = true;
85        traits->doubleBuffer = true;
86        traits->sharedContext = 0;
87
88        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
89        if (gc.valid())
90        {
91            osg::notify(osg::INFO)<<"  GraphicsWindow has been created successfully."<<std::endl;
92        }
93        else
94        {
95            osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
96        }
97
98        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
99        camera->setGraphicsContext(gc.get());
100        camera->setViewport(new osg::Viewport(0,0, width/numCameras, height));
101        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
102        camera->setDrawBuffer(buffer);
103        camera->setReadBuffer(buffer);
104
105        viewer.addSlave(camera.get(), osg::Matrix::scale(aspectRatioScale, 1.0, 1.0)*osg::Matrix::translate(translate_x, 0.0, 0.0), osg::Matrix() );
106    }
107}
108
109int main( int argc, char **argv )
110{
111    // use an ArgumentParser object to manage the program arguments.
112    osg::ArgumentParser arguments(&argc,argv);
113
114    if (argc<2)
115    {
116        std::cout << argv[0] <<": requires filename argument." << std::endl;
117        return 1;
118    }
119
120
121    std::string pathfile;
122    osg::ref_ptr<osgGA::AnimationPathManipulator> apm = 0;
123    while (arguments.read("-p",pathfile))
124    {
125        apm = new osgGA::AnimationPathManipulator(pathfile);
126        if(!apm.valid() || !(apm->valid()) )
127        {
128            apm = 0;
129        }
130    }
131
132    osgViewer::Viewer viewer;
133   
134    while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); }
135    while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerContext); }
136    while (arguments.read("-c")) { viewer.setThreadingModel(osgViewer::Viewer::ThreadPerCamera); }
137   
138    bool limitNumberOfFrames = false;
139    unsigned int maxFrames = 10;
140    while (arguments.read("--run-till-frame-number",maxFrames)) { limitNumberOfFrames = true; }
141
142    // alternative viewer window setups.
143    while (arguments.read("-1")) { singleWindowMultipleCameras(viewer); }
144    while (arguments.read("-2")) { multipleWindowMultipleCameras(viewer); }
145
146
147    if (apm.valid()) viewer.setCameraManipulator(apm.get());
148    else viewer.setCameraManipulator( new osgGA::TrackballManipulator() );
149
150    // load the scene.
151    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
152    if (!loadedModel)
153    {
154        std::cout << argv[0] <<": No data loaded." << std::endl;
155        return 1;
156    }
157
158    viewer.setSceneData(loadedModel.get());
159
160    // viewer.realize();
161
162    unsigned int numFrames = 0;
163    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
164    {
165        viewer.frame();
166        ++numFrames;
167    }
168
169    return 0;
170}
171#else
172
173#include <osgViewer/Viewer>
174#include <osgDB/ReadFile>
175
176int main( int, char **)
177{
178    osgViewer::Viewer viewer;
179    viewer.setSceneData(osgDB::readNodeFile("cow.osg"));
180    viewer.run();
181}
182#endif
Note: See TracBrowser for help on using the browser.