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

Revision 1818, 4.5 kB (checked in by robert, 11 years ago)

Added osgwindows example which sets up and Producer::CameraConfig? up
and uses that to create multiple windows.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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 <osgProducer/Viewer>
15
16static Producer::CameraConfig *BuildConfig(void)
17{
18    Producer::RenderSurface *rs1 = new Producer::RenderSurface;
19    rs1->setScreenNum(0);
20    //rs1->useBorder(false);
21    rs1->setWindowRect(0,0,640,480);
22    Producer::Camera *camera1 = new Producer::Camera;
23    camera1->setRenderSurface(rs1);
24    camera1->setOffset( 1.0, 0.0 );
25
26    Producer::RenderSurface *rs2 = new Producer::RenderSurface;
27    rs2->setScreenNum(0);
28    //rs2->useBorder(false);
29    rs2->setWindowRect(640,0,640,480);
30    Producer::Camera *camera2 = new Producer::Camera;
31    camera2->setRenderSurface(rs2);
32    camera2->setOffset( -1.0, 0.0 );
33
34    Producer::InputArea *ia = new Producer::InputArea;
35    ia->addInputRectangle( rs1, Producer::InputRectangle(0.0,0.5,0.0,1.0));
36    ia->addInputRectangle( rs2, Producer::InputRectangle(0.5,1.0,0.0,1.0));
37
38
39    Producer::CameraConfig *cfg = new Producer::CameraConfig;
40    cfg->addCamera("Camera 1",camera1);
41    cfg->addCamera("Camera 2", camera2);
42    cfg->setInputArea(ia);
43    return cfg;
44}
45
46int main( int argc, char **argv )
47{
48
49    // use an ArgumentParser object to manage the program arguments.
50    osg::ArgumentParser arguments(&argc,argv);
51   
52    // set up the usage document, in case we need to print out how to use this program.
53    arguments.getApplicationUsage()->setApplicatonName(arguments.getApplicationName());
54    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
55    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
56    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
57   
58
59    // construct the viewer.
60    osgProducer::Viewer viewer(BuildConfig());
61
62    // set up the value with sensible default event handlers.
63    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
64
65    // get details on keyboard and mouse bindings used by the viewer.
66    viewer.getUsage(*arguments.getApplicationUsage());
67
68    // if user request help write it out to cout.
69    if (arguments.read("-h") || arguments.read("--help"))
70    {
71        arguments.getApplicationUsage()->write(std::cout);
72        return 1;
73    }
74
75    // any option left unread are converted into errors to write out later.
76    arguments.reportRemainingOptionsAsUnrecognized();
77
78    // report any errors if they have occured when parsing the program aguments.
79    if (arguments.errors())
80    {
81        arguments.writeErrorMessages(std::cout);
82        return 1;
83    }
84   
85    if (arguments.argc()<=1)
86    {
87        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
88        return 1;
89    }
90
91    // read the scene from the list of file specified commandline args.
92    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
93
94    // if no model has been successfully loaded report failure.
95    if (!loadedModel)
96    {
97        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
98        return 1;
99    }
100
101
102    // optimize the scene graph, remove rendundent nodes and state etc.
103    osgUtil::Optimizer optimizer;
104    optimizer.optimize(loadedModel.get());
105
106    // set the scene to render
107    viewer.setSceneData(loadedModel.get());
108
109    // create the windows and run the threads.
110//    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
111    viewer.realize(Producer::CameraGroup::SingleThreaded);
112
113    while( !viewer.done() )
114    {
115        // wait for all cull and draw threads to complete.
116        viewer.sync();
117
118        // update the scene by traversing it with the the update visitor which will
119        // call all node update callbacks and animations.
120        viewer.update();
121         
122        // fire off the cull and draw traversals of the scene.
123        viewer.frame();
124       
125    }
126   
127    // wait for all cull and draw threads to complete before exit.
128    viewer.sync();
129
130    return 0;
131}
Note: See TracBrowser for help on using the browser.