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.
22 # include <simgear_config.h>
25 #include <simgear/compiler.h>
32 #include <osg/MatrixTransform>
34 #include <osg/NodeCallback>
36 #include <osgDB/FileNameUtils>
37 #include <osgDB/ReaderWriter>
38 #include <osgDB/ReadFile>
39 #include <osgDB/Registry>
41 #include <simgear/bucket/newbucket.hxx>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/math/sg_geodesy.hxx>
44 #include <simgear/math/sg_random.h>
45 #include <simgear/math/SGMath.hxx>
46 #include <simgear/misc/sgstream.hxx>
47 #include <simgear/scene/material/mat.hxx>
48 #include <simgear/scene/material/matlib.hxx>
49 #include <simgear/scene/model/ModelRegistry.hxx>
50 #include <simgear/scene/tgdb/apt_signs.hxx>
51 #include <simgear/scene/tgdb/obj.hxx>
52 #include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
54 #include "ReaderWriterSTG.hxx"
55 #include "TileEntry.hxx"
58 using namespace simgear;
60 ModelLoadHelper *TileEntry::_modelLoader=0;
63 osgDB::RegisterReaderWriterProxy<ReaderWriterSTG> g_readerWriterSTGProxy;
64 ModelRegistryCallbackProxy<LoadOnlyCallback> g_stgCallbackProxy("stg");
69 TileEntry::TileEntry ( const SGBucket& b )
71 tileFileName(b.gen_index_str()),
72 _node( new osg::LOD ),
77 tileFileName += ".stg";
78 _node->setName(tileFileName);
79 // Give a default LOD range so that traversals that traverse
80 // active children (like the groundcache lookup) will work before
81 // tile manager has had a chance to update this node.
82 _node->setRange(0, 0.0, 10000.0);
87 TileEntry::~TileEntry ()
91 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
92 double lon, double elev, double hdg)
94 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
95 obj_pos = geod.makeZUpFrame();
96 // hdg is not a compass heading, but a counter-clockwise rotation
98 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
103 // Update the ssg transform node for this tile so it can be
104 // properly drawn relative to our (0,0,0) point
105 void TileEntry::prep_ssg_node(float vis) {
108 // visibility can change from frame to frame so we update the
109 // range selector cutoff's each time.
110 float bounding_radius = _node->getChild(0)->getBound().radius();
111 _node->setRange( 0, 0, vis + bounding_radius );
114 bool TileEntry::obj_load(const string& path, osg::Group *geometry, bool is_base,
115 const osgDB::ReaderWriter::Options* options)
117 osg::Node* node = osgDB::readNodeFile(path, options);
119 geometry->addChild(node);
134 // storage class for deferred object processing in TileEntry::load()
136 Object(object_type t, const string& token, const SGPath& p,
142 in >> lon >> lat >> elev >> hdg;
146 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name);
148 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name << " lon=" <<
149 lon << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
154 double lon, lat, elev, hdg;
157 // Work in progress... load the tile based entirely by name cuz that's
158 // what we'll want to do with the database pager.
161 TileEntry::loadTileByFileName(const string& fileName,
162 const osgDB::ReaderWriter::Options* options)
164 std::string index_str = osgDB::getNameLessExtension(fileName);
165 index_str = osgDB::getSimpleFileName(index_str);
169 std::istringstream idxStream(index_str);
170 idxStream >> tileIndex;
172 SGBucket tile_bucket(tileIndex);
173 const string basePath = tile_bucket.gen_base_path();
175 bool found_tile_base = false;
178 vector<const Object*> objects;
180 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << index_str );
182 osgDB::FilePathList path_list=options->getDatabasePathList();
183 // Make sure we find the original filename here...
184 std::string filePath = osgDB::getFilePath(fileName);
185 if (!filePath.empty())
186 path_list.push_front(filePath);
188 // scan and parse all files and store information
189 for (unsigned int i = 0; i < path_list.size(); i++) {
190 // If we found a terrain tile in Terrain/, we have to process the
191 // Objects/ dir in the same group, too, before we can stop scanning.
192 // FGGlobals::set_fg_scenery() inserts an empty string to path_list
195 if (path_list[i].empty()) {
202 bool has_base = false;
204 SGPath tile_path = path_list[i];
205 tile_path.append(basePath);
207 SGPath basename = tile_path;
208 basename.append( index_str );
210 SG_LOG( SG_TERRAIN, SG_DEBUG, " Trying " << basename.str() );
213 // Check for master .stg (scene terra gear) file
214 SGPath stg_name = basename;
215 stg_name.concat( ".stg" );
217 sg_gzifstream in( stg_name.str() );
221 while ( ! in.eof() ) {
225 if ( token.empty() || token[0] == '#' ) {
229 // Load only once (first found)
230 if ( token == "OBJECT_BASE" ) {
232 in >> name >> ::skipws;
233 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
235 if (!found_tile_base) {
236 found_tile_base = true;
239 object_base = tile_path;
240 object_base.append(name);
243 SG_LOG(SG_TERRAIN, SG_BULK, " (skipped)");
245 // Load only if base is not in another file
246 } else if ( token == "OBJECT" ) {
247 if (!found_tile_base || has_base)
248 objects.push_back(new Object(OBJECT, token, tile_path, in));
251 in >> name >> ::skipeol;
252 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " "
253 << name << " (skipped)");
257 } else if ( token == "OBJECT_STATIC" ) {
258 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
260 } else if ( token == "OBJECT_SHARED" ) {
261 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
263 } else if ( token == "OBJECT_SIGN" ) {
264 objects.push_back(new Object(OBJECT_SIGN, token, tile_path, in));
266 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
267 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
270 SG_LOG( SG_TERRAIN, SG_DEBUG,
271 "Unknown token '" << token << "' in " << stg_name.str() );
277 const SGReaderWriterBTGOptions* btgOpt;
278 btgOpt = dynamic_cast<const SGReaderWriterBTGOptions *>(options);
279 osg::ref_ptr<SGReaderWriterBTGOptions> opt;
281 opt = new SGReaderWriterBTGOptions(*btgOpt);
283 opt = new SGReaderWriterBTGOptions;
285 // obj_load() will generate ground lighting for us ...
286 osg::Group* new_tile = new osg::Group;
288 if (found_tile_base) {
289 // load tile if found ...
290 opt->setCalcLights(true);
291 obj_load( object_base.str(), new_tile, true, opt.get());
294 // ... or generate an ocean tile on the fly
295 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
296 if ( !SGGenTile( path_list[0], tile_bucket,
297 opt->getMatlib(), new_tile ) ) {
298 SG_LOG( SG_TERRAIN, SG_ALERT,
299 "Warning: failed to generate ocean tile!" );
304 // now that we have a valid center, process all the objects
305 for (unsigned int j = 0; j < objects.size(); j++) {
306 const Object *obj = objects[j];
308 if (obj->type == OBJECT) {
309 SGPath custom_path = obj->path;
310 custom_path.append( obj->name );
311 opt->setCalcLights(true);
312 obj_load( custom_path.str(), new_tile, false, opt.get());
314 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
315 // object loading is deferred to main render thread,
316 // but lets figure out the paths right now.
318 if ( obj->type == OBJECT_STATIC ) {
319 custom_path = obj->path;
321 // custom_path = globals->get_fg_root();
323 custom_path.append( obj->name );
326 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
328 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
329 obj_trans->setDataVariance(osg::Object::STATIC);
330 obj_trans->setMatrix( obj_pos );
332 // wire as much of the scene graph together as we can
333 new_tile->addChild( obj_trans );
335 osg::Node* model = 0;
337 model = _modelLoader->loadTileModel(custom_path.str(),
338 obj->type == OBJECT_SHARED);
340 obj_trans->addChild(model);
341 } else if (obj->type == OBJECT_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
342 // load the object itself
343 SGPath custom_path = obj->path;
344 custom_path.append( obj->name );
347 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
349 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
350 obj_trans->setDataVariance(osg::Object::STATIC);
351 obj_trans->setMatrix( obj_pos );
353 osg::Node *custom_obj = 0;
354 if (obj->type == OBJECT_SIGN)
355 custom_obj = SGMakeSign(opt->getMatlib(), custom_path.str(), obj->name);
357 custom_obj = SGMakeRunwaySign(opt->getMatlib(), custom_path.str(), obj->name);
359 // wire the pieces together
360 if ( custom_obj != NULL ) {
361 obj_trans -> addChild( custom_obj );
363 new_tile->addChild( obj_trans );
372 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
374 terrain_branch->addChild( _node.get() );
376 SG_LOG( SG_TERRAIN, SG_DEBUG,
377 "connected a tile into scene graph. _node = "
379 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
380 << _node->getNumParents() );
385 TileEntry::removeFromSceneGraph()
387 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
390 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
392 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! _node = " << _node.get() );
395 // find the nodes branch parent
396 if ( _node->getNumParents() > 0 ) {
397 // find the first parent (should only be one)
398 osg::Group *parent = _node->getParent( 0 ) ;
400 parent->removeChild( _node.get() );