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/misc/sgstream.hxx>
46 #include <simgear/scene/material/mat.hxx>
47 #include <simgear/scene/material/matlib.hxx>
48 #include <simgear/scene/model/ModelRegistry.hxx>
49 #include <simgear/scene/tgdb/apt_signs.hxx>
50 #include <simgear/scene/tgdb/obj.hxx>
51 #include <simgear/scene/util/OsgMath.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 = makeZUpFrame(geod);
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 );
142 // storage class for deferred object processing in TileEntry::load()
144 Object(object_type t, const string& token, const SGPath& p,
150 in >> lon >> lat >> elev >> hdg;
154 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name);
156 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " " << name << " lon=" <<
157 lon << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
162 double lon, lat, elev, hdg;
165 // Work in progress... load the tile based entirely by name cuz that's
166 // what we'll want to do with the database pager.
169 TileEntry::loadTileByFileName(const string& fileName,
170 const osgDB::Options* options)
172 std::string index_str = osgDB::getNameLessExtension(fileName);
173 index_str = osgDB::getSimpleFileName(index_str);
177 std::istringstream idxStream(index_str);
178 idxStream >> tileIndex;
180 SGBucket tile_bucket(tileIndex);
181 const string basePath = tile_bucket.gen_base_path();
183 bool found_tile_base = false;
186 vector<const Object*> objects;
188 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << index_str );
190 osgDB::FilePathList path_list=options->getDatabasePathList();
191 // Make sure we find the original filename here...
192 std::string filePath = osgDB::getFilePath(fileName);
193 if (!filePath.empty()) {
197 path_list.push_front(p.str());
200 // scan and parse all files and store information
201 for (unsigned int i = 0; i < path_list.size(); i++) {
202 // If we found a terrain tile in Terrain/, we have to process the
203 // Objects/ dir in the same group, too, before we can stop scanning.
204 // FGGlobals::set_fg_scenery() inserts an empty string to path_list
207 if (path_list[i].empty()) {
214 bool has_base = false;
216 SGPath tile_path = path_list[i];
217 tile_path.append(basePath);
219 SGPath basename = tile_path;
220 basename.append( index_str );
222 SG_LOG( SG_TERRAIN, SG_DEBUG, " Trying " << basename.str() );
225 // Check for master .stg (scene terra gear) file
226 SGPath stg_name = basename;
227 stg_name.concat( ".stg" );
229 sg_gzifstream in( stg_name.str() );
233 while ( ! in.eof() ) {
237 if ( token.empty() || token[0] == '#' ) {
241 // Load only once (first found)
242 if ( token == "OBJECT_BASE" ) {
244 in >> name >> ::skipws;
245 SG_LOG( SG_TERRAIN, SG_BULK, " " << token << " " << name );
247 if (!found_tile_base) {
248 found_tile_base = true;
251 object_base = tile_path;
252 object_base.append(name);
255 SG_LOG(SG_TERRAIN, SG_BULK, " (skipped)");
257 // Load only if base is not in another file
258 } else if ( token == "OBJECT" ) {
259 if (!found_tile_base || has_base)
260 objects.push_back(new Object(OBJECT, token, tile_path, in));
263 in >> name >> ::skipeol;
264 SG_LOG(SG_TERRAIN, SG_BULK, " " << token << " "
265 << name << " (skipped)");
269 } else if ( token == "OBJECT_STATIC" ) {
270 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
272 } else if ( token == "OBJECT_SHARED" ) {
273 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
275 } else if ( token == "OBJECT_SIGN" ) {
276 objects.push_back(new Object(OBJECT_SIGN, token, tile_path, in));
278 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
279 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
282 SG_LOG( SG_TERRAIN, SG_DEBUG,
283 "Unknown token '" << token << "' in " << stg_name.str() );
289 osg::ref_ptr<SGReaderWriterOptions> opt;
290 opt = SGReaderWriterOptions::copyOrCreate(options);
292 // obj_load() will generate ground lighting for us ...
293 osg::Group* new_tile = new osg::Group;
295 if (found_tile_base) {
296 // load tile if found ...
297 osg::ref_ptr<osg::Node> node;
298 node = osgDB::readRefNodeFile(object_base.str(), opt.get());
300 new_tile->addChild(node);
302 // ... or generate an ocean tile on the fly
303 SG_LOG(SG_TERRAIN, SG_INFO, " Generating ocean tile");
305 osg::Node* node = SGOceanTile(tile_bucket, opt->getMaterialLib());
307 new_tile->addChild(node);
309 SG_LOG( SG_TERRAIN, SG_ALERT,
310 "Warning: failed to generate ocean tile!" );
315 // now that we have a valid center, process all the objects
316 for (unsigned int j = 0; j < objects.size(); j++) {
317 const Object *obj = objects[j];
319 if (obj->type == OBJECT) {
320 SGPath custom_path = obj->path;
321 custom_path.append( obj->name );
322 osg::ref_ptr<osg::Node> node;
323 node = osgDB::readRefNodeFile(custom_path.str(), opt.get());
325 new_tile->addChild(node);
327 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
328 // object loading is deferred to main render thread,
329 // but lets figure out the paths right now.
331 if ( obj->type == OBJECT_STATIC ) {
332 custom_path = obj->path;
334 // custom_path = globals->get_fg_root();
336 custom_path.append( obj->name );
338 osg::ref_ptr<osg::Node> model;
340 model = _modelLoader->loadTileModel(custom_path.str(),
341 obj->type == OBJECT_SHARED);
343 model = osgDB::readRefNodeFile(custom_path.str(), opt.get());
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 // wire as much of the scene graph together as we can
354 new_tile->addChild( obj_trans );
356 obj_trans->addChild(model.get());
358 } else if (obj->type == OBJECT_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
359 // load the object itself
360 SGPath custom_path = obj->path;
361 custom_path.append( obj->name );
363 osg::Node *custom_obj = 0;
364 if (obj->type == OBJECT_SIGN)
365 custom_obj = SGMakeSign(opt->getMaterialLib(), obj->name);
367 custom_obj = SGMakeRunwaySign(opt->getMaterialLib(), obj->name);
369 // wire the pieces together
370 if ( custom_obj != NULL ) {
372 WorldCoordinate( obj_pos, obj->lat, obj->lon, obj->elev, obj->hdg );
374 osg::MatrixTransform *obj_trans = new osg::MatrixTransform;
375 obj_trans->setDataVariance(osg::Object::STATIC);
376 obj_trans->setMatrix( obj_pos );
378 obj_trans->addChild( custom_obj );
380 new_tile->addChild( obj_trans );
389 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
391 terrain_branch->addChild( _node.get() );
393 SG_LOG( SG_TERRAIN, SG_DEBUG,
394 "connected a tile into scene graph. _node = "
396 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
397 << _node->getNumParents() );
402 TileEntry::removeFromSceneGraph()
404 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
407 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
409 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! _node = " << _node.get() );
412 // find the nodes branch parent
413 if ( _node->getNumParents() > 0 ) {
414 // find the first parent (should only be one)
415 osg::Group *parent = _node->getParent( 0 ) ;
417 parent->removeChild( _node.get() );
425 osg::Group *parent = NULL;
426 // find the nodes branch parent
427 if ( _node->getNumParents() > 0 ) {
428 // find the first parent (should only be one)
429 parent = _node->getParent( 0 ) ;
431 parent->removeChild( _node.get() );
434 _node = new osg::LOD;
436 parent->addChild(_node.get());