- Timestamp:
- 09/28/05 18:05:35 (8 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgsimplifier/osgsimplifier.cpp
r4393 r4552 11 11 12 12 #include <osgDB/ReadFile> 13 #include <osgDB/WriteFile>14 13 #include <osgUtil/Optimizer> 15 14 #include <osgUtil/Simplifier> 16 #include <osgUtil/TriStripVisitor>17 #include <osgUtil/Optimizer>18 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 19 59 20 60 int main( int argc, char **argv ) … … 51 91 return 1; 52 92 } 53 std::string outputFileName;54 while (arguments.read("-o",outputFileName)) {}55 93 56 94 // report any errors if they have occured when parsing the program aguments. … … 86 124 { 87 125 arguments.writeErrorMessages(std::cout); 88 return 1;89 126 } 90 127 91 osg::Timer_t end_ load_tick = osg::Timer::instance()->tick();128 osg::Timer_t end_tick = osg::Timer::instance()->tick(); 92 129 93 std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_ load_tick)<<std::endl;130 std::cout << "Time to load = "<<osg::Timer::instance()->delta_s(start_tick,end_tick)<<std::endl; 94 131 95 132 osgUtil::Simplifier simplifier(sampleRatio); 96 loadedModel->accept(simplifier); 133 simplifier.setSampleRatio(1.0f); 134 simplifier.setMaximumError(0.4f); 135 136 //loadedModel->accept(simplifier); 97 137 98 osg::Timer_t end_simplifier_tick = osg::Timer::instance()->tick(); 99 100 std::cout << "Time to simplify = "<<osg::Timer::instance()->delta_s(end_load_tick, end_simplifier_tick)<<std::endl; 101 102 osgUtil::TriStripVisitor tsv; 103 tsv.setMinStripSize(3); 104 loadedModel->accept(tsv); 105 tsv.stripify(); 106 107 osg::Timer_t end_tristrip_tick = osg::Timer::instance()->tick(); 108 109 std::cout << "Time to tri strip = "<<osg::Timer::instance()->delta_s(end_simplifier_tick, end_tristrip_tick)<<std::endl; 110 111 // run optimization over the scene graph 112 osgUtil::Optimizer optimzer; 113 optimzer.optimize(loadedModel.get()); 114 115 if (!outputFileName.empty()) 116 { 117 std::cout << "Writing out scene graph as '" << outputFileName << "'"<<std::endl; 118 osgDB::writeNodeFile(*loadedModel,outputFileName); 119 return 0; 120 } 138 unsigned int keyFlag = 0; 139 viewer.getEventHandlerList().push_front(new KeyboardEventHandler(keyFlag)); 121 140 122 141 // set the scene to render … … 125 144 // create the windows and run the threads. 126 145 viewer.realize(); 146 147 float multiplier = 0.99f; 148 float minRatio = 0.00f; 149 float ratio = 0.5f; 127 150 128 151 while( !viewer.done() ) … … 137 160 // fire off the cull and draw traversals of the scene. 138 161 viewer.frame(); 139 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 } 140 176 } 141 177
