- Timestamp:
- 07/14/05 23:04:40 (8 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgpagedlod/osgpagedlod.cpp
r4380 r4385 11 11 #include <osgDB/ReadFile> 12 12 #include <osgDB/WriteFile> 13 #include <osgDB/FileNameUtils> 13 14 14 15 #include <osgUtil/Optimizer> … … 16 17 #include <iostream> 17 18 #include <sstream> 19 20 class NameVistor : public osg::NodeVisitor 21 { 22 public: 23 NameVistor(): 24 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), 25 _count(0) 26 { 27 } 28 29 virtual void apply(osg::Node& node) 30 { 31 std::ostringstream os; 32 os << node.className() << "_"<<_count++; 33 34 node.setName(os.str()); 35 36 traverse(node); 37 } 38 39 unsigned int _count; 40 }; 41 42 class CheckVisitor : public osg::NodeVisitor 43 { 44 public: 45 CheckVisitor(): 46 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) 47 { 48 } 49 50 virtual void apply(osg::PagedLOD& plod) 51 { 52 std::cout<<"PagedLOD "<<plod.getName()<<" numRanges = "<< plod.getNumRanges()<<" numFiles = "<<plod.getNumFileNames()<<std::endl; 53 for(unsigned int i=0;i<plod.getNumFileNames();++i) 54 { 55 std::cout<<" files = '"<<plod.getFileName(i)<<"'"<<std::endl; 56 } 57 } 58 }; 59 18 60 19 61 class WriteOutPagedLODSubgraphsVistor : public osg::NodeVisitor … … 47 89 { 48 90 public: 49 ConvertToPageLODVistor(const std::string& basename, const std::string& extension ):91 ConvertToPageLODVistor(const std::string& basename, const std::string& extension, bool makeAllChildrenPaged): 50 92 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), 51 93 _basename(basename), 52 _extension(extension) 94 _extension(extension), 95 _makeAllChildrenPaged(makeAllChildrenPaged) 53 96 { 54 97 } … … 86 129 } 87 130 88 osg::notify(osg::NOTICE)<<"Converting LOD to PagedLOD"<<std::endl; 131 if (!_makeAllChildrenPaged && lod->getNumRanges()<2) 132 { 133 osg::notify(osg::NOTICE)<<"Leaving LOD with one child as is."<<std::endl; 134 break; 135 } 136 137 osg::notify(osg::NOTICE)<<"Converting LOD to PagedLOD."<<std::endl; 89 138 90 139 osg::PagedLOD* plod = new osg::PagedLOD; 91 140 92 141 const osg::LOD::RangeList& originalRangeList = lod->getRangeList(); 93 typedef std::m ap< osg::LOD::MinMaxPair , unsigned int > MinMaxPairMap;142 typedef std::multimap< osg::LOD::MinMaxPair , unsigned int > MinMaxPairMap; 94 143 MinMaxPairMap rangeMap; 95 144 unsigned int pos = 0; … … 98 147 ++ritr, ++pos) 99 148 { 100 rangeMap [*ritr] = pos;149 rangeMap.insert(std::multimap< osg::LOD::MinMaxPair , unsigned int >::value_type(*ritr, pos)); 101 150 } 102 151 … … 106 155 ++mitr, ++pos) 107 156 { 108 if (pos==0 )157 if (pos==0 && !_makeAllChildrenPaged) 109 158 { 110 159 plod->addChild(lod->getChild(mitr->second), mitr->first.first, mitr->first.second); 111 osg::notify(osg::NOTICE)<<" adding staight child"<<std::endl;112 160 } 113 161 else … … 118 166 119 167 plod->addChild(lod->getChild(mitr->second), mitr->first.first, mitr->first.second, os.str()); 120 osg::notify(osg::NOTICE)<<" adding tiled subgraph"<<os.str()<<std::endl;121 168 } 122 169 } … … 131 178 132 179 plod->setCenter(plod->getBound().center()); 133 plod->setCenter(plod->getBound().center());180 134 181 135 182 } … … 141 188 std::string _basename; 142 189 std::string _extension; 143 190 bool _makeAllChildrenPaged; 144 191 }; 145 192 … … 156 203 arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); 157 204 arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); 205 arguments.getApplicationUsage()->addCommandLineOption("-o","set the output file (defaults to output.ive)"); 206 arguments.getApplicationUsage()->addCommandLineOption("--makeAllChildrenPaged","Force all children of LOD to be written out as external PagedLOD children"); 158 207 159 208 // if user request help write it out to cout. … … 163 212 return 1; 164 213 } 214 215 std::string outputfile("output.ive"); 216 while (arguments.read("-o",outputfile)) {} 217 218 219 bool makeAllChildrenPaged = false; 220 while (arguments.read("--makeAllChildrenPaged")) { makeAllChildrenPaged = true; } 165 221 166 222 // any option left unread are converted into errors to write out later. … … 189 245 } 190 246 191 ConvertToPageLODVistor converter("tile",".ive"); 247 std::string basename( osgDB::getNameLessExtension(outputfile) ); 248 std::string ext = '.'+ osgDB::getFileExtension(outputfile); 249 250 ConvertToPageLODVistor converter(basename,ext, makeAllChildrenPaged); 192 251 model->accept(converter); 193 252 converter.convert(); 194 253 254 NameVistor nameNodes; 255 model->accept(nameNodes); 256 257 //CheckVisitor checkNodes; 258 //model->accept(checkNodes); 259 195 260 if (model.valid()) 196 261 { 197 osgDB::writeNodeFile(*model, "tile.ive");262 osgDB::writeNodeFile(*model,outputfile); 198 263 199 264 WriteOutPagedLODSubgraphsVistor woplsv;
