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

Revision 5959, 4.6 kB (checked in by robert, 8 years ago)

Ported follow examples to osgViewer:

osgshape
osgsimplifier
osgsimulation
osgslice
osgspacewarp
osgspheresegment
osgspotlight
osgstereoimage

  • 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 <osgViewer/Viewer>
16#include <osgGA/TrackballManipulator>
17#include <iostream>
18
19class KeyboardEventHandler : public osgGA::GUIEventHandler
20{
21public:
22   
23    KeyboardEventHandler(unsigned int& flag) : _flag(flag)
24    {}
25   
26    virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
27    {
28        switch(ea.getEventType())
29        {
30            case(osgGA::GUIEventAdapter::KEYDOWN):
31            {
32                if (ea.getKey()=='n')
33                {
34                    _flag = 1;
35                    return true;
36                }
37                if (ea.getKey()=='p')
38                {
39                    _flag = 2;
40                    return true;
41                }
42                break;
43            }
44            default:
45                break;
46        }
47        return false;
48    }
49
50private:
51
52    unsigned int& _flag;
53};
54
55
56int main( int argc, char **argv )
57{
58
59    // use an ArgumentParser object to manage the program arguments.
60    osg::ArgumentParser arguments(&argc,argv);
61   
62    // set up the usage document, in case we need to print out how to use this program.
63    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
64    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes.");
65    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
66    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
67    arguments.getApplicationUsage()->addCommandLineOption("--ratio <ratio>","Specify the sample ratio","0.5]");
68    arguments.getApplicationUsage()->addCommandLineOption("--max-error <error>","Specify the maximum error","4.0");
69   
70
71    float sampleRatio = 0.5f;
72    float maxError = 4.0f;
73
74    // construct the viewer.
75    osgViewer::Viewer viewer;
76
77    // read the sample ratio if one is supplied
78    while (arguments.read("--ratio",sampleRatio)) {}
79    while (arguments.read("--max-error",maxError)) {}
80
81    // if user request help write it out to cout.
82    if (arguments.read("-h") || arguments.read("--help"))
83    {
84        arguments.getApplicationUsage()->write(std::cout);
85        return 1;
86    }
87
88    if (arguments.argc()<=1)
89    {
90        arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION);
91        return 1;
92    }
93
94    // read the scene from the list of file specified commandline args.
95    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
96
97    // if no model has been successfully loaded report failure.
98    if (!loadedModel)
99    {
100        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
101        return 1;
102    }
103   
104    //loadedModel->accept(simplifier);
105
106    unsigned int keyFlag = 0;
107    viewer.addEventHandler(new KeyboardEventHandler(keyFlag));
108
109    // set the scene to render
110    viewer.setSceneData(loadedModel.get());
111
112    viewer.setCameraManipulator(new osgGA::TrackballManipulator());
113
114    // create the windows and run the threads.
115    viewer.realize();
116
117    float multiplier = 0.8f;
118    float minRatio = 0.001f;
119    float ratio = sampleRatio;
120
121
122    while( !viewer.done() )
123    {
124        // fire off the cull and draw traversals of the scene.
125        viewer.frame();
126   
127        if (keyFlag == 1 || keyFlag == 2)
128        {
129            if (keyFlag == 1) ratio *= multiplier;
130            if (keyFlag == 2) ratio /= multiplier;
131            if (ratio<minRatio) ratio=minRatio;
132           
133            osgUtil::Simplifier simplifier(ratio, maxError);
134
135            std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ...";
136            std::cout.flush();
137           
138            osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL);
139
140            root->accept(simplifier);
141           
142            std::cout<<"done"<<std::endl;
143           
144            viewer.setSceneData(root.get());
145            keyFlag = 0;
146        }
147    }
148   
149    return 0;
150}
Note: See TracBrowser for help on using the browser.