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

Revision 4393, 4.9 kB (checked in by robert, 9 years ago)

Added return 1.

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