| 17 | | |
| 18 | | osg::Geode* createTile(const osg::Vec3& lb, const osg::Vec3& rb, |
| 19 | | const osg::Vec3& lt, const osg::Vec3& rt, |
| 20 | | const std::string& imageFile) |
| 21 | | { |
| 22 | | |
| 23 | | // create the geometry. |
| 24 | | osg::Geometry* geom = new osg::Geometry; |
| 25 | | |
| 26 | | osg::Vec3Array* coords = new osg::Vec3Array(4); |
| 27 | | (*coords)[0] = lt; |
| 28 | | (*coords)[1] = lb; |
| 29 | | (*coords)[2] = rb; |
| 30 | | (*coords)[3] = rt; |
| 31 | | geom->setVertexArray(coords); |
| 32 | | |
| 33 | | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
| 34 | | (*tcoords)[0].set(0.0f,1.0f); |
| 35 | | (*tcoords)[1].set(0.0f,0.0f); |
| 36 | | (*tcoords)[2].set(1.0f,0.0f); |
| 37 | | (*tcoords)[3].set(1.0f,1.0f); |
| 38 | | geom->setTexCoordArray(0,tcoords); |
| 39 | | |
| 40 | | osg::Vec4Array* colours = new osg::Vec4Array(1); |
| 41 | | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
| 42 | | geom->setColorArray(colours); |
| 43 | | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
| 44 | | |
| 45 | | osg::Vec3Array* normals = new osg::Vec3Array(1); |
| 46 | | (*normals)[0] = (rb-lb)^(lt-lb); |
| 47 | | (*normals)[0].normalize(); |
| 48 | | geom->setNormalArray(normals); |
| 49 | | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
| 50 | | |
| 51 | | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
| 52 | | |
| 53 | | |
| 54 | | // add the texture to the geometry. |
| 55 | | geom->getOrCreateStateSet()->setTextureAttributeAndModes( |
| 56 | | 0, // texture unit 0. |
| 57 | | new osg::Texture2D(osgDB::readImageFile(imageFile)), |
| 58 | | osg::StateAttribute::ON); |
| 59 | | |
| 60 | | |
| 61 | | // create the geode. |
| 62 | | osg::Geode* geode = new osg::Geode; |
| 63 | | geode->addDrawable(geom); |
| 64 | | |
| 65 | | return geode; |
| 66 | | |
| 67 | | } |
| 68 | | |
| 69 | | |
| 70 | | osg::Node* createPagedModel() |
| 71 | | { |
| 72 | | osg::PagedLOD* level_0 = new osg::PagedLOD; |
| 73 | | |
| 74 | | float distance = 1000.0f; |
| 75 | | |
| 76 | | osg::Vec3 lb(0.0f,0.0f,0.0f); |
| 77 | | osg::Vec3 rb(distance,0.0f,0.0f); |
| 78 | | osg::Vec3 rt(distance,0.0f,distance); |
| 79 | | osg::Vec3 lt(0.0f,0.0f,distance); |
| 80 | | |
| 81 | | level_0->addChild(createTile(lb,rb,lt,rt,"lz.rgb"),distance,1e5,""); |
| 82 | | level_0->addChild(createTile(lb,rb,lt,rt,"land_shallow_topo_2048.jpg"),0,distance,"level_1.osg"); |
| 83 | | |
| 84 | | return level_0; |
| 85 | | } |
| | 17 | #include <sstream> |
| | 46 | class ConvertToPageLODVistor : public osg::NodeVisitor |
| | 47 | { |
| | 48 | public: |
| | 49 | ConvertToPageLODVistor(const std::string& basename, const std::string& extension): |
| | 50 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
| | 51 | _basename(basename), |
| | 52 | _extension(extension) |
| | 53 | { |
| | 54 | } |
| | 55 | |
| | 56 | virtual ~ConvertToPageLODVistor() |
| | 57 | { |
| | 58 | } |
| | 59 | |
| | 60 | virtual void apply(osg::LOD& lod) |
| | 61 | { |
| | 62 | _lodSet.insert(&lod); |
| | 63 | |
| | 64 | traverse(lod); |
| | 65 | } |
| | 66 | |
| | 67 | virtual void apply(osg::PagedLOD& plod) |
| | 68 | { |
| | 69 | // do thing, but want to avoid call LOD. |
| | 70 | traverse(plod); |
| | 71 | } |
| | 72 | |
| | 73 | void convert() |
| | 74 | { |
| | 75 | unsigned int lodNum = 0; |
| | 76 | for(LODSet::iterator itr = _lodSet.begin(); |
| | 77 | itr != _lodSet.end(); |
| | 78 | ++itr, ++lodNum) |
| | 79 | { |
| | 80 | osg::ref_ptr<osg::LOD> lod = const_cast<osg::LOD*>(itr->get()); |
| | 81 | |
| | 82 | if (lod->getNumParents()==0) |
| | 83 | { |
| | 84 | osg::notify(osg::NOTICE)<<"Warning can't operator on root node."<<std::endl; |
| | 85 | break; |
| | 86 | } |
| | 87 | |
| | 88 | osg::notify(osg::NOTICE)<<"Converting LOD to PagedLOD"<<std::endl; |
| | 89 | |
| | 90 | osg::PagedLOD* plod = new osg::PagedLOD; |
| | 91 | |
| | 92 | const osg::LOD::RangeList& originalRangeList = lod->getRangeList(); |
| | 93 | typedef std::map< osg::LOD::MinMaxPair , unsigned int > MinMaxPairMap; |
| | 94 | MinMaxPairMap rangeMap; |
| | 95 | unsigned int pos = 0; |
| | 96 | for(osg::LOD::RangeList::const_iterator ritr = originalRangeList.begin(); |
| | 97 | ritr != originalRangeList.end(); |
| | 98 | ++ritr, ++pos) |
| | 99 | { |
| | 100 | rangeMap[*ritr] = pos; |
| | 101 | } |
| | 102 | |
| | 103 | pos = 0; |
| | 104 | for(MinMaxPairMap::reverse_iterator mitr = rangeMap.rbegin(); |
| | 105 | mitr != rangeMap.rend(); |
| | 106 | ++mitr, ++pos) |
| | 107 | { |
| | 108 | if (pos==0) |
| | 109 | { |
| | 110 | plod->addChild(lod->getChild(mitr->second), mitr->first.first, mitr->first.second); |
| | 111 | osg::notify(osg::NOTICE)<<" adding staight child"<<std::endl; |
| | 112 | } |
| | 113 | else |
| | 114 | { |
| | 115 | std::string filename = _basename; |
| | 116 | std::ostringstream os; |
| | 117 | os << _basename << "_"<<lodNum<<"_"<<pos<<_extension; |
| | 118 | |
| | 119 | 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 | } |
| | 122 | } |
| | 123 | |
| | 124 | osg::Node::ParentList parents = lod->getParents(); |
| | 125 | for(osg::Node::ParentList::iterator pitr=parents.begin(); |
| | 126 | pitr!=parents.end(); |
| | 127 | ++pitr) |
| | 128 | { |
| | 129 | (*pitr)->replaceChild(lod.get(),plod); |
| | 130 | } |
| | 131 | |
| | 132 | plod->setCenter(plod->getBound().center()); |
| | 133 | plod->setCenter(plod->getBound().center()); |
| | 134 | |
| | 135 | } |
| | 136 | } |
| | 137 | |
| | 138 | |
| | 139 | typedef std::set< osg::ref_ptr<osg::LOD> > LODSet; |
| | 140 | LODSet _lodSet; |
| | 141 | std::string _basename; |
| | 142 | std::string _extension; |
| | 143 | |
| | 144 | }; |