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

Revision 6941, 5.2 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgsimplifier.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgDB/ReadFile>
20#include <osgUtil/Optimizer>
21#include <osgUtil/Simplifier>
22#include <osgViewer/Viewer>
23#include <osgGA/TrackballManipulator>
24#include <iostream>
25
26class KeyboardEventHandler : public osgGA::GUIEventHandler
27{
28public:
29   
30    KeyboardEventHandler(unsigned int& flag) : _flag(flag)
31    {}
32   
33    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
34    {
35        switch(ea.getEventType())
36        {
37            case(osgGA::GUIEventAdapter::KEYDOWN):
38            {
39                if (ea.getKey()=='n')
40                {
41                    _flag = 1;
42                    return true;
43                }
44                if (ea.getKey()=='p')
45                {
46                    _flag = 2;
47                    return true;
48                }
49                break;
50            }
51            default:
52                break;
53        }
54        return false;
55    }
56
57private:
58
59    unsigned int& _flag;
60};
61
62
63int main( int argc, char **argv )
64{
65
66    // use an ArgumentParser object to manage the program arguments.
67    osg::ArgumentParser arguments(&argc,argv);
68   
69    // set up the usage document, in case we need to print out how to use this program.
70    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
71    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes.");
72    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
73    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
74    arguments.getApplicationUsage()->addCommandLineOption("--ratio <ratio>","Specify the sample ratio","0.5]");
75    arguments.getApplicationUsage()->addCommandLineOption("--max-error <error>","Specify the maximum error","4.0");
76   
77
78    float sampleRatio = 0.5f;
79    float maxError = 4.0f;
80
81    // construct the viewer.
82    osgViewer::Viewer viewer;
83
84    // read the sample ratio if one is supplied
85    while (arguments.read("--ratio",sampleRatio)) {}
86    while (arguments.read("--max-error",maxError)) {}
87
88    // if user request help write it out to cout.
89    if (arguments.read("-h") || arguments.read("--help"))
90    {
91        arguments.getApplicationUsage()->write(std::cout);
92        return 1;
93    }
94
95    if (arguments.argc()<=1)
96    {
97        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
98        return 1;
99    }
100
101    // read the scene from the list of file specified commandline args.
102    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
103 
104    // if not loaded assume no arguments passed in, try use default mode instead.
105    if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg");
106   
107    // if no model has been successfully loaded report failure.
108    if (!loadedModel)
109    {
110        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
111        return 1;
112    }
113   
114    //loadedModel->accept(simplifier);
115
116    unsigned int keyFlag = 0;
117    viewer.addEventHandler(new KeyboardEventHandler(keyFlag));
118
119    // set the scene to render
120    viewer.setSceneData(loadedModel.get());
121
122    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
123
124    // create the windows and run the threads.
125    viewer.realize();
126
127    float multiplier = 0.8f;
128    float minRatio = 0.001f;
129    float ratio = sampleRatio;
130
131
132    while( !viewer.done() )
133    {
134        // fire off the cull and draw traversals of the scene.
135        viewer.frame();
136   
137        if (keyFlag == 1 || keyFlag == 2)
138        {
139            if (keyFlag == 1) ratio *= multiplier;
140            if (keyFlag == 2) ratio /= multiplier;
141            if (ratio<minRatio) ratio=minRatio;
142           
143            osgUtil::Simplifier simplifier(ratio, maxError);
144
145            std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ...";
146            std::cout.flush();
147           
148            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
149
150            root->accept(simplifier);
151           
152            std::cout<<"done"<<std::endl;
153           
154            viewer.setSceneData(root.get());
155            keyFlag = 0;
156        }
157    }
158   
159    return 0;
160}
Note: See TracBrowser for help on using the browser.