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

Revision 6916, 3.5 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osgDB/ReadFile>
13#include <osgUtil/Optimizer>
14#include <osgViewer/Viewer>
15#include <iostream>
16
17int main( int argc, char **argv )
18{
19    // use an ArgumentParser object to manage the program arguments.
20    osg::ArgumentParser arguments(&argc,argv);
21
22    // read the scene from the list of file specified commandline args.
23    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
24
25    // if not loaded assume no arguments passed in, try use default mode instead.
26    if (!loadedModel) loadedModel = osgDB::readNodeFile("cow.osg");
27   
28    // if no model has been successfully loaded report failure.
29    if (!loadedModel)
30    {
31        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
32        return 1;
33    }
34
35    // construct the viewer.
36    osgViewer::Viewer viewer;
37
38    // left window + left slave camera
39    {
40        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
41        traits->x = 0;
42        traits->y = 0;
43        traits->width = 640;
44        traits->height = 480;
45        traits->windowDecoration = true;
46        traits->doubleBuffer = true;
47        traits->sharedContext = 0;
48
49        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
50
51        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
52        camera->setGraphicsContext(gc.get());
53        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
54        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
55        camera->setDrawBuffer(buffer);
56        camera->setReadBuffer(buffer);
57
58        // add this slave camra to the viewer, with a shift left of the projection matrix
59        viewer.addSlave(camera.get(), osg::Matrixd::translate(1.0,0.0,0.0), osg::Matrixd());
60    }
61   
62    // left window + left slave camera
63    {
64        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
65        traits->x = 640;
66        traits->y = 0;
67        traits->width = 640;
68        traits->height = 480;
69        traits->windowDecoration = true;
70        traits->doubleBuffer = true;
71        traits->sharedContext = 0;
72
73        osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
74
75        osg::ref_ptr<osg::Camera> camera = new osg::Camera;
76        camera->setGraphicsContext(gc.get());
77        camera->setViewport(new osg::Viewport(0,0, traits->width, traits->height));
78        GLenum buffer = traits->doubleBuffer ? GL_BACK : GL_FRONT;
79        camera->setDrawBuffer(buffer);
80        camera->setReadBuffer(buffer);
81
82        // add this slave camra to the viewer, with a shift right of the projection matrix
83        viewer.addSlave(camera.get(), osg::Matrixd::translate(-1.0,0.0,0.0), osg::Matrixd());
84    }
85
86
87    // optimize the scene graph, remove rendundent nodes and state etc.
88    osgUtil::Optimizer optimizer;
89    optimizer.optimize(loadedModel.get());
90
91    // set the scene to render
92    viewer.setSceneData(loadedModel.get());
93
94    return viewer.run();
95}
Note: See TracBrowser for help on using the browser.