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/util/SGReaderWriterOptions.hxx>
54 #include "ReaderWriterSPT.hxx"
55 #include "ReaderWriterSTG.hxx"
56 #include "SGOceanTile.hxx"
57 #include "TileEntry.hxx"
60 using namespace simgear;
62 ModelLoadHelper *TileEntry::_modelLoader=0;
65 osgDB::RegisterReaderWriterProxy<ReaderWriterSTG> g_readerWriterSTGProxy;
66 ModelRegistryCallbackProxy<LoadOnlyCallback> g_stgCallbackProxy("stg");
68 osgDB::RegisterReaderWriterProxy<ReaderWriterSPT> g_readerWriterSPTProxy;
69 ModelRegistryCallbackProxy<LoadOnlyCallback> g_sptCallbackProxy("spt");
74 TileEntry::TileEntry ( const SGBucket& b )
76 tileFileName(b.gen_index_str()),
77 _node( new osg::LOD ),
82 tileFileName += ".stg";
83 _node->setName(tileFileName);
84 // Give a default LOD range so that traversals that traverse
85 // active children (like the groundcache lookup) will work before
86 // tile manager has had a chance to update this node.
87 _node->setRange(0, 0.0, 10000.0);
90 TileEntry::TileEntry( const TileEntry& t )
91 : tile_bucket( t.tile_bucket ),
92 tileFileName(t.tileFileName),
93 _node( new osg::LOD ),
94 _priority(t._priority),
95 _current_view(t._current_view),
96 _time_expired(t._time_expired)
98 _node->setName(tileFileName);
99 // Give a default LOD range so that traversals that traverse
100 // active children (like the groundcache lookup) will work before
101 // tile manager has had a chance to update this node.
102 _node->setRange(0, 0.0, 10000.0);
106 TileEntry::~TileEntry ()
110 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
111 double lon, double elev, double hdg)
113 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
114 obj_pos = geod.makeZUpFrame();
115 // hdg is not a compass heading, but a counter-clockwise rotation
117 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
122 // Update the ssg transform node for this tile so it can be
123 // properly drawn relative to our (0,0,0) point
124 void TileEntry::prep_ssg_node(float vis) {
127 // visibility can change from frame to frame so we update the
128 // range selector cutoff's each time.
129 float bounding_radius = _node->getChild(0)->getBound().radius();
130 _node->setRange( 0, 0, vis + bounding_radius );
133 bool TileEntry::obj_load(const string& path, osg::Group *geometry, bool is_base,
134 const osgDB::ReaderWriter::Options* options)
136 osg::Node* node = osgDB::readNodeFile(path, options);
138 geometry->addChild(node);
153 // storage class for deferred object processing in TileEntry::load()
155 Object(object_type t, const string& token, const SGPath& p,
161 in >> lon >> lat >> elev >> hdg;
165 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name);
167 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name << " lon=" <<
168 lon << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
173 double lon, lat, elev, hdg;
176 // Work in progress... load the tile based entirely by name cuz that's
177 // what we'll want to do with the database pager.
180 TileEntry::loadTileByFileName(const string& fileName,
181 const osgDB::ReaderWriter::Options* options)
183 std::string index_str = osgDB::getNameLessExtension(fileName);
184 index_str = osgDB::getSimpleFileName(index_str);
188 std::istringstream idxStream(index_str);
189 idxStream >> tileIndex;
191 SGBucket tile_bucket(tileIndex);
192 const string basePath = tile_bucket.gen_base_path();
194 bool found_tile_base = false;
197 vector<const Object*> objects;
199 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << index_str );
201 osgDB::FilePathList path_list=options->getDatabasePathList();
202 // Make sure we find the original filename here...
203 std::string filePath = osgDB::getFilePath(fileName);
204 if (!filePath.empty()) {
208 path_list.push_front(p.str());
211 // scan and parse all files and store information
212 for (unsigned int i = 0; i < path_list.size(); i++) {
213 // If we found a terrain tile in Terrain/, we have to process the
214 // Objects/ dir in the same group, too, before we can stop scanning.
215 // FGGlobals::set_fg_scenery() inserts an empty string to path_list
218 if (path_list[i].empty()) {
225 bool has_base = false;
227 SGPath tile_path = path_list[i];
228 tile_path.append(basePath);
230 SGPath basename = tile_path;
231 basename.append( index_str );
233 SG_LOG( SG_TERRAIN, SG_DEBUG, " Trying " << basename.str() );
236 // Check for master .stg (scene terra gear) file
237 SGPath stg_name = basename;
238 stg_name.concat( ".stg" );
240 sg_gzifstream in( stg_name.str() );
244 while ( ! in.eof() ) {
248 if ( token.empty() || token[0] == '#' ) {
252 // Load only once (first found)
253 if ( token == "OBJECT_BASE" ) {
255 in >> name >> ::skipws;
256 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
258 if (!found_tile_base) {
259 found_tile_base = true;
262 object_base = tile_path;
263 object_base.append(name);
266 SG_LOG(SG_TERRAIN, SG_BULK, " (skipped)");
268 // Load only if base is not in another file
269 } else if ( token == "OBJECT" ) {
270 if (!found_tile_base || has_base)
271 objects.push_back(new Object(OBJECT, token, tile_path, in));
274 in >> name >> ::skipeol;
275 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " "
276 << name << " (skipped)");
280 } else if ( token == "OBJECT_STATIC" ) {
281 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
283 } else if ( token == "OBJECT_SHARED" ) {
284 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
286 } else if ( token == "OBJECT_SIGN" ) {
287 objects.push_back(new Object(OBJECT_SIGN, token, tile_path, in));
289 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
290 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
293 SG_LOG( SG_TERRAIN, SG_DEBUG,
294 "Unknown token '" << token << "' in " << stg_name.str() );
300 const SGReaderWriterOptions* btgOpt;
301 btgOpt = dynamic_cast<const SGReaderWriterOptions*>(options);
302 osg::ref_ptr<SGReaderWriterOptions> opt;
304 opt = new SGReaderWriterOptions(*btgOpt);
306 opt = new SGReaderWriterOptions;
308 // obj_load() will generate ground lighting for us ...
309 osg::Group* new_tile = new osg::Group;
311 if (found_tile_base) {
312 // load tile if found ...
313 obj_load( object_base.str(), new_tile, true, opt.get());
316 // ... or generate an ocean tile on the fly
317 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
319 osg::Node* node = SGOceanTile(tile_bucket, opt->getMaterialLib());
321 new_tile->addChild(node);
323 SG_LOG( SG_TERRAIN, SG_ALERT,
324 "Warning: failed to generate ocean tile!" );
329 // now that we have a valid center, process all the objects
330 for (unsigned int j = 0; j < objects.size(); j++) {
331 const Object *obj = objects[j];
333 if (obj->type == OBJECT) {
334 SGPath custom_path = obj->path;
335 custom_path.append( obj->name );
336 obj_load( custom_path.str(), new_tile, false, opt.get());
338 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
339 // object loading is deferred to main render thread,
340 // but lets figure out the paths right now.
342 if ( obj->type == OBJECT_STATIC ) {
343 custom_path = obj->path;
345 // custom_path = globals->get_fg_root();
347 custom_path.append( obj->name );
350 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
352 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
353 obj_trans->setDataVariance(osg::Object::STATIC);
354 obj_trans->setMatrix( obj_pos );
356 // wire as much of the scene graph together as we can
357 new_tile->addChild( obj_trans );
359 osg::Node* model = 0;
361 model = _modelLoader->loadTileModel(custom_path.str(),
362 obj->type == OBJECT_SHARED);
364 obj_trans->addChild(model);
365 } else if (obj->type == OBJECT_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
366 // load the object itself
367 SGPath custom_path = obj->path;
368 custom_path.append( obj->name );
371 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
373 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
374 obj_trans->setDataVariance(osg::Object::STATIC);
375 obj_trans->setMatrix( obj_pos );
377 osg::Node *custom_obj = 0;
378 if (obj->type == OBJECT_SIGN)
379 custom_obj = SGMakeSign(opt->getMaterialLib(), custom_path.str(), obj->name);
381 custom_obj = SGMakeRunwaySign(opt->getMaterialLib(), custom_path.str(), obj->name);
383 // wire the pieces together
384 if ( custom_obj != NULL ) {
385 obj_trans -> addChild( custom_obj );
387 new_tile->addChild( obj_trans );
396 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
398 terrain_branch->addChild( _node.get() );
400 SG_LOG( SG_TERRAIN, SG_DEBUG,
401 "connected a tile into scene graph. _node = "
403 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
404 << _node->getNumParents() );
409 TileEntry::removeFromSceneGraph()
411 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
414 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
416 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! _node = " << _node.get() );
419 // find the nodes branch parent
420 if ( _node->getNumParents() > 0 ) {
421 // find the first parent (should only be one)
422 osg::Group *parent = _node->getParent( 0 ) ;
424 parent->removeChild( _node.get() );
432 osg::Group *parent = NULL;
433 // find the nodes branch parent
434 if ( _node->getNumParents() > 0 ) {
435 // find the first parent (should only be one)
436 parent = _node->getParent( 0 ) ;
438 parent->removeChild( _node.get() );
441 _node = new osg::LOD;
443 parent->addChild(_node.get());