root/OpenSceneGraph/trunk/examples/osgposter/osgposter.cpp @ 12529

Revision 12529, 16.0 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph example, osgposter.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/ArgumentParser>
20#include <osg/Texture2D>
21#include <osg/Switch>
22#include <osgDB/ReadFile>
23#include <osgDB/WriteFile>
24#include <osgGA/TrackballManipulator>
25#include <osgGA/StateSetManipulator>
26#include <osgViewer/Renderer>
27#include <osgViewer/Viewer>
28#include <osgViewer/ViewerEventHandlers>
29#include <iostream>
30#include "PosterPrinter.h"
31
32/* Computing view matrix helpers */
33template<class T>
34class FindTopMostNodeOfTypeVisitor : public osg::NodeVisitor
35{
36public:
37    FindTopMostNodeOfTypeVisitor()
38    :   osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
39        _foundNode(0)
40    {}
41   
42    void apply( osg::Node& node )
43    {
44        T* result = dynamic_cast<T*>( &node );
45        if ( result ) _foundNode = result;
46        else traverse( node );
47    }
48   
49    T* _foundNode;
50};
51
52template<class T>
53T* findTopMostNodeOfType( osg::Node* node )
54{
55    if ( !node ) return 0;
56   
57    FindTopMostNodeOfTypeVisitor<T> fnotv;
58    node->accept( fnotv );
59    return fnotv._foundNode;
60}
61
62/* Computing view matrix functions */
63void computeViewMatrix( osg::Camera* camera, const osg::Vec3d& eye, const osg::Vec3d& hpr )
64{
65    osg::Matrixd matrix;
66    matrix.makeTranslate( eye );
67    matrix.preMult( osg::Matrixd::rotate( hpr[0], 0.0, 1.0, 0.0) );
68    matrix.preMult( osg::Matrixd::rotate( hpr[1], 1.0, 0.0, 0.0) );
69    matrix.preMult( osg::Matrixd::rotate( hpr[2], 0.0, 0.0, 1.0) );
70    camera->setViewMatrix( osg::Matrixd::inverse(matrix) );
71}
72
73void computeViewMatrixOnEarth( osg::Camera* camera, osg::Node* scene,
74                               const osg::Vec3d& latLongHeight, const osg::Vec3d& hpr )
75{
76    osg::CoordinateSystemNode* csn = findTopMostNodeOfType<osg::CoordinateSystemNode>(scene);
77    if ( !csn ) return;
78   
79    // Compute eye point in world coordiantes
80    osg::Vec3d eye;
81    csn->getEllipsoidModel()->convertLatLongHeightToXYZ(
82        latLongHeight.x(), latLongHeight.y(), latLongHeight.z(), eye.x(), eye.y(), eye.z() );
83   
84    // Build matrix for computing target vector
85    osg::Matrixd target_matrix =
86        osg::Matrixd::rotate( -hpr.x(), osg::Vec3d(1,0,0),
87                              -latLongHeight.x(), osg::Vec3d(0,1,0),
88                               latLongHeight.y(), osg::Vec3d(0,0,1) );
89   
90    // Compute tangent vector
91    osg::Vec3d tangent = target_matrix.preMult( osg::Vec3d(0,0,1) );
92   
93    // Compute non-inclined, non-rolled up vector
94    osg::Vec3d up( eye );
95    up.normalize();
96   
97    // Incline by rotating the target- and up vector around the tangent/up-vector
98    // cross-product
99    osg::Vec3d up_cross_tangent = up ^ tangent;
100    osg::Matrixd incline_matrix = osg::Matrixd::rotate( hpr.y(), up_cross_tangent );
101    osg::Vec3d target = incline_matrix.preMult( tangent );
102   
103    // Roll by rotating the up vector around the target vector
104    osg::Matrixd roll_matrix = incline_matrix * osg::Matrixd::rotate( hpr.z(), target );
105    up = roll_matrix.preMult( up );
106    camera->setViewMatrixAsLookAt( eye, eye+target, up );
107}
108
109/* CustomRenderer: Do culling only while loading PagedLODs */
110class CustomRenderer : public osgViewer::Renderer
111{
112public:
113    CustomRenderer( osg::Camera* camera )
114    : osgViewer::Renderer(camera), _cullOnly(true)
115    {
116    }
117   
118    void setCullOnly(bool on) { _cullOnly = on; }
119
120    virtual void operator ()( osg::GraphicsContext* )
121    {
122        if ( _graphicsThreadDoesCull )
123        {
124            if (_cullOnly) cull();
125            else cull_draw();
126        }
127    }
128   
129    virtual void cull()
130    {
131        osgUtil::SceneView* sceneView = _sceneView[0].get();
132        if ( !sceneView || _done || _graphicsThreadDoesCull )
133            return;
134       
135        updateSceneView( sceneView );
136       
137        osgViewer::View* view = dynamic_cast<osgViewer::View*>( _camera->getView() );
138        if ( view )
139            sceneView->setFusionDistance( view->getFusionDistanceMode(), view->getFusionDistanceValue() );
140        sceneView->inheritCullSettings( *(sceneView->getCamera()) );
141        sceneView->cull();
142    }
143   
144    bool _cullOnly;
145};
146
147/* PrintPosterHandler: A gui handler for interactive high-res capturing */
148class PrintPosterHandler : public osgGA::GUIEventHandler
149{
150public:
151    PrintPosterHandler( PosterPrinter* printer )
152    : _printer(printer), _started(false) {}
153   
154    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
155    {
156        osgViewer::View* view = dynamic_cast<osgViewer::View*>( &aa );
157        if ( !view ) return false;
158       
159        switch( ea.getEventType() )
160        {
161        case osgGA::GUIEventAdapter::FRAME:
162            if ( view->getDatabasePager() )
163            {
164                // Wait until all paged nodes are processed
165                if ( view->getDatabasePager()->getRequestsInProgress() )
166                    break;
167            }
168           
169            if ( _printer.valid() )
170            {
171                _printer->frame( view->getFrameStamp(), view->getSceneData() );
172                if ( _started && _printer->done() )
173                {
174                    osg::Switch* root = dynamic_cast<osg::Switch*>( view->getSceneData() );
175                    if ( root )
176                    {
177                        // Assume child 0 is the loaded model and 1 is the poster camera
178                        // Switch them in time to prevent dual traversals of subgraph
179                        root->setValue( 0, true );
180                        root->setValue( 1, false );
181                    }
182                    _started = false;
183                }
184            }
185            break;
186       
187        case osgGA::GUIEventAdapter::KEYDOWN:
188            if ( ea.getKey()=='p' || ea.getKey()=='P' )
189            {
190                if ( _printer.valid() )
191                {
192                    osg::Switch* root = dynamic_cast<osg::Switch*>( view->getSceneData() );
193                    if ( root )
194                    {
195                        // Assume child 0 is the loaded model and 1 is the poster camera
196                        root->setValue( 0, false );
197                        root->setValue( 1, true );
198                    }
199                   
200                    _printer->init( view->getCamera() );
201                    _started = true;
202                }
203                return true;
204            }
205            break;
206       
207        default:
208            break;
209        }
210        return false;
211    }
212
213protected:
214    osg::ref_ptr<PosterPrinter> _printer;
215    bool _started;
216};
217
218/* The main entry */
219int main( int argc, char** argv )
220{
221    osg::ArgumentParser arguments( &argc, argv );
222    osg::ApplicationUsage* usage = arguments.getApplicationUsage();
223    usage->setDescription( arguments.getApplicationName() +
224                           " is the example which demonstrates how to render high-resolution images (posters).");
225    usage->setCommandLineUsage( arguments.getApplicationName() + " [options] scene_file" );
226    usage->addCommandLineOption( "-h or --help", "Display this information." );
227    usage->addCommandLineOption( "--color <r> <g> <b>", "The background color." );
228    usage->addCommandLineOption( "--ext <ext>", "The output tiles' extension (Default: bmp)." );
229    usage->addCommandLineOption( "--poster <filename>", "The output poster's name (Default: poster.bmp)." );
230    usage->addCommandLineOption( "--tilesize <w> <h>", "Size of each image tile (Default: 640 480)." );
231    usage->addCommandLineOption( "--finalsize <w> <h>", "Size of the poster (Default: 6400 4800)." );
232    usage->addCommandLineOption( "--enable-output-poster", "Output the final poster file (Default)." );
233    usage->addCommandLineOption( "--disable-output-poster", "Don't output the final poster file." );
234    usage->addCommandLineOption( "--enable-output-tiles", "Output all tile files." );
235    usage->addCommandLineOption( "--disable-output-tiles", "Don't output all tile files (Default)." );
236    usage->addCommandLineOption( "--use-fb", "Use Frame Buffer for rendering tiles (Default, recommended).");
237    usage->addCommandLineOption( "--use-fbo", "Use Frame Buffer Object for rendering tiles.");
238    usage->addCommandLineOption( "--use-pbuffer","Use Pixel Buffer for rendering tiles.");
239    usage->addCommandLineOption( "--use-pbuffer-rtt","Use Pixel Buffer RTT for rendering tiles.");
240    usage->addCommandLineOption( "--inactive", "Inactive capturing mode." );
241    usage->addCommandLineOption( "--camera-eye <x> <y> <z>", "Set eye position in inactive mode." );
242    usage->addCommandLineOption( "--camera-latlongheight <lat> <lon> <h>", "Set eye position on earth in inactive mode." );
243    usage->addCommandLineOption( "--camera-hpr <h> <p> <r>", "Set eye rotation in inactive mode." );
244   
245    if ( arguments.read("-h") || arguments.read("--help") )
246    {
247        usage->write( std::cout );
248        return 1;
249    }
250   
251    // Poster arguments
252    bool activeMode = true;
253    bool outputPoster = true, outputTiles = false;
254    int tileWidth = 640, tileHeight = 480;
255    int posterWidth = 640*2, posterHeight = 480*2;
256    std::string posterName = "poster.bmp", extName = "bmp";
257    osg::Vec4 bgColor(0.2f, 0.2f, 0.6f, 1.0f);
258    osg::Camera::RenderTargetImplementation renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
259   
260    while ( arguments.read("--inactive") ) { activeMode = false; }
261    while ( arguments.read("--color", bgColor.r(), bgColor.g(), bgColor.b()) ) {}
262    while ( arguments.read("--tilesize", tileWidth, tileHeight) ) {}
263    while ( arguments.read("--finalsize", posterWidth, posterHeight) ) {}
264    while ( arguments.read("--poster", posterName) ) {}
265    while ( arguments.read("--ext", extName) ) {}
266    while ( arguments.read("--enable-output-poster") ) { outputPoster = true; }
267    while ( arguments.read("--disable-output-poster") ) { outputPoster = false; }
268    while ( arguments.read("--enable-output-tiles") ) { outputTiles = true; }
269    while ( arguments.read("--disable-output-tiles") ) { outputTiles = false; }
270    while ( arguments.read("--use-fbo")) { renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT; }
271    while ( arguments.read("--use-pbuffer")) { renderImplementation = osg::Camera::PIXEL_BUFFER; }
272    while ( arguments.read("--use-pbuffer-rtt")) { renderImplementation = osg::Camera::PIXEL_BUFFER_RTT; }
273    while ( arguments.read("--use-fb")) { renderImplementation = osg::Camera::FRAME_BUFFER; }
274   
275    // Camera settings for inactive screenshot
276    bool useLatLongHeight = true;
277    osg::Vec3d eye;
278    osg::Vec3d latLongHeight( 50.0, 10.0, 2000.0 );
279    osg::Vec3d hpr( 0.0, 0.0, 0.0 );
280    if ( arguments.read("--camera-eye", eye.x(), eye.y(), eye.z()) )
281    {
282        useLatLongHeight = false;
283        activeMode = false;
284    }
285    else if ( arguments.read("--camera-latlongheight", latLongHeight.x(), latLongHeight.y(), latLongHeight.z()) )
286    {
287        activeMode = false;
288        latLongHeight.x() = osg::DegreesToRadians( latLongHeight.x() );
289        latLongHeight.y() = osg::DegreesToRadians( latLongHeight.y() );
290    }
291    if ( arguments.read("--camera-hpr", hpr.x(), hpr.y(), hpr.z()) )
292    {
293        activeMode = false;
294        hpr.x() = osg::DegreesToRadians( hpr.x() );
295        hpr.y() = osg::DegreesToRadians( hpr.y() );
296        hpr.z() = osg::DegreesToRadians( hpr.z() );
297    }
298   
299    // Construct scene graph
300    osg::Node* scene = osgDB::readNodeFiles( arguments );
301    if ( !scene ) scene = osgDB::readNodeFile( "cow.osgt" );
302    if ( !scene )
303    {
304        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
305        return 1;
306    }
307   
308    // Create camera for rendering tiles offscreen. FrameBuffer is recommended because it requires less memory.
309    osg::ref_ptr<osg::Camera> camera = new osg::Camera;
310    camera->setClearColor( bgColor );
311    camera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
312    camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
313    camera->setRenderOrder( osg::Camera::PRE_RENDER );
314    camera->setRenderTargetImplementation( renderImplementation );
315    camera->setViewport( 0, 0, tileWidth, tileHeight );
316    camera->addChild( scene );
317   
318    // Set the printer
319    osg::ref_ptr<PosterPrinter> printer = new PosterPrinter;
320    printer->setTileSize( tileWidth, tileHeight );
321    printer->setPosterSize( posterWidth, posterHeight );
322    printer->setCamera( camera.get() );
323   
324    osg::ref_ptr<osg::Image> posterImage = 0;
325    if ( outputPoster )
326    {
327        posterImage = new osg::Image;
328        posterImage->allocateImage( posterWidth, posterHeight, 1, GL_RGBA, GL_UNSIGNED_BYTE );
329        printer->setFinalPoster( posterImage.get() );
330        printer->setOutputPosterName( posterName );
331    }
332   
333#if 0
334    // While recording sub-images of the poster, the scene will always be traversed twice, from its two
335    // parent node: root and camera. Sometimes this may not be so comfortable.
336    // To prevent this behaviour, we can use a switch node to enable one parent and disable the other.
337    // However, the solution also needs to be used with care, as the window will go blank while taking
338    // snapshots and recover later.
339    osg::ref_ptr<osg::Switch> root = new osg::Switch;
340    root->addChild( scene, true );
341    root->addChild( camera.get(), false );
342#else
343    osg::ref_ptr<osg::Group> root = new osg::Group;
344    root->addChild( scene );
345    root->addChild( camera.get() );
346#endif
347   
348    osgViewer::Viewer viewer;
349    viewer.setSceneData( root.get() );
350    viewer.getDatabasePager()->setDoPreCompile( false );
351   
352    if ( renderImplementation==osg::Camera::FRAME_BUFFER )
353    {
354        // FRAME_BUFFER requires the window resolution equal or greater than the to-be-copied size
355        viewer.setUpViewInWindow( 100, 100, tileWidth, tileHeight );
356    }
357    else
358    {
359        // We want to see the console output, so just render in a window
360        viewer.setUpViewInWindow( 100, 100, 800, 600 );
361    }
362   
363    if ( activeMode )
364    {
365        viewer.addEventHandler( new PrintPosterHandler(printer.get()) );
366        viewer.addEventHandler( new osgViewer::StatsHandler );
367        viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
368        viewer.setCameraManipulator( new osgGA::TrackballManipulator );
369        viewer.run();
370    }
371    else
372    {
373        osg::Camera* camera = viewer.getCamera();
374        if ( !useLatLongHeight ) computeViewMatrix( camera, eye, hpr );
375        else computeViewMatrixOnEarth( camera, scene, latLongHeight, hpr );
376       
377        osg::ref_ptr<CustomRenderer> renderer = new CustomRenderer( camera );
378        camera->setRenderer( renderer.get() );
379        viewer.setThreadingModel( osgViewer::Viewer::SingleThreaded );
380       
381        // Realize and initiate the first PagedLOD request
382        viewer.realize();
383        viewer.frame();
384       
385        printer->init( camera );
386        while ( !printer->done() )
387        {
388            viewer.advance();
389           
390            // Keep updating and culling until full level of detail is reached
391            renderer->setCullOnly( true );
392            while ( viewer.getDatabasePager()->getRequestsInProgress() )
393            {
394                viewer.updateTraversal();
395                viewer.renderingTraversals();
396            }
397           
398            renderer->setCullOnly( false );
399            printer->frame( viewer.getFrameStamp(), viewer.getSceneData() );
400            viewer.renderingTraversals();
401        }
402    }
403    return 0;
404}
Note: See TracBrowser for help on using the browser.