root/OpenSceneGraph/trunk/examples/osgscreencapture/osgscreencapture.cpp @ 8331

Revision 8331, 6.9 kB (checked in by robert, 7 years ago)

Added camera final callback attachment code.

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
32class WindowCaptureCallback : public osg::Camera::DrawCallback
33{
34    public:
35   
36        WindowCaptureCallback()
37        {
38        }
39
40        virtual void operator () (osg::RenderInfo& renderInfo) const
41        {
42            osg::GraphicsContext* gc = renderInfo.getState()->getGraphicsContext();
43            osg::notify(osg::NOTICE)<<"Capture screen image "<<gc<<std::endl;
44           
45        }
46};
47
48void addCallbackToViewer(osgViewer::ViewerBase& viewer, WindowCaptureCallback* callback)
49{
50    osgViewer::ViewerBase::Windows windows;
51    viewer.getWindows(windows);
52    for(osgViewer::ViewerBase::Windows::iterator itr = windows.begin();
53        itr != windows.end();
54        ++itr)
55    {
56        osgViewer::GraphicsWindow* window = *itr;
57        osg::GraphicsContext::Cameras& cameras = window->getCameras();
58        osg::Camera* lastCamera = 0;
59        for(osg::GraphicsContext::Cameras::iterator cam_itr = cameras.begin();
60            cam_itr != cameras.end();
61            ++cam_itr)
62        {
63            if (lastCamera)
64            {
65                if ((*cam_itr)->getRenderOrder() > (*cam_itr)->getRenderOrder())
66                {
67                    lastCamera = (*cam_itr);
68                }
69                if ((*cam_itr)->getRenderOrder() == lastCamera->getRenderOrder() &&
70                    (*cam_itr)->getRenderOrderNum() >= lastCamera->getRenderOrderNum())
71                {
72                    lastCamera = (*cam_itr);
73                }
74            }
75            else
76            {
77                lastCamera = *cam_itr;
78            }
79        }
80       
81        if (lastCamera)
82        {
83            osg::notify(osg::NOTICE)<<"Last camera "<<lastCamera<<std::endl;
84           
85            lastCamera->setFinalDrawCallback(callback);
86        }
87        else
88        {
89            osg::notify(osg::NOTICE)<<"No camera found"<<std::endl;
90        }
91    }
92}
93
94int main(int argc, char** argv)
95{
96    // use an ArgumentParser object to manage the program arguments.
97    osg::ArgumentParser arguments(&argc,argv);
98
99    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
100    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
101    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
102    arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad");
103    arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField");
104
105    osgViewer::Viewer viewer(arguments);
106
107    unsigned int helpType = 0;
108    if ((helpType = arguments.readHelpType()))
109    {
110        arguments.getApplicationUsage()->write(std::cout, helpType);
111        return 1;
112    }
113   
114    // report any errors if they have occurred when parsing the program arguments.
115    if (arguments.errors())
116    {
117        arguments.writeErrorMessages(std::cout);
118        return 1;
119    }
120   
121    if (arguments.argc()<=1)
122    {
123        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
124        return 1;
125    }
126
127    // set up the camera manipulators.
128    {
129        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
130
131        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
132        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
133        keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
134        keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
135
136        std::string pathfile;
137        char keyForAnimationPath = '5';
138        while (arguments.read("-p",pathfile))
139        {
140            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
141            if (apm || !apm->valid())
142            {
143                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
144                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
145                keyswitchManipulator->selectMatrixManipulator(num);
146                ++keyForAnimationPath;
147            }
148        }
149
150        viewer.setCameraManipulator( keyswitchManipulator.get() );
151    }
152
153    // add the state manipulator
154    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
155   
156    // add the thread model handler
157    viewer.addEventHandler(new osgViewer::ThreadingHandler);
158
159    // add the window size toggle handler
160    viewer.addEventHandler(new osgViewer::WindowSizeHandler);
161       
162    // add the stats handler
163    viewer.addEventHandler(new osgViewer::StatsHandler);
164
165    // add the help handler
166    viewer.addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));
167
168    // add the record camera path handler
169    viewer.addEventHandler(new osgViewer::RecordCameraPathHandler);
170
171    // add the LOD Scale handler
172    viewer.addEventHandler(new osgViewer::LODScaleHandler);
173
174    // load the data
175    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
176    if (!loadedModel)
177    {
178        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
179        return 1;
180    }
181
182    // any option left unread are converted into errors to write out later.
183    arguments.reportRemainingOptionsAsUnrecognized();
184
185    // report any errors if they have occurred when parsing the program arguments.
186    if (arguments.errors())
187    {
188        arguments.writeErrorMessages(std::cout);
189        return 1;
190    }
191
192
193    // optimize the scene graph, remove redundant nodes and state etc.
194    osgUtil::Optimizer optimizer;
195    optimizer.optimize(loadedModel.get());
196
197    viewer.setSceneData( loadedModel.get() );
198
199    viewer.realize();
200   
201    addCallbackToViewer(viewer, new WindowCaptureCallback);
202
203    return viewer.run();
204
205}
Note: See TracBrowser for help on using the browser.