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

Revision 6916, 4.7 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, added fallbacks for when now command line args are provided.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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 <osgUtil/Simplifier>
15#include <osgViewer/Viewer>
16#include <osgGA/TrackballManipulator>
17#include <iostream>
18
19class KeyboardEventHandler : public osgGA::GUIEventHandler
20{
21public:
22   
23    KeyboardEventHandler(unsigned int& flag) : _flag(flag)
24    {}
25   
26    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
27    {
28        switch(ea.getEventType())
29        {
30            case(osgGA::GUIEventAdapter::KEYDOWN):
31            {
32                if (ea.getKey()=='n')
33                {
34                    _flag = 1;
35                    return true;
36                }
37                if (ea.getKey()=='p')
38                {
39                    _flag = 2;
40                    return true;
41                }
42                break;
43            }
44            default:
45                break;
46        }
47        return false;
48    }
49
50private:
51
52    unsigned int& _flag;
53};
54
55
56int main( int argc, char **argv )
57{
58
59    // use an ArgumentParser object to manage the program arguments.
60    osg::ArgumentParser arguments(&argc,argv);
61   
62    // set up the usage document, in case we need to print out how to use this program.
63    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
64    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes.");
65    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
66    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
67    arguments.getApplicationUsage()->addCommandLineOption("--ratio <ratio>","Specify the sample ratio","0.5]");
68    arguments.getApplicationUsage()->addCommandLineOption("--max-error <error>","Specify the maximum error","4.0");
69   
70
71    float sampleRatio = 0.5f;
72    float maxError = 4.0f;
73
74    // construct the viewer.
75    osgViewer::Viewer viewer;
76
77    // read the sample ratio if one is supplied
78    while (arguments.read("--ratio",sampleRatio)) {}
79    while (arguments.read("--max-error",maxError)) {}
80
81    // if user request help write it out to cout.
82    if (arguments.read("-h") || arguments.read("--help"))
83    {
84        arguments.getApplicationUsage()->write(std::cout);
85        return 1;
86    }
87
88    if (arguments.argc()<=1)
89    {
90        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
91        return 1;
92    }
93
94    // read the scene from the list of file specified commandline args.
95    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
96 
97    // if not loaded assume no arguments passed in, try use default mode instead.
98    if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg");
99   
100    // if no model has been successfully loaded report failure.
101    if (!loadedModel)
102    {
103        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
104        return 1;
105    }
106   
107    //loadedModel->accept(simplifier);
108
109    unsigned int keyFlag = 0;
110    viewer.addEventHandler(new KeyboardEventHandler(keyFlag));
111
112    // set the scene to render
113    viewer.setSceneData(loadedModel.get());
114
115    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
116
117    // create the windows and run the threads.
118    viewer.realize();
119
120    float multiplier = 0.8f;
121    float minRatio = 0.001f;
122    float ratio = sampleRatio;
123
124
125    while( !viewer.done() )
126    {
127        // fire off the cull and draw traversals of the scene.
128        viewer.frame();
129   
130        if (keyFlag == 1 || keyFlag == 2)
131        {
132            if (keyFlag == 1) ratio *= multiplier;
133            if (keyFlag == 2) ratio /= multiplier;
134            if (ratio<minRatio) ratio=minRatio;
135           
136            osgUtil::Simplifier simplifier(ratio, maxError);
137
138            std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ...";
139            std::cout.flush();
140           
141            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
142
143            root->accept(simplifier);
144           
145            std::cout<<"done"<<std::endl;
146           
147            viewer.setSceneData(root.get());
148            keyFlag = 0;
149        }
150    }
151   
152    return 0;
153}
Note: See TracBrowser for help on using the browser.