| 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()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); |
|---|
| 69 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 70 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | float sampleRatio = 0.5f; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | osgProducer::Viewer viewer(arguments); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | viewer.getUsage(*arguments.getApplicationUsage()); |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | while (arguments.read("-s",sampleRatio)) {} |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 89 | { |
|---|
| 90 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 91 | return 1; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | if (arguments.errors()) |
|---|
| 96 | { |
|---|
| 97 | arguments.writeErrorMessages(std::cout); |
|---|
| 98 | return 1; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (arguments.argc()<=1) |
|---|
| 102 | { |
|---|
| 103 | arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); |
|---|
| 104 | return 1; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | if (!loadedModel) |
|---|
| 114 | { |
|---|
| 115 | std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; |
|---|
| 116 | return 1; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | if (arguments.errors()) |
|---|
| 124 | { |
|---|
| 125 | arguments.writeErrorMessages(std::cout); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | osg::Timer_t end_tick = osg::Timer::instance()->tick(); |
|---|
| 129 | |
|---|
| 130 | std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl; |
|---|
| 131 | |
|---|
| 132 | osgUtil::Simplifier simplifier(sampleRatio); |
|---|
| 133 | simplifier.setSampleRatio(1.0f); |
|---|
| 134 | simplifier.setMaximumError(0.4f); |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | unsigned int keyFlag = 0; |
|---|
| 139 | viewer.getEventHandlerList().push_front(new KeyboardEventHandler(keyFlag)); |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | viewer.setSceneData(loadedModel.get()); |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | viewer.realize(); |
|---|
| 146 | |
|---|
| 147 | float multiplier = 0.99f; |
|---|
| 148 | float minRatio = 0.00f; |
|---|
| 149 | float ratio = 0.5f; |
|---|
| 150 | |
|---|
| 151 | while( !viewer.done() ) |
|---|
| 152 | { |
|---|
| 153 | |
|---|
| 154 | viewer.sync(); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | viewer.update(); |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | viewer.frame(); |
|---|
| 162 | |
|---|
| 163 | if (keyFlag == 1 || keyFlag == 2) |
|---|
| 164 | { |
|---|
| 165 | if (keyFlag == 1) ratio *= multiplier; |
|---|
| 166 | if (keyFlag == 2) ratio /= multiplier; |
|---|
| 167 | if (ratio>1.0f) ratio=1.0f; |
|---|
| 168 | if (ratio<minRatio) ratio=minRatio; |
|---|
| 169 | |
|---|
| 170 | simplifier.setSampleRatio(ratio); |
|---|
| 171 | osg::ref_ptr<osg::Node> root = (osg::Node*)loadedModel->clone(osg::CopyOp::DEEP_COPY_ALL); |
|---|
| 172 | root->accept(simplifier); |
|---|
| 173 | viewer.setSceneData(root.get()); |
|---|
| 174 | keyFlag = 0; |
|---|
| 175 | } |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | viewer.sync(); |
|---|
| 180 | |
|---|
| 181 | return 0; |
|---|
| 182 | } |
|---|