| 1 | #include <osg/Group> |
|---|
| 2 | #include <osg/Notify> |
|---|
| 3 | #include <osg/Geometry> |
|---|
| 4 | #include <osg/ArgumentParser> |
|---|
| 5 | #include <osg/ApplicationUsage> |
|---|
| 6 | #include <osg/Texture2D> |
|---|
| 7 | #include <osg/Geode> |
|---|
| 8 | #include <osg/PagedLOD> |
|---|
| 9 | |
|---|
| 10 | #include <osgDB/Registry> |
|---|
| 11 | #include <osgDB/ReadFile> |
|---|
| 12 | #include <osgDB/WriteFile> |
|---|
| 13 | |
|---|
| 14 | #include <osgUtil/Optimizer> |
|---|
| 15 | |
|---|
| 16 | #include <iostream> |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 55 | geom->getOrCreateStateSet()->setTextureAttributeAndModes( |
|---|
| 56 | 0, |
|---|
| 57 | new osg::Texture2D(osgDB::readImageFile(imageFile)), |
|---|
| 58 | osg::StateAttribute::ON); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 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 | } |
|---|
| 86 | |
|---|
| 87 | class WriteOutPagedLODSubgraphsVistor : public osg::NodeVisitor |
|---|
| 88 | { |
|---|
| 89 | public: |
|---|
| 90 | WriteOutPagedLODSubgraphsVistor(): |
|---|
| 91 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 92 | { |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | virtual void apply(osg::PagedLOD& plod) |
|---|
| 96 | { |
|---|
| 97 | |
|---|
| 98 | for(unsigned int i=0;i<plod.getNumChildren();++i) |
|---|
| 99 | { |
|---|
| 100 | osg::Node* child = plod.getChild(i); |
|---|
| 101 | std::string filename = plod.getFileName(i); |
|---|
| 102 | if (!filename.empty()) osgDB::writeNodeFile(*child,filename); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | traverse(plod); |
|---|
| 106 | } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | int main( int argc, char **argv ) |
|---|
| 112 | { |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 119 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" creates a hierachy of files for paging which can be later loaded by viewers."); |
|---|
| 120 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 121 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 125 | { |
|---|
| 126 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 127 | return 1; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | if (arguments.errors()) |
|---|
| 135 | { |
|---|
| 136 | arguments.writeErrorMessages(std::cout); |
|---|
| 137 | return 1; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | osg::ref_ptr<osg::Node> model = createPagedModel(); |
|---|
| 148 | |
|---|
| 149 | if (model.valid()) |
|---|
| 150 | { |
|---|
| 151 | osgDB::writeNodeFile(*model,"level_0.osg"); |
|---|
| 152 | |
|---|
| 153 | WriteOutPagedLODSubgraphsVistor woplsv; |
|---|
| 154 | model->accept(woplsv); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | return 0; |
|---|
| 158 | } |
|---|