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/util/RenderConstants.hxx>
42 #include <simgear/scene/material/mat.hxx>
43 #include <simgear/scene/material/matlib.hxx>
44 #include <simgear/scene/tgdb/apt_signs.hxx>
45 #include <simgear/scene/tgdb/obj.hxx>
47 #include "SGOceanTile.hxx"
49 using namespace simgear;
51 static SGBucket bucketIndexFromFileName(const std::string& fileName)
53 // Extract the bucket from the filename
54 std::istringstream ss(osgDB::getNameLessExtension(fileName));
60 return SGBucket(index);
63 static bool hasOptionalValue(sg_gzifstream &in)
65 while ( (in.peek() != '\n') && (in.peek() != '\r')
66 && isspace(in.peek()) ) {
69 if ( isdigit(in.peek()) || (in.peek() == '-') ){
76 ReaderWriterSTG::ReaderWriterSTG()
78 supportsExtension("stg", "SimGear stg database format");
81 ReaderWriterSTG::~ReaderWriterSTG()
85 const char* ReaderWriterSTG::className() const
87 return "STG Database reader";
90 osgDB::ReaderWriter::ReadResult
91 ReaderWriterSTG::readNode(const std::string& fileName, const osgDB::Options* options) const
93 // We treat 123.stg different than ./123.stg.
94 // The difference is that ./123.stg as well as any absolute path
95 // really loads the given stg file and only this.
96 // In contrast 123.stg uses the search paths to load a set of stg
97 // files spread across the scenery directories.
98 if (osgDB::getSimpleFileName(fileName) != fileName)
99 return readStgFile(fileName, options);
101 // For stg meta files, we need options for the search path.
103 return ReadResult::FILE_NOT_FOUND;
107 SG_LOG(SG_TERRAIN, SG_INFO, "Loading tile " << fileName);
109 SGBucket bucket(bucketIndexFromFileName(fileName));
110 std::string basePath = bucket.gen_base_path();
112 osg::ref_ptr<osg::Group> group = new osg::Group;
114 // Stop scanning once an object base is found
115 bool foundBase = false;
116 // This is considered a meta file, so apply the scenery path search
117 const osgDB::FilePathList& filePathList = options->getDatabasePathList();
118 for (osgDB::FilePathList::const_iterator i = filePathList.begin();
119 i != filePathList.end() && !foundBase; ++i) {
121 objects.append("Objects");
122 objects.append(basePath);
123 objects.append(fileName);
124 if (readStgFile(objects.str(), bucket, *group, options))
128 terrain.append("Terrain");
129 terrain.append(basePath);
130 terrain.append(fileName);
131 if (readStgFile(terrain.str(), bucket, *group, options))
135 // ... or generate an ocean tile on the fly
137 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
139 osg::ref_ptr<SGReaderWriterOptions> opt;
140 opt = SGReaderWriterOptions::copyOrCreate(options);
141 osg::Node* node = SGOceanTile(bucket, opt->getMaterialLib());
143 group->addChild(node);
145 SG_LOG( SG_TERRAIN, SG_ALERT,
146 "Warning: failed to generate ocean tile!" );
153 osgDB::ReaderWriter::ReadResult
154 ReaderWriterSTG::readStgFile(const std::string& fileName, const osgDB::Options* options) const
156 // This is considered a real existing file.
157 // We still apply the search path algorithms for relative files.
158 osg::ref_ptr<osg::Group> group = new osg::Group;
159 std::string path = osgDB::findDataFile(fileName, options);
160 readStgFile(path, bucketIndexFromFileName(path), *group, options);
166 ReaderWriterSTG::readStgFile(const std::string& absoluteFileName,
167 const SGBucket& bucket,
168 osg::Group& group, const osgDB::Options* options) const
170 if (absoluteFileName.empty())
173 sg_gzifstream in( absoluteFileName );
177 SG_LOG(SG_TERRAIN, SG_INFO, "Loading stg file " << absoluteFileName);
179 std::string filePath = osgDB::getFilePath(absoluteFileName);
181 osg::ref_ptr<SGReaderWriterOptions> staticOptions;
182 staticOptions = SGReaderWriterOptions::copyOrCreate(options);
183 staticOptions->getDatabasePathList().clear();
184 staticOptions->getDatabasePathList().push_back(filePath);
185 staticOptions->setObjectCacheHint(osgDB::Options::CACHE_NONE);
187 osg::ref_ptr<SGReaderWriterOptions> sharedOptions;
188 sharedOptions = SGReaderWriterOptions::copyOrCreate(options);
189 sharedOptions->getDatabasePathList().clear();
191 SGPath path = filePath;
192 path.append(".."); path.append(".."); path.append("..");
193 sharedOptions->getDatabasePathList().push_back(path.str());
194 std::string fg_root = options->getPluginStringData("SimGear::FG_ROOT");
195 sharedOptions->getDatabasePathList().push_back(fg_root);
197 simgear::AirportSignBuilder signBuilder(staticOptions->getMaterialLib(), bucket.get_center());
199 bool has_base = false;
200 while ( ! in.eof() ) {
205 if ( token.empty() || token[0] == '#' ) {
210 // Then there is always a name
214 SGPath path = filePath;
217 osg::ref_ptr<osg::Node> node;
218 if ( token == "OBJECT_BASE" ) {
219 // Load only once (first found)
220 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
223 node = osgDB::readRefNodeFile(path.str(),
224 staticOptions.get());
227 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
228 << ": Failed to load OBJECT_BASE '"
232 } else if ( token == "OBJECT" ) {
233 node = osgDB::readRefNodeFile(path.str(),
234 staticOptions.get());
237 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
238 << ": Failed to load OBJECT '"
243 double lon, lat, elev, hdg;
244 in >> lon >> lat >> elev >> hdg;
247 if ( token == "OBJECT_STATIC" ) {
248 osg::ref_ptr<SGReaderWriterOptions> opt;
249 opt = new SGReaderWriterOptions(*staticOptions);
250 osg::ProxyNode* proxyNode = new osg::ProxyNode;
251 proxyNode->setLoadingExternalReferenceMode(osg::ProxyNode::DEFER_LOADING_TO_DATABASE_PAGER);
252 /// Hmm, the findDataFile should happen downstream
253 std::string absName = osgDB::findDataFile(name, opt.get());
254 proxyNode->setFileName(0, absName);
255 if (SGPath(absName).lower_extension() == "ac")
257 proxyNode->setNodeMask( ~simgear::MODELLIGHT_BIT );
258 opt->setInstantiateEffects(true);
261 opt->setInstantiateEffects(false);
262 proxyNode->setDatabaseOptions(opt.get());
266 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
267 << ": Failed to load OBJECT_STATIC '"
271 } else if ( token == "OBJECT_SHARED" ) {
272 osg::ref_ptr<SGReaderWriterOptions> opt;
273 opt = new SGReaderWriterOptions(*sharedOptions);
274 /// Hmm, the findDataFile should happen in the downstream readers
275 std::string absName = osgDB::findDataFile(name, opt.get());
276 if (SGPath(absName).lower_extension() == "ac")
277 opt->setInstantiateEffects(true);
279 opt->setInstantiateEffects(false);
280 node = osgDB::readRefNodeFile(absName, opt.get());
281 if (SGPath(absName).lower_extension() == "ac")
282 node->setNodeMask( ~simgear::MODELLIGHT_BIT );
285 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
286 << ": Failed to load OBJECT_SHARED '"
290 } else if ( token == "OBJECT_SIGN" ) {
293 if ( hasOptionalValue(in) ){
296 signBuilder.addSign(SGGeod::fromDegM(lon, lat, elev), hdg, name, size);
298 SG_LOG( SG_TERRAIN, SG_ALERT, absoluteFileName
299 << ": Unknown token '" << token << "'" );
304 matrix = makeZUpFrame(SGGeod::fromDegM(lon, lat, elev));
305 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(hdg),
306 osg::Vec3(0, 0, 1)));
308 if ( hasOptionalValue(in) ){
309 double pitch(0.0), roll(0.0);
312 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(pitch),
313 osg::Vec3(0, 1, 0)));
314 matrix.preMultRotate(osg::Quat(SGMiscd::deg2rad(roll),
315 osg::Vec3(1, 0, 0)));
318 osg::MatrixTransform* matrixTransform;
319 matrixTransform = new osg::MatrixTransform(matrix);
320 matrixTransform->setDataVariance(osg::Object::STATIC);
321 matrixTransform->addChild(node.get());
322 node = matrixTransform;
327 group.addChild(node.get());
332 if (signBuilder.getSignsGroup())
333 group.addChild(signBuilder.getSignsGroup());