root/OpenSceneGraph/trunk/examples/osgscreencapture/osgscreenscapture.cpp @ 8329

Revision 8329, 5.0 kB (checked in by robert, 7 years ago)

Added new osgscreencapture example folder, implementation to follow

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 <osg/CoordinateSystemNode>
15
16#include <osg/Switch>
17#include <osgText/Text>
18
19#include <osgViewer/Viewer>
20#include <osgViewer/ViewerEventHandlers>
21
22#include <osgGA/TrackballManipulator>
23#include <osgGA/FlightManipulator>
24#include <osgGA/DriveManipulator>
25#include <osgGA/KeySwitchMatrixManipulator>
26#include <osgGA/StateSetManipulator>
27#include <osgGA/AnimationPathManipulator>
28#include <osgGA/TerrainManipulator>
29
30#include <iostream>
31
32int main(int argc, char** argv)
33{
34    // use an ArgumentParser object to manage the program arguments.
35    osg::ArgumentParser arguments(&argc,argv);
36
37    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
38    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
39    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
40    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
41    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
42
43    osgViewer::Viewer viewer(arguments);
44
45    unsigned int helpType = 0;
46    if ((helpType = arguments.readHelpType()))
47    {
48        arguments.getApplicationUsage()->write(std::cout, helpType);
49        return 1;
50    }
51   
52    // report any errors if they have occurred when parsing the program arguments.
53    if (arguments.errors())
54    {
55        arguments.writeErrorMessages(std::cout);
56        return 1;
57    }
58   
59    if (arguments.argc()<=1)
60    {
61        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
62        return 1;
63    }
64
65    // set up the camera manipulators.
66    {
67        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
68
69        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
70        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
71        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
72        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
73
74        std::string pathfile;
75        char keyForAnimationPath = '5';
76        while (arguments.read("-p",pathfile))
77        {
78            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
79            if (apm || !apm->valid())
80            {
81                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
82                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
83                keyswitchManipulator->selectMatrixManipulator(num);
84                ++keyForAnimationPath;
85            }
86        }
87
88        viewer.setCameraManipulator( keyswitchManipulator.get() );
89    }
90
91    // add the state manipulator
92    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
93   
94    // add the thread model handler
95    viewer.addEventHandler(new osgViewer::ThreadingHandler);
96
97    // add the window size toggle handler
98    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
99       
100    // add the stats handler
101    viewer.addEventHandler(new osgViewer::StatsHandler);
102
103    // add the help handler
104    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
105
106    // add the record camera path handler
107    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
108
109    // add the LOD Scale handler
110    viewer.addEventHandler(new osgViewer::LODScaleHandler);
111
112    // load the data
113    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
114    if (!loadedModel)
115    {
116        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
117        return 1;
118    }
119
120    // any option left unread are converted into errors to write out later.
121    arguments.reportRemainingOptionsAsUnrecognized();
122
123    // report any errors if they have occurred when parsing the program arguments.
124    if (arguments.errors())
125    {
126        arguments.writeErrorMessages(std::cout);
127        return 1;
128    }
129
130
131    // optimize the scene graph, remove redundant nodes and state etc.
132    osgUtil::Optimizer optimizer;
133    optimizer.optimize(loadedModel.get());
134
135    viewer.setSceneData( loadedModel.get() );
136
137    viewer.realize();
138
139    return viewer.run();
140
141}
Note: See TracBrowser for help on using the browser.