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 "TileEntry.hxx"
59 using namespace simgear;
61 ModelLoadHelper *TileEntry::_modelLoader=0;
64 osgDB::RegisterReaderWriterProxy<ReaderWriterSTG> g_readerWriterSTGProxy;
65 ModelRegistryCallbackProxy<LoadOnlyCallback> g_stgCallbackProxy("stg");
67 osgDB::RegisterReaderWriterProxy<ReaderWriterSPT> g_readerWriterSPTProxy;
68 ModelRegistryCallbackProxy<LoadOnlyCallback> g_sptCallbackProxy("spt");
73 TileEntry::TileEntry ( const SGBucket& b )
75 tileFileName(b.gen_index_str()),
76 _node( new osg::LOD ),
81 tileFileName += ".stg";
82 _node->setName(tileFileName);
83 // Give a default LOD range so that traversals that traverse
84 // active children (like the groundcache lookup) will work before
85 // tile manager has had a chance to update this node.
86 _node->setRange(0, 0.0, 10000.0);
89 TileEntry::TileEntry( const TileEntry& t )
90 : tile_bucket( t.tile_bucket ),
91 tileFileName(t.tileFileName),
92 _node( new osg::LOD ),
93 _priority(t._priority),
94 _current_view(t._current_view),
95 _time_expired(t._time_expired)
97 _node->setName(tileFileName);
98 // Give a default LOD range so that traversals that traverse
99 // active children (like the groundcache lookup) will work before
100 // tile manager has had a chance to update this node.
101 _node->setRange(0, 0.0, 10000.0);
105 TileEntry::~TileEntry ()
109 static void WorldCoordinate(osg::Matrix& obj_pos, double lat,
110 double lon, double elev, double hdg)
112 SGGeod geod = SGGeod::fromDegM(lon, lat, elev);
113 obj_pos = geod.makeZUpFrame();
114 // hdg is not a compass heading, but a counter-clockwise rotation
116 obj_pos.preMult(osg::Matrix::rotate(hdg * SGD_DEGREES_TO_RADIANS,
121 // Update the ssg transform node for this tile so it can be
122 // properly drawn relative to our (0,0,0) point
123 void TileEntry::prep_ssg_node(float vis) {
126 // visibility can change from frame to frame so we update the
127 // range selector cutoff's each time.
128 float bounding_radius = _node->getChild(0)->getBound().radius();
129 _node->setRange( 0, 0, vis + bounding_radius );
132 bool TileEntry::obj_load(const string& path, osg::Group *geometry, bool is_base,
133 const osgDB::ReaderWriter::Options* options)
135 osg::Node* node = osgDB::readNodeFile(path, options);
137 geometry->addChild(node);
152 // storage class for deferred object processing in TileEntry::load()
154 Object(object_type t, const string& token, const SGPath& p,
160 in >> lon >> lat >> elev >> hdg;
164 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name);
166 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name << " lon=" <<
167 lon << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
172 double lon, lat, elev, hdg;
175 // Work in progress... load the tile based entirely by name cuz that's
176 // what we'll want to do with the database pager.
179 TileEntry::loadTileByFileName(const string& fileName,
180 const osgDB::ReaderWriter::Options* options)
182 std::string index_str = osgDB::getNameLessExtension(fileName);
183 index_str = osgDB::getSimpleFileName(index_str);
187 std::istringstream idxStream(index_str);
188 idxStream >> tileIndex;
190 SGBucket tile_bucket(tileIndex);
191 const string basePath = tile_bucket.gen_base_path();
193 bool found_tile_base = false;
196 vector<const Object*> objects;
198 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << index_str );
200 osgDB::FilePathList path_list=options->getDatabasePathList();
201 // Make sure we find the original filename here...
202 std::string filePath = osgDB::getFilePath(fileName);
203 if (!filePath.empty()) {
207 path_list.push_front(p.str());
210 // scan and parse all files and store information
211 for (unsigned int i = 0; i < path_list.size(); i++) {
212 // If we found a terrain tile in Terrain/, we have to process the
213 // Objects/ dir in the same group, too, before we can stop scanning.
214 // FGGlobals::set_fg_scenery() inserts an empty string to path_list
217 if (path_list[i].empty()) {
224 bool has_base = false;
226 SGPath tile_path = path_list[i];
227 tile_path.append(basePath);
229 SGPath basename = tile_path;
230 basename.append( index_str );
232 SG_LOG( SG_TERRAIN, SG_DEBUG, " Trying " << basename.str() );
235 // Check for master .stg (scene terra gear) file
236 SGPath stg_name = basename;
237 stg_name.concat( ".stg" );
239 sg_gzifstream in( stg_name.str() );
243 while ( ! in.eof() ) {
247 if ( token.empty() || token[0] == '#' ) {
251 // Load only once (first found)
252 if ( token == "OBJECT_BASE" ) {
254 in >> name >> ::skipws;
255 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
257 if (!found_tile_base) {
258 found_tile_base = true;
261 object_base = tile_path;
262 object_base.append(name);
265 SG_LOG(SG_TERRAIN, SG_BULK, " (skipped)");
267 // Load only if base is not in another file
268 } else if ( token == "OBJECT" ) {
269 if (!found_tile_base || has_base)
270 objects.push_back(new Object(OBJECT, token, tile_path, in));
273 in >> name >> ::skipeol;
274 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " "
275 << name << " (skipped)");
279 } else if ( token == "OBJECT_STATIC" ) {
280 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
282 } else if ( token == "OBJECT_SHARED" ) {
283 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
285 } else if ( token == "OBJECT_SIGN" ) {
286 objects.push_back(new Object(OBJECT_SIGN, token, tile_path, in));
288 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
289 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
292 SG_LOG( SG_TERRAIN, SG_DEBUG,
293 "Unknown token '" << token << "' in " << stg_name.str() );
299 const SGReaderWriterOptions* btgOpt;
300 btgOpt = dynamic_cast<const SGReaderWriterOptions*>(options);
301 osg::ref_ptr<SGReaderWriterOptions> opt;
303 opt = new SGReaderWriterOptions(*btgOpt);
305 opt = new SGReaderWriterOptions;
307 // obj_load() will generate ground lighting for us ...
308 osg::Group* new_tile = new osg::Group;
310 if (found_tile_base) {
311 // load tile if found ...
312 obj_load( object_base.str(), new_tile, true, opt.get());
315 // ... or generate an ocean tile on the fly
316 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
317 if ( !SGGenTile( path_list[0], tile_bucket,
318 opt->getMaterialLib(), new_tile ) ) {
319 SG_LOG( SG_TERRAIN, SG_ALERT,
320 "Warning: failed to generate ocean tile!" );
325 // now that we have a valid center, process all the objects
326 for (unsigned int j = 0; j < objects.size(); j++) {
327 const Object *obj = objects[j];
329 if (obj->type == OBJECT) {
330 SGPath custom_path = obj->path;
331 custom_path.append( obj->name );
332 obj_load( custom_path.str(), new_tile, false, opt.get());
334 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
335 // object loading is deferred to main render thread,
336 // but lets figure out the paths right now.
338 if ( obj->type == OBJECT_STATIC ) {
339 custom_path = obj->path;
341 // custom_path = globals->get_fg_root();
343 custom_path.append( obj->name );
346 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
348 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
349 obj_trans->setDataVariance(osg::Object::STATIC);
350 obj_trans->setMatrix( obj_pos );
352 // wire as much of the scene graph together as we can
353 new_tile->addChild( obj_trans );
355 osg::Node* model = 0;
357 model = _modelLoader->loadTileModel(custom_path.str(),
358 obj->type == OBJECT_SHARED);
360 obj_trans->addChild(model);
361 } else if (obj->type == OBJECT_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
362 // load the object itself
363 SGPath custom_path = obj->path;
364 custom_path.append( obj->name );
367 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
369 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
370 obj_trans->setDataVariance(osg::Object::STATIC);
371 obj_trans->setMatrix( obj_pos );
373 osg::Node *custom_obj = 0;
374 if (obj->type == OBJECT_SIGN)
375 custom_obj = SGMakeSign(opt->getMaterialLib(), custom_path.str(), obj->name);
377 custom_obj = SGMakeRunwaySign(opt->getMaterialLib(), custom_path.str(), obj->name);
379 // wire the pieces together
380 if ( custom_obj != NULL ) {
381 obj_trans -> addChild( custom_obj );
383 new_tile->addChild( obj_trans );
392 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
394 terrain_branch->addChild( _node.get() );
396 SG_LOG( SG_TERRAIN, SG_DEBUG,
397 "connected a tile into scene graph. _node = "
399 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
400 << _node->getNumParents() );
405 TileEntry::removeFromSceneGraph()
407 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
410 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
412 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! _node = " << _node.get() );
415 // find the nodes branch parent
416 if ( _node->getNumParents() > 0 ) {
417 // find the first parent (should only be one)
418 osg::Group *parent = _node->getParent( 0 ) ;
420 parent->removeChild( _node.get() );
428 osg::Group *parent = NULL;
429 // find the nodes branch parent
430 if ( _node->getNumParents() > 0 ) {
431 // find the first parent (should only be one)
432 parent = _node->getParent( 0 ) ;
434 parent->removeChild( _node.get() );
437 _node = new osg::LOD;
439 parent->addChild(_node.get());