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

Revision 5636, 5.9 kB (checked in by robert, 8 years ago)

Removed deprecated GUIEventHandler method

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