| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | #include <osgUtil/Optimizer> |
|---|
| 14 | #include <osgUtil/Simplifier> |
|---|
| 15 | #include <osgProducer/Viewer> |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class KeyboardEventHandler : public osgGA::GUIEventHandler |
|---|
| 19 | { |
|---|
| 20 | public: |
|---|
| 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 | |
|---|
| 54 | private: |
|---|
| 55 | |
|---|
| 56 | unsigned int& _flag; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | int main( int argc, char **argv ) |
|---|
| 61 | { |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 68 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" examples illustrates simplification of triangle meshes."); |
|---|
| 69 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 70 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 71 | arguments.getApplicationUsage()->addCommandLineOption("--ratio <ratio>","Specify the sample ratio","0.5]"); |
|---|
| 72 | arguments.getApplicationUsage()->addCommandLineOption("--max-error <error>","Specify the maximum error","4.0"); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | float sampleRatio = 0.5f; |
|---|
| 76 | float maxError = 4.0f; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | osgProducer::Viewer viewer(arguments); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | while (arguments.read("--ratio",sampleRatio)) {} |
|---|
| 89 | while (arguments.read("--max-error",maxError)) {} |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 93 | { |
|---|
| 94 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 95 | return 1; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | if (arguments.errors()) |
|---|
| 100 | { |
|---|
| 101 | arguments.writeErrorMessages(std::cout); |
|---|
| 102 | return 1; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | if (arguments.argc()<=1) |
|---|
| 106 | { |
|---|
| 107 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 108 | return 1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | if (!loadedModel) |
|---|
| 118 | { |
|---|
| 119 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 120 | return 1; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if (arguments.errors()) |
|---|
| 128 | { |
|---|
| 129 | arguments.writeErrorMessages(std::cout); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | osg::Timer_t end_tick = osg::Timer::instance()->tick(); |
|---|
| 133 | |
|---|
| 134 | std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl; |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | unsigned int keyFlag = 0; |
|---|
| 140 | viewer.getEventHandlerList().push_front(new KeyboardEventHandler(keyFlag)); |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | viewer.setSceneData(loadedModel.get()); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | viewer.realize(); |
|---|
| 147 | |
|---|
| 148 | float multiplier = 0.99f; |
|---|
| 149 | float minRatio = 0.001f; |
|---|
| 150 | float ratio = sampleRatio; |
|---|
| 151 | |
|---|
| 152 | while( !viewer.done() ) |
|---|
| 153 | { |
|---|
| 154 | |
|---|
| 155 | viewer.sync(); |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | viewer.update(); |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | viewer.frame(); |
|---|
| 163 | |
|---|
| 164 | if (keyFlag == 1 || keyFlag == 2) |
|---|
| 165 | { |
|---|
| 166 | if (keyFlag == 1) ratio *= multiplier; |
|---|
| 167 | if (keyFlag == 2) ratio /= multiplier; |
|---|
| 168 | if (ratio<minRatio) ratio=minRatio; |
|---|
| 169 | |
|---|
| 170 | osgUtil::Simplifier simplifier(ratio, maxError); |
|---|
| 171 | |
|---|
| 172 | std::cout<<"Runing osgUtil::Simplifier with SampleRatio="<<ratio<<" maxError="<<maxError<<" ..."; |
|---|
| 173 | std::cout.flush(); |
|---|
| 174 | |
|---|
| 175 | osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL); |
|---|
| 176 | |
|---|
| 177 | root->accept(simplifier); |
|---|
| 178 | |
|---|
| 179 | std::cout<<"done"<<std::endl; |
|---|
| 180 | |
|---|
| 181 | viewer.setSceneData(root.get()); |
|---|
| 182 | keyFlag = 0; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | viewer.sync(); |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | viewer.cleanup_frame(); |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | viewer.sync(); |
|---|
| 194 | |
|---|
| 195 | return 0; |
|---|
| 196 | } |
|---|