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 "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);
85 TileEntry::TileEntry( const TileEntry& t )
86 : tile_bucket( t.tile_bucket ),
87 tileFileName(t.tileFileName),
88 _node( new osg::LOD ),
89 _priority(t._priority),
90 _current_view(t._current_view),
91 _time_expired(t._time_expired)
93 _node->setName(tileFileName);
94 // Give a default LOD range so that traversals that traverse
95 // active children (like the groundcache lookup) will work before
96 // tile manager has had a chance to update this node.
97 _node->setRange(0, 0.0, 10000.0);
101 TileEntry::~TileEntry ()
105 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
106 double lon, double elev, double hdg)
108 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
109 obj_pos = geod.makeZUpFrame();
110 // hdg is not a compass heading, but a counter-clockwise rotation
112 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
117 // Update the ssg transform node for this tile so it can be
118 // properly drawn relative to our (0,0,0) point
119 void TileEntry::prep_ssg_node(float vis) {
122 // visibility can change from frame to frame so we update the
123 // range selector cutoff's each time.
124 float bounding_radius = _node->getChild(0)->getBound().radius();
125 _node->setRange( 0, 0, vis + bounding_radius );
128 bool TileEntry::obj_load(const string& path, osg::Group *geometry, bool is_base,
129 const osgDB::ReaderWriter::Options* options)
131 osg::Node* node = osgDB::readNodeFile(path, options);
133 geometry->addChild(node);
148 // storage class for deferred object processing in TileEntry::load()
150 Object(object_type t, const string& token, const SGPath& p,
156 in >> lon >> lat >> elev >> hdg;
160 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name);
162 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name << " lon=" <<
163 lon << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
168 double lon, lat, elev, hdg;
171 // Work in progress... load the tile based entirely by name cuz that's
172 // what we'll want to do with the database pager.
175 TileEntry::loadTileByFileName(const string& fileName,
176 const osgDB::ReaderWriter::Options* options)
178 std::string index_str = osgDB::getNameLessExtension(fileName);
179 index_str = osgDB::getSimpleFileName(index_str);
183 std::istringstream idxStream(index_str);
184 idxStream >> tileIndex;
186 SGBucket tile_bucket(tileIndex);
187 const string basePath = tile_bucket.gen_base_path();
189 bool found_tile_base = false;
192 vector<const Object*> objects;
194 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << index_str );
196 osgDB::FilePathList path_list=options->getDatabasePathList();
197 // Make sure we find the original filename here...
198 std::string filePath = osgDB::getFilePath(fileName);
199 if (!filePath.empty())
200 path_list.push_front(filePath);
202 // scan and parse all files and store information
203 for (unsigned int i = 0; i < path_list.size(); i++) {
204 // If we found a terrain tile in Terrain/, we have to process the
205 // Objects/ dir in the same group, too, before we can stop scanning.
206 // FGGlobals::set_fg_scenery() inserts an empty string to path_list
209 if (path_list[i].empty()) {
216 bool has_base = false;
218 SGPath tile_path = path_list[i];
219 tile_path.append(basePath);
221 SGPath basename = tile_path;
222 basename.append( index_str );
224 SG_LOG( SG_TERRAIN, SG_DEBUG, " Trying " << basename.str() );
227 // Check for master .stg (scene terra gear) file
228 SGPath stg_name = basename;
229 stg_name.concat( ".stg" );
231 sg_gzifstream in( stg_name.str() );
235 while ( ! in.eof() ) {
239 if ( token.empty() || token[0] == '#' ) {
243 // Load only once (first found)
244 if ( token == "OBJECT_BASE" ) {
246 in >> name >> ::skipws;
247 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
249 if (!found_tile_base) {
250 found_tile_base = true;
253 object_base = tile_path;
254 object_base.append(name);
257 SG_LOG(SG_TERRAIN, SG_BULK, " (skipped)");
259 // Load only if base is not in another file
260 } else if ( token == "OBJECT" ) {
261 if (!found_tile_base || has_base)
262 objects.push_back(new Object(OBJECT, token, tile_path, in));
265 in >> name >> ::skipeol;
266 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " "
267 << name << " (skipped)");
271 } else if ( token == "OBJECT_STATIC" ) {
272 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
274 } else if ( token == "OBJECT_SHARED" ) {
275 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
277 } else if ( token == "OBJECT_SIGN" ) {
278 objects.push_back(new Object(OBJECT_SIGN, token, tile_path, in));
280 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
281 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
284 SG_LOG( SG_TERRAIN, SG_DEBUG,
285 "Unknown token '" << token << "' in " << stg_name.str() );
291 const SGReaderWriterOptions* btgOpt;
292 btgOpt = dynamic_cast<const SGReaderWriterOptions*>(options);
293 osg::ref_ptr<SGReaderWriterOptions> opt;
295 opt = new SGReaderWriterOptions(*btgOpt);
297 opt = new SGReaderWriterOptions;
299 // obj_load() will generate ground lighting for us ...
300 osg::Group* new_tile = new osg::Group;
302 if (found_tile_base) {
303 // load tile if found ...
304 obj_load( object_base.str(), new_tile, true, opt.get());
307 // ... or generate an ocean tile on the fly
308 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
309 if ( !SGGenTile( path_list[0], tile_bucket,
310 opt->getMaterialLib(), new_tile ) ) {
311 SG_LOG( SG_TERRAIN, SG_ALERT,
312 "Warning: failed to generate ocean tile!" );
317 // now that we have a valid center, process all the objects
318 for (unsigned int j = 0; j < objects.size(); j++) {
319 const Object *obj = objects[j];
321 if (obj->type == OBJECT) {
322 SGPath custom_path = obj->path;
323 custom_path.append( obj->name );
324 obj_load( custom_path.str(), new_tile, false, opt.get());
326 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
327 // object loading is deferred to main render thread,
328 // but lets figure out the paths right now.
330 if ( obj->type == OBJECT_STATIC ) {
331 custom_path = obj->path;
333 // custom_path = globals->get_fg_root();
335 custom_path.append( obj->name );
338 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
340 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
341 obj_trans->setDataVariance(osg::Object::STATIC);
342 obj_trans->setMatrix( obj_pos );
344 // wire as much of the scene graph together as we can
345 new_tile->addChild( obj_trans );
347 osg::Node* model = 0;
349 model = _modelLoader->loadTileModel(custom_path.str(),
350 obj->type == OBJECT_SHARED);
352 obj_trans->addChild(model);
353 } else if (obj->type == OBJECT_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
354 // load the object itself
355 SGPath custom_path = obj->path;
356 custom_path.append( obj->name );
359 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
361 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
362 obj_trans->setDataVariance(osg::Object::STATIC);
363 obj_trans->setMatrix( obj_pos );
365 osg::Node *custom_obj = 0;
366 if (obj->type == OBJECT_SIGN)
367 custom_obj = SGMakeSign(opt->getMaterialLib(), custom_path.str(), obj->name);
369 custom_obj = SGMakeRunwaySign(opt->getMaterialLib(), custom_path.str(), obj->name);
371 // wire the pieces together
372 if ( custom_obj != NULL ) {
373 obj_trans -> addChild( custom_obj );
375 new_tile->addChild( obj_trans );
384 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
386 terrain_branch->addChild( _node.get() );
388 SG_LOG( SG_TERRAIN, SG_DEBUG,
389 "connected a tile into scene graph. _node = "
391 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
392 << _node->getNumParents() );
397 TileEntry::removeFromSceneGraph()
399 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
402 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
404 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! _node = " << _node.get() );
407 // find the nodes branch parent
408 if ( _node->getNumParents() > 0 ) {
409 // find the first parent (should only be one)
410 osg::Group *parent = _node->getParent( 0 ) ;
412 parent->removeChild( _node.get() );
420 osg::Group *parent = NULL;
421 // find the nodes branch parent
422 if ( _node->getNumParents() > 0 ) {
423 // find the first parent (should only be one)
424 parent = _node->getParent( 0 ) ;
426 parent->removeChild( _node.get() );
429 _node = new osg::LOD;
431 parent->addChild(_node.get());