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

Revision 12529, 5.2 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[6941]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.
[2898]17*/
18
19#include <osgDB/ReadFile>
20#include <osgUtil/Optimizer>
21#include <osgUtil/Simplifier>
[5959]22#include <osgViewer/Viewer>
23#include <osgGA/TrackballManipulator>
24#include <iostream>
[2898]25
[4552]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
[2898]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());
[4780]71    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes.");
[2898]72    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
73    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
[4685]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");
[2898]76   
77
78    float sampleRatio = 0.5f;
[4685]79    float maxError = 4.0f;
[2898]80
81    // construct the viewer.
[5959]82    osgViewer::Viewer viewer;
[2898]83
84    // read the sample ratio if one is supplied
[4685]85    while (arguments.read("--ratio",sampleRatio)) {}
86    while (arguments.read("--max-error",maxError)) {}
[2898]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);
[6916]103 
104    // if not loaded assume no arguments passed in, try use default mode instead.
[12529]105    if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osgt");
[6916]106   
[2898]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    }
[4552]113   
114    //loadedModel->accept(simplifier);
[2898]115
[4552]116    unsigned int keyFlag = 0;
[5959]117    viewer.addEventHandler(new KeyboardEventHandler(keyFlag));
[4367]118
[2898]119    // set the scene to render
120    viewer.setSceneData(loadedModel.get());
121
[5959]122    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
123
[2898]124    // create the windows and run the threads.
125    viewer.realize();
126
[5959]127    float multiplier = 0.8f;
[4685]128    float minRatio = 0.001f;
129    float ratio = sampleRatio;
[4552]130
[5959]131
[2898]132    while( !viewer.done() )
133    {
134        // fire off the cull and draw traversals of the scene.
135        viewer.frame();
[4552]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           
[4979]143            osgUtil::Simplifier simplifier(ratio, maxError);
[4685]144
145            std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ...";
146            std::cout.flush();
147           
[4552]148            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
[4685]149
[4552]150            root->accept(simplifier);
[4685]151           
152            std::cout<<"done"<<std::endl;
153           
[4552]154            viewer.setSceneData(root.get());
155            keyFlag = 0;
156        }
[2898]157    }
158   
159    return 0;
160}
Note: See TracBrowser for help on using the browser.