| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 26 | class KeyboardEventHandler : public osgGA::GUIEventHandler |
|---|
| 27 | { |
|---|
| 28 | public: |
|---|
| 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 | |
|---|
| 57 | private: |
|---|
| 58 | |
|---|
| 59 | unsigned int& _flag; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | int main( int argc, char **argv ) |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 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 | |
|---|
| 82 | osgViewer::Viewer viewer; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | while (arguments.read("--ratio",sampleRatio)) {} |
|---|
| 86 | while (arguments.read("--max-error",maxError)) {} |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 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 | |
|---|
| 102 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osgt"); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | if (!loadedModel) |
|---|
| 109 | { |
|---|
| 110 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 111 | return 1; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | unsigned int keyFlag = 0; |
|---|
| 117 | viewer.addEventHandler(new KeyboardEventHandler(keyFlag)); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | viewer.setSceneData(loadedModel.get()); |
|---|
| 121 | |
|---|
| 122 | viewer.setCameraManipulator(new osgGA::TrackballManipulator()); |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 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 | |
|---|
| 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 | } |
|---|