| 1 | #include <sstream> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Image> |
|---|
| 4 | #include <osg/Group> |
|---|
| 5 | #include <osg/Notify> |
|---|
| 6 | |
|---|
| 7 | #include <osgDB/FileNameUtils> |
|---|
| 8 | #include <osgDB/FileUtils> |
|---|
| 9 | #include <osgDB/ReadFile> |
|---|
| 10 | #include <osgDB/Registry> |
|---|
| 11 | #include <osgDB/Input> |
|---|
| 12 | #include <osgDB/Output> |
|---|
| 13 | |
|---|
| 14 | #include <osgTerrain/TerrainTile> |
|---|
| 15 | |
|---|
| 16 | class ReaderWriterTerrain : public osgDB::ReaderWriter |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | |
|---|
| 20 | ReaderWriterTerrain() |
|---|
| 21 | { |
|---|
| 22 | supportsExtension("osgTerrain","OpenSceneGraph terrain extension to .osg ascii format"); |
|---|
| 23 | supportsExtension("terrain","OpenSceneGraph terrain ascii format"); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | virtual const char* className() const { return "Terrain ReaderWriter"; } |
|---|
| 27 | |
|---|
| 28 | virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* opt) const |
|---|
| 29 | { |
|---|
| 30 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 31 | |
|---|
| 32 | if (osgDB::equalCaseInsensitive(ext,"terrain")) |
|---|
| 33 | { |
|---|
| 34 | #if 0 |
|---|
| 35 | KeywordValueMap keywordValueMap; |
|---|
| 36 | parseTerrainString(osgDB::getNameLessExtension(file), keywordValueMap); |
|---|
| 37 | |
|---|
| 38 | for(KeywordValueMap::iterator itr = keywordValueMap.begin(); |
|---|
| 39 | itr != keywordValueMap.end(); |
|---|
| 40 | ++itr) |
|---|
| 41 | { |
|---|
| 42 | OSG_NOTICE<<"["<<itr->first<<"] = "<<"["<<itr->second<<"]"<<std::endl; |
|---|
| 43 | } |
|---|
| 44 | #else |
|---|
| 45 | std::istringstream fin(osgDB::getNameLessExtension(file)); |
|---|
| 46 | if (fin) return readNode(fin,opt); |
|---|
| 47 | #endif |
|---|
| 48 | return 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 52 | |
|---|
| 53 | std::string fileName = osgDB::findDataFile( file, opt ); |
|---|
| 54 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | osg::ref_ptr<Options> local_opt = opt ? static_cast<Options*>(opt->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; |
|---|
| 58 | local_opt->setDatabasePath(osgDB::getFilePath(fileName)); |
|---|
| 59 | |
|---|
| 60 | osgDB::ifstream fin(fileName.c_str()); |
|---|
| 61 | if (fin) |
|---|
| 62 | { |
|---|
| 63 | return readNode(fin, local_opt.get()); |
|---|
| 64 | } |
|---|
| 65 | return 0L; |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | virtual osgDB::ReaderWriter::ReadResult readNode(std::istream& fin, const Options* options) const |
|---|
| 70 | { |
|---|
| 71 | fin.imbue(std::locale::classic()); |
|---|
| 72 | |
|---|
| 73 | osgDB::Input fr; |
|---|
| 74 | fr.attach(&fin); |
|---|
| 75 | fr.setOptions(options); |
|---|
| 76 | |
|---|
| 77 | osg::ref_ptr<osg::Group> group = new osg::Group; |
|---|
| 78 | |
|---|
| 79 | while(!fr.eof()) |
|---|
| 80 | { |
|---|
| 81 | |
|---|
| 82 | bool itrAdvanced = false; |
|---|
| 83 | |
|---|
| 84 | if (fr.matchSequence("file %s") || fr.matchSequence("file %w") ) |
|---|
| 85 | { |
|---|
| 86 | osg::Node* node = osgDB::readNodeFile(fr[1].getStr()); |
|---|
| 87 | |
|---|
| 88 | if (node) group->addChild(node); |
|---|
| 89 | |
|---|
| 90 | fr += 2; |
|---|
| 91 | itrAdvanced = true; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | osg::ref_ptr<osg::Node> node = fr.readNode(); |
|---|
| 95 | if (node.valid()) |
|---|
| 96 | { |
|---|
| 97 | group->addChild(node.get()); |
|---|
| 98 | itrAdvanced = true; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (!itrAdvanced) |
|---|
| 102 | { |
|---|
| 103 | if (fr[0].getStr()) { OSG_NOTICE<<"Terrain file - unreconised token : "<<fr[0].getStr() <<""<< std::endl; } |
|---|
| 104 | ++fr; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if (group->getNumChildren()>0) return group.release(); |
|---|
| 109 | else return 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | typedef std::map<std::string, std::string> KeywordValueMap; |
|---|
| 113 | bool parseTerrainString(const std::string& str, KeywordValueMap& keywordValueMap) const |
|---|
| 114 | { |
|---|
| 115 | bool success = false; |
|---|
| 116 | |
|---|
| 117 | std::string::size_type pos = 0; |
|---|
| 118 | while(pos != std::string::npos) |
|---|
| 119 | { |
|---|
| 120 | pos = str.find_first_not_of(' ',pos); |
|---|
| 121 | if (pos == std::string::npos) break; |
|---|
| 122 | |
|---|
| 123 | std::string::size_type semicolon = str.find_first_of(';', pos); |
|---|
| 124 | std::string::size_type startstatement = pos; |
|---|
| 125 | std::string::size_type endstatement = std::string::npos; |
|---|
| 126 | if (semicolon!=std::string::npos) |
|---|
| 127 | { |
|---|
| 128 | endstatement = semicolon-1; |
|---|
| 129 | pos = semicolon+1; |
|---|
| 130 | } |
|---|
| 131 | else |
|---|
| 132 | { |
|---|
| 133 | endstatement = str.length()-1; |
|---|
| 134 | pos = std::string::npos; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | if (startstatement<endstatement) endstatement = str.find_last_not_of(' ',endstatement); |
|---|
| 138 | |
|---|
| 139 | if (startstatement<=endstatement) |
|---|
| 140 | { |
|---|
| 141 | |
|---|
| 142 | std::string::size_type assignment = str.find_first_of('=', startstatement); |
|---|
| 143 | if (assignment!=std::string::npos && assignment>endstatement) |
|---|
| 144 | { |
|---|
| 145 | assignment = std::string::npos; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | std::string::size_type startvariable = startstatement; |
|---|
| 149 | std::string::size_type endvariable = startstatement; |
|---|
| 150 | std::string::size_type startvalue = startstatement; |
|---|
| 151 | std::string::size_type endvalue = endstatement; |
|---|
| 152 | |
|---|
| 153 | if (assignment!=std::string::npos) |
|---|
| 154 | { |
|---|
| 155 | endvariable = assignment-1; |
|---|
| 156 | startvalue = assignment+1; |
|---|
| 157 | |
|---|
| 158 | if (startvariable<=endvariable) |
|---|
| 159 | { |
|---|
| 160 | endvariable = str.find_last_not_of(' ',endvariable); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | if (startvariable<=endvariable) |
|---|
| 164 | { |
|---|
| 165 | ++endvariable; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | if (startvalue<=endvalue) |
|---|
| 171 | { |
|---|
| 172 | startvalue = str.find_first_not_of(' ',startvalue); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | if (startvalue<=endvalue) |
|---|
| 176 | { |
|---|
| 177 | if (startvariable<endvariable) |
|---|
| 178 | { |
|---|
| 179 | keywordValueMap[str.substr(startvariable, endvariable-startvariable)] = str.substr(startvalue, endvalue-startvalue+1); |
|---|
| 180 | success = true; |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | keywordValueMap[""] = str.substr(startvalue, endvalue-startvalue+1); |
|---|
| 185 | success = true; |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | return success; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | }; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | REGISTER_OSGPLUGIN(terrain, ReaderWriterTerrain) |
|---|