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>
30 #include <osgUtil/LineSegmentIntersector>
31 #include <osgUtil/IntersectionVisitor>
33 #include <osgDB/FileNameUtils>
34 #include <osgDB/FileUtils>
35 #include <osgDB/Registry>
36 #include <osgDB/ReaderWriter>
37 #include <osgDB/ReadFile>
39 #include <simgear/bucket/newbucket.hxx>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/misc/sgstream.hxx>
42 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
43 #include <simgear/scene/util/RenderConstants.hxx>
44 #include <simgear/scene/util/OsgMath.hxx>
45 #include <simgear/scene/tgdb/apt_signs.hxx>
46 #include <simgear/scene/tgdb/obj.hxx>
48 #include "SGOceanTile.hxx"
52 /// Ok, this is a hack - we do not exactly know if it's an airport or not.
53 /// This feature might also vanish again later. This is currently to
54 /// support testing an external ai component that just loads the the airports
55 /// and supports ground queries on only these areas.
56 static bool isAirportBtg(const std::string& name)
60 if (name.substr(4, 8) != ".btg")
62 for (unsigned i = 0; i < 4; ++i) {
63 if (name[i] < 'A' || 'Z' < name[i])
70 static SGBucket bucketIndexFromFileName(const std::string& fileName)
72 // Extract the bucket from the filename
73 std::istringstream ss(osgDB::getNameLessExtension(fileName));
79 return SGBucket(index);
82 struct ReaderWriterSTG::_ModelBin {
84 std::string _errorLocation;
87 osg::ref_ptr<SGReaderWriterOptions> _options;
89 struct _ObjectStatic {
90 _ObjectStatic() : _agl(false), _proxy(false), _lon(0), _lat(0), _elev(0), _hdg(0), _pitch(0), _roll(0) { }
91 std::string _errorLocation;
96 double _lon, _lat, _elev;
97 double _hdg, _pitch, _roll;
98 osg::ref_ptr<SGReaderWriterOptions> _options;
101 _Sign() : _agl(false), _lon(0), _lat(0), _elev(0), _hdg(0), _size(-1) { }
102 std::string _errorLocation;
106 double _lon, _lat, _elev;
115 SGReaderWriterOptions* sharedOptions(const std::string& filePath, const osgDB::Options* options)
117 osg::ref_ptr<SGReaderWriterOptions> sharedOptions;
118 sharedOptions = SGReaderWriterOptions::copyOrCreate(options);
119 sharedOptions->getDatabasePathList().clear();
121 SGPath path = filePath;
122 path.append(".."); path.append(".."); path.append("..");
123 sharedOptions->getDatabasePathList().push_back(path.str());
124 std::string fg_root = options->getPluginStringData("SimGear::FG_ROOT");
125 sharedOptions->getDatabasePathList().push_back(fg_root);
127 return sharedOptions.release();
129 SGReaderWriterOptions* staticOptions(const std::string& filePath, const osgDB::Options* options)
131 osg::ref_ptr<SGReaderWriterOptions> staticOptions;
132 staticOptions = SGReaderWriterOptions::copyOrCreate(options);
133 staticOptions->getDatabasePathList().clear();
135 staticOptions->getDatabasePathList().push_back(filePath);
136 staticOptions->setObjectCacheHint(osgDB::Options::CACHE_NONE);
138 return staticOptions.release();
141 double elevation(osg::Group& group, const SGGeod& geod)
143 SGVec3d start = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, 10000));
144 SGVec3d end = SGVec3d::fromGeod(SGGeod::fromGeodM(geod, -1000));
146 osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector;
147 intersector = new osgUtil::LineSegmentIntersector(toOsg(start), toOsg(end));
148 osgUtil::IntersectionVisitor visitor(intersector.get());
149 group.accept(visitor);
151 if (!intersector->containsIntersections())
154 SGVec3d cart = toSG(intersector->getFirstIntersection().getWorldIntersectPoint());
155 return SGGeod::fromCart(cart).getElevationM();
158 bool read(const std::string& absoluteFileName, const osgDB::Options* options)
160 if (absoluteFileName.empty())
163 sg_gzifstream stream(absoluteFileName);
164 if (!stream.is_open())
167 SG_LOG(SG_TERRAIN, SG_INFO, "Loading stg file " << absoluteFileName);
169 std::string filePath = osgDB::getFilePath(absoluteFileName);
171 // do only load airport btg files.
172 bool onlyAirports = options->getPluginStringData("SimGear::FG_ONLY_AIRPORTS") == "ON";
173 // do only load terrain btg files
174 bool onlyTerrain = options->getPluginStringData("SimGear::FG_ONLY_TERRAIN") == "ON";
176 while (!stream.eof()) {
179 std::getline(stream, line);
182 std::string::size_type hash_pos = line.find('#');
183 if (hash_pos != std::string::npos)
184 line.resize(hash_pos);
186 // and process further
187 std::stringstream in(line);
196 // Then there is always a name
200 SGPath path = filePath;
203 if (token == "OBJECT_BASE") {
204 // Load only once (first found)
205 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
207 if (!onlyAirports || isAirportBtg(name)) {
209 obj._errorLocation = absoluteFileName;
211 obj._name = path.str();
212 obj._options = staticOptions(filePath, options);
213 _objectList.push_back(obj);
216 } else if (token == "OBJECT") {
217 if (!onlyAirports || isAirportBtg(name)) {
219 obj._errorLocation = absoluteFileName;
221 obj._name = path.str();
222 obj._options = staticOptions(filePath, options);
223 _objectList.push_back(obj);
228 if (token == "OBJECT_STATIC" || token == "OBJECT_STATIC_AGL") {
230 osg::ref_ptr<SGReaderWriterOptions> opt;
231 opt = staticOptions(filePath, options);
232 if (SGPath(name).lower_extension() == "ac")
233 opt->setInstantiateEffects(true);
235 opt->setInstantiateEffects(false);
237 obj._errorLocation = absoluteFileName;
240 obj._agl = (token == "OBJECT_STATIC_AGL");
242 in >> obj._lon >> obj._lat >> obj._elev >> obj._hdg >> obj._pitch >> obj._roll;
244 _objectStaticList.push_back(obj);
247 } else if (token == "OBJECT_SHARED" || token == "OBJECT_SHARED_AGL") {
249 osg::ref_ptr<SGReaderWriterOptions> opt;
250 opt = sharedOptions(filePath, options);
251 if (SGPath(name).lower_extension() == "ac")
252 opt->setInstantiateEffects(true);
254 opt->setInstantiateEffects(false);
256 obj._errorLocation = absoluteFileName;
259 obj._agl = (token == "OBJECT_SHARED_AGL");
261 in >> obj._lon >> obj._lat >> obj._elev >> obj._hdg >> obj._pitch >> obj._roll;
263 _objectStaticList.push_back(obj);
266 } else if (token == "OBJECT_SIGN" || token == "OBJECT_SIGN_AGL") {
271 sign._agl = (token == "OBJECT_SIGN_AGL");
272 in >> sign._lon >> sign._lat >> sign._elev >> sign._hdg >> sign._size;
273 _signList.push_back(sign);
277 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
278 << ": Unknown token '" << token << "'" );
286 osg::Node* load(const SGBucket& bucket, const osgDB::Options* opt)
288 osg::ref_ptr<SGReaderWriterOptions> options;
289 options = SGReaderWriterOptions::copyOrCreate(opt);
291 osg::ref_ptr<osg::Group> group = new osg::Group;
292 group->setDataVariance(osg::Object::STATIC);
295 for (std::list<_Object>::iterator i = _objectList.begin(); i != _objectList.end(); ++i) {
296 osg::ref_ptr<osg::Node> node;
297 node = osgDB::readRefNodeFile(i->_name, i->_options.get());
299 SG_LOG(SG_TERRAIN, SG_ALERT, i->_errorLocation << ": Failed to load "
300 << i->_token << " '" << i->_name << "'");
303 group->addChild(node.get());
306 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
308 osg::Node* node = SGOceanTile(bucket, options->getMaterialLib());
310 group->addChild(node);
312 SG_LOG( SG_TERRAIN, SG_ALERT,
313 "Warning: failed to generate ocean tile!" );
317 for (std::list<_ObjectStatic>::iterator i = _objectStaticList.begin(); i != _objectStaticList.end(); ++i) {
320 i->_elev += elevation(*group, SGGeod::fromDeg(i->_lon, i->_lat));
323 for (std::list<_Sign>::iterator i = _signList.begin(); i != _signList.end(); ++i) {
326 i->_elev += elevation(*group, SGGeod::fromDeg(i->_lon, i->_lat));
329 for (std::list<_ObjectStatic>::iterator i = _objectStaticList.begin(); i != _objectStaticList.end(); ++i) {
330 osg::ref_ptr<osg::Node> node;
332 osg::ref_ptr<osg::ProxyNode> proxy = new osg::ProxyNode;
333 proxy->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
334 proxy->setFileName(0, i->_name);
335 proxy->setDatabaseOptions(i->_options.get());
338 node = osgDB::readRefNodeFile(i->_name, i->_options.get());
340 SG_LOG(SG_TERRAIN, SG_ALERT, i->_errorLocation << ": Failed to load "
341 << i->_token << " '" << i->_name << "'");
345 if (SGPath(i->_name).lower_extension() == "ac")
346 node->setNodeMask(~simgear::MODELLIGHT_BIT);
349 matrix = makeZUpFrame(SGGeod::fromDegM(i->_lon, i->_lat, i->_elev));
350 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(i->_hdg), osg::Vec3(0, 0, 1)));
351 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(i->_pitch), osg::Vec3(0, 1, 0)));
352 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(i->_roll), osg::Vec3(1, 0, 0)));
354 osg::MatrixTransform* matrixTransform;
355 matrixTransform = new osg::MatrixTransform(matrix);
356 matrixTransform->setDataVariance(osg::Object::STATIC);
357 matrixTransform->addChild(node.get());
358 group->addChild(matrixTransform);
361 simgear::AirportSignBuilder signBuilder(options->getMaterialLib(), bucket.get_center());
362 for (std::list<_Sign>::iterator i = _signList.begin(); i != _signList.end(); ++i)
363 signBuilder.addSign(SGGeod::fromDegM(i->_lon, i->_lat, i->_elev), i->_hdg, i->_name, i->_size);
364 if (signBuilder.getSignsGroup())
365 group->addChild(signBuilder.getSignsGroup());
367 return group.release();
371 std::list<_Object> _objectList;
372 std::list<_ObjectStatic> _objectStaticList;
373 std::list<_Sign> _signList;
376 ReaderWriterSTG::ReaderWriterSTG()
378 supportsExtension("stg", "SimGear stg database format");
381 ReaderWriterSTG::~ReaderWriterSTG()
385 const char* ReaderWriterSTG::className() const
387 return "STG Database reader";
390 osgDB::ReaderWriter::ReadResult
391 ReaderWriterSTG::readNode(const std::string& fileName, const osgDB::Options* options) const
394 SGBucket bucket(bucketIndexFromFileName(fileName));
396 // We treat 123.stg different than ./123.stg.
397 // The difference is that ./123.stg as well as any absolute path
398 // really loads the given stg file and only this.
399 // In contrast 123.stg uses the search paths to load a set of stg
400 // files spread across the scenery directories.
401 if (osgDB::getSimpleFileName(fileName) != fileName) {
402 if (!modelBin.read(fileName, options))
403 return ReadResult::FILE_NOT_FOUND;
405 // For stg meta files, we need options for the search path.
407 return ReadResult::FILE_NOT_FOUND;
409 SG_LOG(SG_TERRAIN, SG_INFO, "Loading tile " << fileName);
411 std::string basePath = bucket.gen_base_path();
413 // Stop scanning once an object base is found
414 // This is considered a meta file, so apply the scenery path search
415 const osgDB::FilePathList& filePathList = options->getDatabasePathList();
416 for (osgDB::FilePathList::const_iterator i = filePathList.begin();
417 i != filePathList.end() && !modelBin._foundBase; ++i) {
419 objects.append("Objects");
420 objects.append(basePath);
421 objects.append(fileName);
422 modelBin.read(objects.str(), options);
425 terrain.append("Terrain");
426 terrain.append(basePath);
427 terrain.append(fileName);
428 modelBin.read(terrain.str(), options);
432 return modelBin.load(bucket, options);