root/OpenSceneGraph/trunk/examples/osgwindows/osgwindows.cpp @ 6941

Revision 6941, 4.0 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgwindows.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgDB/ReadFile>
20#include <osgUtil/Optimizer>
21#include <osgViewer/Viewer>
22
23
24#include <iostream>
25
26int main( int argc, char **argv )
27{
28    // use an ArgumentParser object to manage the program arguments.
29    osg::ArgumentParser arguments(&argc,argv);
30
31    // read the scene from the list of file specified commandline args.
32    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
33
34    // if not loaded assume no arguments passed in, try use default mode instead.
35    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
36   
37    // if no model has been successfully loaded report failure.
38    if (!loadedModel)
39    {
40        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
41        return 1;
42    }
43
44    // construct the viewer.
45    osgViewer::Viewer viewer;
46
47    // left window + left slave camera
48    {
49        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
50        traits->x = 0;
51        traits->y = 0;
52        traits->width = 640;
53        traits->height = 480;
54        traits->windowDecoration = true;
55        traits->doubleBuffer = true;
56        traits->sharedContext = 0;
57
58        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
59
60        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
61        camera->setGraphicsContext(gc.get());
62        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
63        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
64        camera->setDrawBuffer(buffer);
65        camera->setReadBuffer(buffer);
66
67        // add this slave camra to the viewer, with a shift left of the projection matrix
68        viewer.addSlave(camera.get(), osg::Matrixd::translate(1.0,0.0,0.0), osg::Matrixd());
69    }
70   
71    // left window + left slave camera
72    {
73        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
74        traits->x = 640;
75        traits->y = 0;
76        traits->width = 640;
77        traits->height = 480;
78        traits->windowDecoration = true;
79        traits->doubleBuffer = true;
80        traits->sharedContext = 0;
81
82        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
83
84        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
85        camera->setGraphicsContext(gc.get());
86        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
87        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
88        camera->setDrawBuffer(buffer);
89        camera->setReadBuffer(buffer);
90
91        // add this slave camra to the viewer, with a shift right of the projection matrix
92        viewer.addSlave(camera.get(), osg::Matrixd::translate(-1.0,0.0,0.0), osg::Matrixd());
93    }
94
95
96    // optimize the scene graph, remove rendundent nodes and state etc.
97    osgUtil::Optimizer optimizer;
98    optimizer.optimize(loadedModel.get());
99
100    // set the scene to render
101    viewer.setSceneData(loadedModel.get());
102
103    return viewer.run();
104}
Note: See TracBrowser for help on using the browser.