root/OpenSceneGraph/trunk/examples/osgsimplifier/osgsimplifier.cpp @ 2898

Revision 2898, 3.9 kB (checked in by robert, 10 years ago)

Added osgsimplifier example.

  • 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 <osgUtil/Simplifier>
15#include <osgProducer/Viewer>
16
17int main( int argc, char **argv )
18{
19
20    // use an ArgumentParser object to manage the program arguments.
21    osg::ArgumentParser arguments(&argc,argv);
22   
23    // set up the usage document, in case we need to print out how to use this program.
24    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
25    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
26    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
27    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
28   
29
30    float sampleRatio = 0.5f;
31
32    // construct the viewer.
33    osgProducer::Viewer viewer(arguments);
34
35    // set up the value with sensible default event handlers.
36    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
37
38    // get details on keyboard and mouse bindings used by the viewer.
39    viewer.getUsage(*arguments.getApplicationUsage());
40
41    // read the sample ratio if one is supplied
42    while (arguments.read("-s",sampleRatio)) {}
43
44    // if user request help write it out to cout.
45    if (arguments.read("-h") || arguments.read("--help"))
46    {
47        arguments.getApplicationUsage()->write(std::cout);
48        return 1;
49    }
50
51    // report any errors if they have occured when parsing the program aguments.
52    if (arguments.errors())
53    {
54        arguments.writeErrorMessages(std::cout);
55        return 1;
56    }
57   
58    if (arguments.argc()<=1)
59    {
60        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
61        return 1;
62    }
63
64    osg::Timer_t start_tick = osg::Timer::instance()->tick();
65
66    // read the scene from the list of file specified commandline args.
67    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
68
69    // if no model has been successfully loaded report failure.
70    if (!loadedModel)
71    {
72        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
73        return 1;
74    }
75
76    // any option left unread are converted into errors to write out later.
77    arguments.reportRemainingOptionsAsUnrecognized();
78
79    // report any errors if they have occured when parsing the program aguments.
80    if (arguments.errors())
81    {
82        arguments.writeErrorMessages(std::cout);
83    }
84
85    osg::Timer_t end_tick = osg::Timer::instance()->tick();
86
87    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
88
89    osgUtil::Simplifier simplifier(sampleRatio);
90   
91    loadedModel->accept(simplifier);
92
93    // set the scene to render
94    viewer.setSceneData(loadedModel.get());
95
96    // create the windows and run the threads.
97    viewer.realize();
98
99    while( !viewer.done() )
100    {
101        // wait for all cull and draw threads to complete.
102        viewer.sync();
103
104        // update the scene by traversing it with the the update visitor which will
105        // call all node update callbacks and animations.
106        viewer.update();
107         
108        // fire off the cull and draw traversals of the scene.
109        viewer.frame();
110       
111    }
112   
113    // wait for all cull and draw threads to complete before exit.
114    viewer.sync();
115
116    return 0;
117}
Note: See TracBrowser for help on using the browser.