| 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 | osg::Geode* createTile(const osg::Vec3& lb, const osg::Vec3& rb, |
|---|
| 17 | const osg::Vec3& lt, const osg::Vec3& rt, |
|---|
| 18 | const std::string& imageFile) |
|---|
| 19 | { |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 23 | |
|---|
| 24 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 25 | (*coords)[0] = lt; |
|---|
| 26 | (*coords)[1] = lb; |
|---|
| 27 | (*coords)[2] = rb; |
|---|
| 28 | (*coords)[3] = rt; |
|---|
| 29 | geom->setVertexArray(coords); |
|---|
| 30 | |
|---|
| 31 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 32 | (*tcoords)[0].set(0.0f,1.0f); |
|---|
| 33 | (*tcoords)[1].set(0.0f,0.0f); |
|---|
| 34 | (*tcoords)[2].set(1.0f,0.0f); |
|---|
| 35 | (*tcoords)[3].set(1.0f,1.0f); |
|---|
| 36 | geom->setTexCoordArray(0,tcoords); |
|---|
| 37 | |
|---|
| 38 | osg::Vec4Array* colours = new osg::Vec4Array(1); |
|---|
| 39 | (*colours)[0].set(1.0f,1.0f,1.0,1.0f); |
|---|
| 40 | geom->setColorArray(colours); |
|---|
| 41 | geom->setColorBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 42 | |
|---|
| 43 | osg::Vec3Array* normals = new osg::Vec3Array(1); |
|---|
| 44 | (*normals)[0] = (rb-lb)^(lt-lb); |
|---|
| 45 | (*normals)[0].normalize(); |
|---|
| 46 | geom->setNormalArray(normals); |
|---|
| 47 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 48 | |
|---|
| 49 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | geom->getOrCreateStateSet()->setTextureAttributeAndModes( |
|---|
| 54 | 0, |
|---|
| 55 | new osg::Texture2D(osgDB::readImageFile(imageFile)), |
|---|
| 56 | osg::StateAttribute::ON); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | osg::Geode* geode = new osg::Geode; |
|---|
| 61 | geode->addDrawable(geom); |
|---|
| 62 | |
|---|
| 63 | return geode; |
|---|
| 64 | |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | osg::Node* createPagedModel() |
|---|
| 69 | { |
|---|
| 70 | osg::PagedLOD* level_0 = new osg::PagedLOD; |
|---|
| 71 | |
|---|
| 72 | float distance = 1000.0f; |
|---|
| 73 | |
|---|
| 74 | osg::Vec3 lb(0.0f,0.0f,0.0f); |
|---|
| 75 | osg::Vec3 rb(distance,0.0f,0.0f); |
|---|
| 76 | osg::Vec3 rt(distance,0.0f,distance); |
|---|
| 77 | osg::Vec3 lt(0.0f,0.0f,distance); |
|---|
| 78 | |
|---|
| 79 | level_0->addChild(createTile(lb,rb,lt,rt,"lz.rgb"),distance,1e5,""); |
|---|
| 80 | level_0->addChild(createTile(lb,rb,lt,rt,"land_shallow_topo_2048.jpg"),0,distance,"level_1.osg"); |
|---|
| 81 | |
|---|
| 82 | return level_0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | class WriteOutPagedLODSubgraphsVistor : public osg::NodeVisitor |
|---|
| 86 | { |
|---|
| 87 | public: |
|---|
| 88 | WriteOutPagedLODSubgraphsVistor(): |
|---|
| 89 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 90 | { |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | virtual void apply(osg::PagedLOD& plod) |
|---|
| 94 | { |
|---|
| 95 | |
|---|
| 96 | for(unsigned int i=0;i<plod.getNumChildren();++i) |
|---|
| 97 | { |
|---|
| 98 | osg::Node* child = plod.getChild(i); |
|---|
| 99 | std::string filename = plod.getFileName(i); |
|---|
| 100 | if (!filename.empty()) osgDB::writeNodeFile(*child,filename); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | traverse(plod); |
|---|
| 104 | } |
|---|
| 105 | }; |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | int main( int argc, char **argv ) |
|---|
| 110 | { |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); |
|---|
| 117 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" creates a hierachy of files for paging which can be later loaded by viewers."); |
|---|
| 118 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); |
|---|
| 119 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | if (arguments.read("-h") || arguments.read("--help")) |
|---|
| 123 | { |
|---|
| 124 | arguments.getApplicationUsage()->write(std::cout); |
|---|
| 125 | return 1; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | arguments.reportRemainingOptionsAsUnrecognized(); |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | if (arguments.errors()) |
|---|
| 133 | { |
|---|
| 134 | arguments.writeErrorMessages(std::cout); |
|---|
| 135 | return 1; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | osg::ref_ptr<osg::Node> model = createPagedModel(); |
|---|
| 146 | |
|---|
| 147 | if (model.valid()) |
|---|
| 148 | { |
|---|
| 149 | osgDB::writeNodeFile(*model,"level_0.osg"); |
|---|
| 150 | |
|---|
| 151 | WriteOutPagedLODSubgraphsVistor woplsv; |
|---|
| 152 | model->accept(woplsv); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | return 0; |
|---|
| 156 | } |
|---|