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

Revision 4170, 5.7 kB (checked in by robert, 10 years ago)

Added more comprehensive releaseGLObjects(State*=0) throughout Nodes,
Drawables,StateSet?, and osgDB::Registry.

Added cleanup_frame() from to osgProducer::OsgCamerGroup? to help with proper
clean of OpenGL objects before exit, and modified osgviewer, osghangglider,
osgwindows examples to do the extra frame call to cleanup_frame() before exit.

  • 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#define TWO_SEPERATE_WINDOWS
19
20#ifdef TWO_SEPERATE_WINDOWS
21    Producer::RenderSurface *rs1 = new Producer::RenderSurface;
22    rs1->setScreenNum(0);
23    rs1->setWindowName("osgwindows");
24    //rs1->useBorder(false);
25    rs1->setWindowRectangle(0,0,640,480);
26    Producer::Camera *camera1 = new Producer::Camera;
27    camera1->setRenderSurface(rs1);
28    camera1->setOffset( 1.0, 0.0 );
29
30    Producer::RenderSurface *rs2 = new Producer::RenderSurface;
31    rs2->setScreenNum(0);
32    rs2->setWindowName("osgwindows");
33    //rs2->useBorder(false);
34    rs2->setWindowRectangle(640,0,640,480);
35    Producer::Camera *camera2 = new Producer::Camera;
36    camera2->setRenderSurface(rs2);
37    camera2->setOffset( -1.0, 0.0 );
38
39    rs1->setInputRectangle( Producer::RenderSurface::InputRectangle(0.0,0.5,0.0,1.0));
40    rs2->setInputRectangle( Producer::RenderSurface::InputRectangle(0.5,1.0,0.0,1.0));
41
42    Producer::InputArea *ia = new Producer::InputArea;
43    ia->addRenderSurface(rs1);
44    ia->addRenderSurface(rs2);
45
46    Producer::CameraConfig *cfg = new Producer::CameraConfig;
47    cfg->addCamera("Camera 1",camera1);
48    cfg->addCamera("Camera 2", camera2);
49    cfg->setInputArea(ia);
50    return cfg;
51
52#else
53    // one window with four camera's.
54    Producer::Camera* pcam1 = new Producer::Camera ();
55    pcam1->setProjectionRectangle (0.0f, 0.5f, 0.5f, 1.0f);
56   
57    Producer::Camera* pcam2 = new Producer::Camera ();
58    pcam2->setRenderSurface (pcam1->getRenderSurface ());
59    pcam2->setProjectionRectangle (0.5f, 1.0f, 0.5f, 1.0f);
60
61    Producer::Camera* pcam3 = new Producer::Camera ();
62    pcam3->setRenderSurface (pcam1->getRenderSurface ());
63    pcam3->setProjectionRectangle (0.0f, 0.5f, 0.0f, 0.5f);
64
65    Producer::Camera* pcam4 = new Producer::Camera ();
66    pcam4->setRenderSurface (pcam1->getRenderSurface ());
67    pcam4->setProjectionRectangle (0.5f, 1.0f, 0.0f, 0.5f);
68
69    Producer::CameraConfig *cfg = new Producer::CameraConfig;
70    cfg->addCamera("Camera 1",pcam1);
71    cfg->addCamera("Camera 2",pcam2);
72    cfg->addCamera("Camera 3",pcam3);
73    cfg->addCamera("Camera 4",pcam4);
74   
75    return cfg;
76#endif
77}
78
79int main( int argc, char **argv )
80{
81
82    // use an ArgumentParser object to manage the program arguments.
83    osg::ArgumentParser arguments(&argc,argv);
84   
85    // set up the usage document, in case we need to print out how to use this program.
86    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
87    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrate hows to set up programitically a Producer::CameraConfig and use it to set up multiple window views.");
88    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
89    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
90   
91
92    // construct the viewer.
93    osgProducer::Viewer viewer(BuildConfig());
94
95    // set up the value with sensible default event handlers.
96    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
97
98    // get details on keyboard and mouse bindings used by the viewer.
99    viewer.getUsage(*arguments.getApplicationUsage());
100
101    // if user request help write it out to cout.
102    if (arguments.read("-h") || arguments.read("--help"))
103    {
104        arguments.getApplicationUsage()->write(std::cout);
105        return 1;
106    }
107
108    // any option left unread are converted into errors to write out later.
109    arguments.reportRemainingOptionsAsUnrecognized();
110
111    // report any errors if they have occured when parsing the program aguments.
112    if (arguments.errors())
113    {
114        arguments.writeErrorMessages(std::cout);
115        return 1;
116    }
117   
118    if (arguments.argc()<=1)
119    {
120        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
121        return 1;
122    }
123
124    // read the scene from the list of file specified commandline args.
125    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
126
127    // if no model has been successfully loaded report failure.
128    if (!loadedModel)
129    {
130        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
131        return 1;
132    }
133
134
135    // optimize the scene graph, remove rendundent nodes and state etc.
136    osgUtil::Optimizer optimizer;
137    optimizer.optimize(loadedModel.get());
138
139    // set the scene to render
140    viewer.setSceneData(loadedModel.get());
141
142    // create the windows and run the threads.
143    viewer.realize();
144
145    while( !viewer.done() )
146    {
147        // wait for all cull and draw threads to complete.
148        viewer.sync();
149
150        // update the scene by traversing it with the the update visitor which will
151        // call all node update callbacks and animations.
152        viewer.update();
153         
154        // fire off the cull and draw traversals of the scene.
155        viewer.frame();
156       
157    }
158   
159    // wait for all cull and draw threads to complete before exit.
160    viewer.sync();
161
162    // run a clean up frame to delete all OpenGL objects.
163    viewer.cleanup_frame();
164
165    // wait for all the clean up frame to complete.
166    viewer.sync();
167
168    return 0;
169}
Note: See TracBrowser for help on using the browser.