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

Revision 4552, 5.5 kB (checked in by robert, 9 years ago)

From Volker Walkiewicz (with tweaks from Robert Osfield):

Fix to the update of the error metrics on the edges in the simplification mesh that are affected by an edge collapse.

Addition of 'n' and 'p' keyboard control in osgsimplifier example to allow users to control the sample ratio manually.

  • 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()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
69    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
70    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
71   
72
73    float sampleRatio = 0.5f;
74
75    // construct the viewer.
76    osgProducer::Viewer viewer(arguments);
77
78    // set up the value with sensible default event handlers.
79    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
80
81    // get details on keyboard and mouse bindings used by the viewer.
82    viewer.getUsage(*arguments.getApplicationUsage());
83
84    // read the sample ratio if one is supplied
85    while (arguments.read("-s",sampleRatio)) {}
86
87    // if user request help write it out to cout.
88    if (arguments.read("-h") || arguments.read("--help"))
89    {
90        arguments.getApplicationUsage()->write(std::cout);
91        return 1;
92    }
93
94    // report any errors if they have occured when parsing the program aguments.
95    if (arguments.errors())
96    {
97        arguments.writeErrorMessages(std::cout);
98        return 1;
99    }
100   
101    if (arguments.argc()<=1)
102    {
103        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
104        return 1;
105    }
106
107    osg::Timer_t start_tick = osg::Timer::instance()->tick();
108
109    // read the scene from the list of file specified commandline args.
110    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
111
112    // if no model has been successfully loaded report failure.
113    if (!loadedModel)
114    {
115        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
116        return 1;
117    }
118
119    // any option left unread are converted into errors to write out later.
120    arguments.reportRemainingOptionsAsUnrecognized();
121
122    // report any errors if they have occured when parsing the program aguments.
123    if (arguments.errors())
124    {
125        arguments.writeErrorMessages(std::cout);
126    }
127
128    osg::Timer_t end_tick = osg::Timer::instance()->tick();
129
130    std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl;
131
132    osgUtil::Simplifier simplifier(sampleRatio);
133    simplifier.setSampleRatio(1.0f);
134    simplifier.setMaximumError(0.4f);
135   
136    //loadedModel->accept(simplifier);
137
138    unsigned int keyFlag = 0;
139    viewer.getEventHandlerList().push_front(new KeyboardEventHandler(keyFlag));
140
141    // set the scene to render
142    viewer.setSceneData(loadedModel.get());
143
144    // create the windows and run the threads.
145    viewer.realize();
146
147    float multiplier = 0.99f;
148    float minRatio = 0.00f;
149    float ratio = 0.5f;
150
151    while( !viewer.done() )
152    {
153        // wait for all cull and draw threads to complete.
154        viewer.sync();
155
156        // update the scene by traversing it with the the update visitor which will
157        // call all node update callbacks and animations.
158        viewer.update();
159         
160        // fire off the cull and draw traversals of the scene.
161        viewer.frame();
162   
163        if (keyFlag == 1 || keyFlag == 2)
164        {
165            if (keyFlag == 1) ratio *= multiplier;
166            if (keyFlag == 2) ratio /= multiplier;
167            if (ratio>1.0f) ratio=1.0f;
168            if (ratio<minRatio) ratio=minRatio;
169           
170            simplifier.setSampleRatio(ratio);
171            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
172            root->accept(simplifier);
173            viewer.setSceneData(root.get());
174            keyFlag = 0;
175        }
176    }
177   
178    // wait for all cull and draw threads to complete before exit.
179    viewer.sync();
180
181    return 0;
182}
Note: See TracBrowser for help on using the browser.