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

Revision 5863, 3.4 kB (checked in by robert, 7 years ago)

Various work on osgViewer library, including warp point and graphics window resize support

  • 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 <iostream>
11
12int main( int argc, char **argv )
13{
14    if (argc<2)
15    {
16        std::cout << argv[0] <<": requires filename argument." << std::endl;
17        return 1;
18    }
19
20
21    osg::DisplaySettings::instance()->setMaxNumberOfGraphicsContexts(2);
22    osg::Referenced::setThreadSafeReferenceCounting(true);
23
24    // load the scene.
25    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
26    if (!loadedModel)
27    {
28        std::cout << argv[0] <<": No data loaded." << std::endl;
29        return 1;
30    }
31
32    osgViewer::Viewer viewer;
33   
34    viewer.setSceneData(loadedModel.get());
35
36    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
37    viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f));
38
39#if 1
40
41    osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface();
42    if (!wsi)
43    {
44        osg::notify(osg::NOTICE)<<"View::setUpViewAcrossAllScreens() : Error, no WindowSystemInterface available, cannot create windows."<<std::endl;
45        return 0;
46    }
47   
48    unsigned int width, height;
49    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height);
50
51    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
52    traits->x = 0;
53    traits->y = 0;
54    traits->width = width;
55    traits->height = height;
56#if 0
57    traits->windowDecoration = false;
58#else
59    traits->windowDecoration = true;
60#endif           
61    traits->doubleBuffer = true;
62    traits->sharedContext = 0;
63
64    osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
65
66    osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
67    if (gw)
68    {
69        osg::notify(osg::NOTICE)<<"  GraphicsWindow has been created successfully."<<gw<<std::endl;
70
71        gw->getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height );
72    }
73    else
74    {
75        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created successfully."<<std::endl;
76    }
77
78    unsigned int numCameras = 2;
79    double aspectRatioScale = 1.0;///(double)numCameras;
80    for(unsigned int i=0; i<numCameras;++i)
81    {
82        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
83        camera->setGraphicsContext(gc.get());
84        camera->setViewport(new osg::Viewport((i*width)/numCameras,(i*height)/numCameras, width/numCameras, height/numCameras));
85        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
86        camera->setDrawBuffer(buffer);
87        camera->setReadBuffer(buffer);
88
89        viewer.addSlave(camera.get(), osg::Matrixd(), osg::Matrixd::scale(aspectRatioScale,1.0,1.0));
90    }
91
92    viewer.setUpRenderingSupport();
93    viewer.assignSceneDataToCameras();
94   
95#else
96
97    viewer.setUpViewAcrossAllScreens();
98
99#endif   
100   
101    viewer.realize();
102
103    gw->requestWarpPointer(100,100);
104
105    bool limitNumberOfFrames = false;
106    unsigned int numFrames = 0;
107    unsigned int maxFrames = 10;
108
109    while(!viewer.done() && !(limitNumberOfFrames && numFrames>=maxFrames))
110    {
111        viewer.frame();
112        ++numFrames;
113    }
114
115    return 0;
116}
Note: See TracBrowser for help on using the browser.