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

Revision 4979, 5.9 kB (checked in by robert, 9 years ago)

Added support for up sampling by dividing longest edges.

  • 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
17
18class KeyboardEventHandler : public osgGA::GUIEventHandler
19{
20public:
21   
22    KeyboardEventHandler(unsigned int& flag) : _flag(flag)
23    {}
24   
25    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
26    {
27        switch(ea.getEventType())
28        {
29            case(osgGA::GUIEventAdapter::KEYDOWN):
30            {
31                if (ea.getKey()=='n')
32                {
33                    _flag = 1;
34                    return true;
35                }
36                if (ea.getKey()=='p')
37                {
38                    _flag = 2;
39                    return true;
40                }
41                break;
42            }
43            default:
44                break;
45        }
46        return false;
47    }
48
49    virtual void accept(osgGA::GUIEventHandlerVisitor& v)
50    {
51        v.visit(*this);
52    }
53
54private:
55
56    unsigned int& _flag;
57};
58
59
60int main( int argc, char **argv )
61{
62
63    // use an ArgumentParser object to manage the program arguments.
64    osg::ArgumentParser arguments(&argc,argv);
65   
66    // set up the usage document, in case we need to print out how to use this program.
67    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
68    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes.");
69    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
70    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
71    arguments.getApplicationUsage()->addCommandLineOption("--ratio <ratio>","Specify the sample ratio","0.5]");
72    arguments.getApplicationUsage()->addCommandLineOption("--max-error <error>","Specify the maximum error","4.0");
73   
74
75    float sampleRatio = 0.5f;
76    float maxError = 4.0f;
77
78    // construct the viewer.
79    osgProducer::Viewer viewer(arguments);
80
81    // set up the value with sensible default event handlers.
82    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
83
84    // get details on keyboard and mouse bindings used by the viewer.
85    viewer.getUsage(*arguments.getApplicationUsage());
86
87    // read the sample ratio if one is supplied
88    while (arguments.read("--ratio",sampleRatio)) {}
89    while (arguments.read("--max-error",maxError)) {}
90
91    // if user request help write it out to cout.
92    if (arguments.read("-h") || arguments.read("--help"))
93    {
94        arguments.getApplicationUsage()->write(std::cout);
95        return 1;
96    }
97
98    // report any errors if they have occured when parsing the program aguments.
99    if (arguments.errors())
100    {
101        arguments.writeErrorMessages(std::cout);
102        return 1;
103    }
104   
105    if (arguments.argc()<=1)
106    {
107        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
108        return 1;
109    }
110
111    osg::Timer_t start_tick = osg::Timer::instance()->tick();
112
113    // read the scene from the list of file specified commandline args.
114    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
115
116    // if no model has been successfully loaded report failure.
117    if (!loadedModel)
118    {
119        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
120        return 1;
121    }
122
123    // any option left unread are converted into errors to write out later.
124    arguments.reportRemainingOptionsAsUnrecognized();
125
126    // report any errors if they have occured when parsing the program aguments.
127    if (arguments.errors())
128    {
129        arguments.writeErrorMessages(std::cout);
130    }
131
132    osg::Timer_t end_tick = osg::Timer::instance()->tick();
133
134    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
135
136   
137    //loadedModel->accept(simplifier);
138
139    unsigned int keyFlag = 0;
140    viewer.getEventHandlerList().push_front(new KeyboardEventHandler(keyFlag));
141
142    // set the scene to render
143    viewer.setSceneData(loadedModel.get());
144
145    // create the windows and run the threads.
146    viewer.realize();
147
148    float multiplier = 0.99f;
149    float minRatio = 0.001f;
150    float ratio = sampleRatio;
151
152    while( !viewer.done() )
153    {
154        // wait for all cull and draw threads to complete.
155        viewer.sync();
156
157        // update the scene by traversing it with the the update visitor which will
158        // call all node update callbacks and animations.
159        viewer.update();
160         
161        // fire off the cull and draw traversals of the scene.
162        viewer.frame();
163   
164        if (keyFlag == 1 || keyFlag == 2)
165        {
166            if (keyFlag == 1) ratio *= multiplier;
167            if (keyFlag == 2) ratio /= multiplier;
168            if (ratio<minRatio) ratio=minRatio;
169           
170            osgUtil::Simplifier simplifier(ratio, maxError);
171
172            std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ...";
173            std::cout.flush();
174           
175            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
176
177            root->accept(simplifier);
178           
179            std::cout<<"done"<<std::endl;
180           
181            viewer.setSceneData(root.get());
182            keyFlag = 0;
183        }
184    }
185   
186    // wait for all cull and draw threads to complete before exit.
187    viewer.sync();
188
189    return 0;
190}
Note: See TracBrowser for help on using the browser.