1 // tileentry.cxx -- routines to handle a scenery tile
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2001 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # include <simgear_config.h>
26 #include "ReaderWriterSTG.hxx"
28 #include <osg/MatrixTransform>
29 #include <osg/ProxyNode>
31 #include <osgDB/FileNameUtils>
32 #include <osgDB/FileUtils>
33 #include <osgDB/Registry>
34 #include <osgDB/ReaderWriter>
35 #include <osgDB/ReadFile>
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/sgstream.hxx>
40 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
41 #include <simgear/scene/material/mat.hxx>
42 #include <simgear/scene/material/matlib.hxx>
43 #include <simgear/scene/tgdb/apt_signs.hxx>
44 #include <simgear/scene/tgdb/obj.hxx>
46 #include "SGOceanTile.hxx"
48 using namespace simgear;
50 static SGBucket bucketIndexFromFileName(const std::string& fileName)
52 // Extract the bucket from the filename
53 std::istringstream ss(osgDB::getNameLessExtension(fileName));
59 return SGBucket(index);
62 ReaderWriterSTG::ReaderWriterSTG()
64 supportsExtension("stg", "SimGear stg database format");
67 ReaderWriterSTG::~ReaderWriterSTG()
71 const char* ReaderWriterSTG::className() const
73 return "STG Database reader";
76 osgDB::ReaderWriter::ReadResult
77 ReaderWriterSTG::readNode(const std::string& fileName, const osgDB::Options* options) const
79 // We treat 123.stg different than ./123.stg.
80 // The difference is that ./123.stg as well as any absolute path
81 // really loads the given stg file and only this.
82 // In contrast 123.stg uses the search paths to load a set of stg
83 // files spread across the scenery directories.
84 if (osgDB::getSimpleFileName(fileName) != fileName)
85 return readStgFile(fileName, options);
87 // For stg meta files, we need options for the search path.
89 return ReadResult::FILE_NOT_FOUND;
93 SG_LOG(SG_TERRAIN, SG_INFO, "Loading tile " << fileName);
95 SGBucket bucket(bucketIndexFromFileName(fileName));
96 std::string basePath = bucket.gen_base_path();
98 osg::ref_ptr<osg::Group> group = new osg::Group;
100 // Stop scanning once an object base is found
101 bool foundBase = false;
102 // This is considered a meta file, so apply the scenery path search
103 const osgDB::FilePathList& filePathList = options->getDatabasePathList();
104 for (osgDB::FilePathList::const_iterator i = filePathList.begin();
105 i != filePathList.end() && !foundBase; ++i) {
107 objects.append("Objects");
108 objects.append(basePath);
109 objects.append(fileName);
110 if (readStgFile(objects.str(), bucket, *group, options))
114 terrain.append("Terrain");
115 terrain.append(basePath);
116 terrain.append(fileName);
117 if (readStgFile(terrain.str(), bucket, *group, options))
121 // ... or generate an ocean tile on the fly
123 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
125 osg::ref_ptr<SGReaderWriterOptions> opt;
126 opt = SGReaderWriterOptions::copyOrCreate(options);
127 osg::Node* node = SGOceanTile(bucket, opt->getMaterialLib());
129 group->addChild(node);
131 SG_LOG( SG_TERRAIN, SG_ALERT,
132 "Warning: failed to generate ocean tile!" );
139 osgDB::ReaderWriter::ReadResult
140 ReaderWriterSTG::readStgFile(const std::string& fileName, const osgDB::Options* options) const
142 // This is considered a real existing file.
143 // We still apply the search path algorithms for relative files.
144 osg::ref_ptr<osg::Group> group = new osg::Group;
145 std::string path = osgDB::findDataFile(fileName, options);
146 readStgFile(path, bucketIndexFromFileName(path), *group, options);
152 ReaderWriterSTG::readStgFile(const std::string& absoluteFileName,
153 const SGBucket& bucket,
154 osg::Group& group, const osgDB::Options* options) const
156 if (absoluteFileName.empty())
159 sg_gzifstream in( absoluteFileName );
163 SG_LOG(SG_TERRAIN, SG_INFO, "Loading stg file " << absoluteFileName);
165 std::string filePath = osgDB::getFilePath(absoluteFileName);
167 osg::ref_ptr<SGReaderWriterOptions> staticOptions;
168 staticOptions = SGReaderWriterOptions::copyOrCreate(options);
169 staticOptions->getDatabasePathList().clear();
170 staticOptions->getDatabasePathList().push_back(filePath);
171 staticOptions->setObjectCacheHint(osgDB::Options::CACHE_NONE);
173 osg::ref_ptr<SGReaderWriterOptions> sharedOptions;
174 sharedOptions = SGReaderWriterOptions::copyOrCreate(options);
175 sharedOptions->getDatabasePathList().clear();
177 SGPath path = filePath;
178 path.append(".."); path.append(".."); path.append("..");
179 sharedOptions->getDatabasePathList().push_back(path.str());
180 std::string fg_root = options->getPluginStringData("SimGear::FG_ROOT");
181 sharedOptions->getDatabasePathList().push_back(fg_root);
183 simgear::AirportSignBuilder signBuilder(staticOptions->getMaterialLib(), bucket.get_center());
185 bool has_base = false;
186 while ( ! in.eof() ) {
191 if ( token.empty() || token[0] == '#' ) {
196 // Then there is always a name
200 SGPath path = filePath;
203 osg::ref_ptr<osg::Node> node;
204 if ( token == "OBJECT_BASE" ) {
205 // Load only once (first found)
206 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
209 node = osgDB::readRefNodeFile(path.str(),
210 staticOptions.get());
213 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
214 << ": Failed to load OBJECT_BASE '"
218 } else if ( token == "OBJECT" ) {
219 node = osgDB::readRefNodeFile(path.str(),
220 staticOptions.get());
223 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
224 << ": Failed to load OBJECT '"
229 double lon, lat, elev, hdg;
230 in >> lon >> lat >> elev >> hdg;
233 if ( token == "OBJECT_STATIC" ) {
234 osg::ref_ptr<SGReaderWriterOptions> opt;
235 opt = new SGReaderWriterOptions(*staticOptions);
236 osg::ProxyNode* proxyNode = new osg::ProxyNode;
237 proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
238 /// Hmm, the findDataFile should happen downstream
239 std::string absName = osgDB::findDataFile(name, opt.get());
240 proxyNode->setFileName(0, absName);
241 if (SGPath(absName).lower_extension() == "ac")
242 opt->setInstantiateEffects(true);
244 opt->setInstantiateEffects(false);
245 proxyNode->setDatabaseOptions(opt.get());
249 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
250 << ": Failed to load OBJECT_STATIC '"
254 } else if ( token == "OBJECT_SHARED" ) {
255 osg::ref_ptr<SGReaderWriterOptions> opt;
256 opt = new SGReaderWriterOptions(*sharedOptions);
257 /// Hmm, the findDataFile should happen in the downstream readers
258 std::string absName = osgDB::findDataFile(name, opt.get());
259 if (SGPath(absName).lower_extension() == "ac")
260 opt->setInstantiateEffects(true);
262 opt->setInstantiateEffects(false);
263 node = osgDB::readRefNodeFile(absName, opt.get());
266 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
267 << ": Failed to load OBJECT_SHARED '"
271 } else if ( token == "OBJECT_SIGN" ) {
272 signBuilder.addSign(SGGeod::fromDegM(lon, lat, elev), hdg, name);
274 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
275 << ": Unknown token '" << token << "'" );
280 matrix = makeZUpFrame(SGGeod::fromDegM(lon, lat, elev));
281 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(hdg),
282 osg::Vec3(0, 0, 1)));
284 while ( (in.peek() != '\n') && (in.peek() != '\r') &&
285 (in.peek() != '-') && isspace(in.peek()) ) {
289 if (isdigit(in.peek()) || (in.peek() == '-')){
290 double pitch(0.0), roll(0.0);
293 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(pitch),
294 osg::Vec3(0, 1, 0)));
295 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(roll),
296 osg::Vec3(1, 0, 0)));
299 osg::MatrixTransform* matrixTransform;
300 matrixTransform = new osg::MatrixTransform(matrix);
301 matrixTransform->setDataVariance(osg::Object::STATIC);
302 matrixTransform->addChild(node.get());
303 node = matrixTransform;
308 group.addChild(node.get());
313 if (signBuilder.getSignsGroup())
314 group.addChild(signBuilder.getSignsGroup());