]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilecache.cxx
One more pass at a reorg.
[flightgear.git] / src / Scenery / tilecache.cxx
index 1e0411d22acd837bae6533eafb13dc6c29efc8a2..41dc85b8bb33417e6caddae550fe2ed997712e9a 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started January 1998.
 //
-// Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
+// Copyright (C) 1998, 1999  Curtis L. Olson  - curt@flightgear.org
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
 #endif
 
 #include <GL/glut.h>
-#include <XGL/xgl.h>
+#include <simgear/xgl/xgl.h>
 
-#include <ssg.h>               // plib include
+#include <plib/ssg.h>          // plib include
+
+#include <simgear/bucket/newbucket.hxx>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/misc/fgstream.hxx>
+#include <simgear/misc/fgpath.hxx>
 
-#include <Debug/logstream.hxx>
 #include <Airports/genapt.hxx>
-#include <Bucket/newbucket.hxx>
+#include <Clouds/cloudobj.hxx>
 #include <Main/options.hxx>
 #include <Main/views.hxx>
-#include <Misc/fgpath.hxx>
 #include <Objects/obj.hxx>
+#include <Scenery/scenery.hxx>  // for scenery.center
 
 #include "tilecache.hxx"
 #include "tileentry.hxx"
@@ -81,11 +85,15 @@ FGTileCache::init( void )
     FG_LOG( FG_TERRAIN, FG_DEBUG, "  current cache size = " 
            << tile_cache.size() );
     FGTileEntry e;
+    e.mark_unused();
+    e.vec3_ptrs.clear();
+    e.vec2_ptrs.clear();
+    e.index_ptrs.clear();
+    
     FG_LOG( FG_TERRAIN, FG_DEBUG, "  size of tile = " 
            << sizeof( e ) );
     if ( target_cache_size > (int)tile_cache.size() ) {
        // FGTileEntry e;
-       e.mark_unused();
        int expansion_amt = target_cache_size - (int)tile_cache.size();
        for ( i = 0; i < expansion_amt; ++i ) {
            tile_cache.push_back( e );
@@ -103,6 +111,10 @@ FGTileCache::init( void )
        tile_cache[i].mark_unused();
        tile_cache[i].tile_bucket.make_bad();
     }
+
+    // and ... just in case we missed something ... 
+    terrain->removeAllKids();
+
     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done with init()"  );
 }
 
@@ -125,29 +137,106 @@ FGTileCache::exists( const FGBucket& p )
 }
 
 
+#if 0
+static void print_refs( ssgSelector *sel, ssgTransform *trans, 
+                ssgRangeSelector *range) 
+{
+    cout << "selector -> " << sel->getRef()
+        << "  transform -> " << trans->getRef()
+        << "  range -> " << range->getRef() << endl;
+}
+#endif
+
+
 // Fill in a tile cache entry with real data for the specified bucket
 void
 FGTileCache::fill_in( int index, const FGBucket& p )
 {
     // cout << "FILL IN CACHE ENTRY = " << index << endl;
 
-    // Load the appropriate data file and build tile fragment list
-    FGPath tile_path( current_options.get_fg_root() );
-    tile_path.append( "Scenery" );
-    tile_path.append( p.gen_base_path() );
-    tile_path.append( p.gen_index_str() );
-
-    tile_cache[index].tile_bucket = p;
+    tile_cache[index].center = Point3D( 0.0 );
+    if ( tile_cache[index].vec3_ptrs.size() || 
+        tile_cache[index].vec2_ptrs.size() || 
+        tile_cache[index].index_ptrs.size() )
+    {
+       FG_LOG( FG_TERRAIN, FG_ALERT, 
+               "Attempting to overwrite existing or"
+               << " not properly freed leaf data." );
+       exit(-1);
+    }
 
     tile_cache[index].select_ptr = new ssgSelector;
     tile_cache[index].transform_ptr = new ssgTransform;
     tile_cache[index].range_ptr = new ssgRangeSelector;
+    tile_cache[index].tile_bucket = p;
+
+    FGPath tile_path( current_options.get_fg_root() );
+    tile_path.append( "Scenery" );
+    tile_path.append( p.gen_base_path() );
+    
+    // Load the appropriate data file and build tile fragment list
+    FGPath tile_base = tile_path;
+    tile_base.append( p.gen_index_str() );
+    ssgBranch *new_tile = fgObjLoad( tile_base.str(), &tile_cache[index], 
+                                    true );
 
-    ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
     if ( new_tile != NULL ) {
        tile_cache[index].range_ptr->addKid( new_tile );
     }
+  
+    // load custom objects
+    cout << "CUSTOM OBJECTS" << endl;
+
+    FGPath index_path = tile_path;
+    index_path.append( p.gen_index_str() );
+    index_path.concat( ".ind" );
+
+    cout << "Looking in " << index_path.str() << endl;
+
+    fg_gzifstream in( index_path.str() );
+
+    if ( in.is_open() ) {
+       string token, name;
+
+       while ( ! in.eof() ) {
+           in >> token;
+           in >> name;
+#ifdef MACOS
+           in >> ::skipws;
+#else
+           in >> skipws;
+#endif
+           cout << "token = " << token << " name = " << name << endl;
+
+           FGPath custom_path = tile_path;
+           custom_path.append( name );
+           ssgBranch *custom_obj = fgObjLoad( custom_path.str(),
+                                              &tile_cache[index], false );
+           if ( (new_tile != NULL) && (custom_obj != NULL) ) {
+               new_tile -> addKid( custom_obj );
+           }
+       }
+    }
+
+    // generate cloud layer
+    if ( current_options.get_clouds() ) {
+       ssgLeaf *cloud_layer = fgGenCloudLayer( &tile_cache[index],
+                                        current_options.get_clouds_asl() );
+       cloud_layer->clrTraversalMaskBits( SSGTRAV_HOT );
+       new_tile -> addKid( cloud_layer );
+    }
+
     tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
+
+    // calculate initial tile offset
+    tile_cache[index].SetOffset( scenery.center );
+    sgCoord sgcoord;
+    sgSetCoord( &sgcoord,
+               tile_cache[index].offset.x(), 
+               tile_cache[index].offset.y(), tile_cache[index].offset.z(),
+               0.0, 0.0, 0.0 );
+    tile_cache[index].transform_ptr->setTransform( &sgcoord );
+
     tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
     terrain->addKid( tile_cache[index].select_ptr );
 
@@ -167,9 +256,10 @@ FGTileCache::fill_in( int index, const FGBucket& p )
 
 // Free a tile cache entry
 void
-FGTileCache::entry_free( int index )
+FGTileCache::entry_free( int cache_index )
 {
-    tile_cache[index].free_tile();
+    // cout << "FREEING CACHE ENTRY = " << cache_index << endl;
+    tile_cache[cache_index].free_tile();
 }
 
 
@@ -177,9 +267,10 @@ FGTileCache::entry_free( int index )
 int
 FGTileCache::next_avail( void )
 {
-    Point3D delta, abs_view_pos;
+    // Point3D delta;
+    Point3D abs_view_pos;
     int i;
-    float max, med, min, tmp;
+    // float max, med, min, tmp;
     float dist, max_dist;
     int max_index;
     
@@ -194,7 +285,7 @@ FGTileCache::next_avail( void )
        if ( tile_cache[i].is_unused() ) {
            // favor unused cache slots
            return(i);
-       } else if ( tile_cache[i].is_loaded() ) {
+       } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
            // calculate approximate distance from view point
            abs_view_pos = current_view.get_abs_view_pos();
 
@@ -203,6 +294,7 @@ FGTileCache::next_avail( void )
            FG_LOG( FG_TERRAIN, FG_DEBUG,
                    "    ref point = " << tile_cache[i].center );
 
+           /*
            delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
            delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
            delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
@@ -215,6 +307,9 @@ FGTileCache::next_avail( void )
                tmp = max; max = min; min = tmp;
            }
            dist = max + (med + min) / 4;
+           */
+
+           dist = tile_cache[i].center.distance3D( abs_view_pos );
 
            FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
 
@@ -228,9 +323,16 @@ FGTileCache::next_avail( void )
     // If we made it this far, then there were no open cache entries.
     // We will instead free the furthest cache entry and return it's
     // index.
-    
-    entry_free( max_index );
-    return( max_index );
+
+    if ( max_index >=0 ) {
+       FG_LOG( FG_TERRAIN, FG_DEBUG, "    max_dist = " << max_dist );
+       FG_LOG( FG_TERRAIN, FG_DEBUG, "    index = " << max_index );
+       entry_free( max_index );
+       return( max_index );
+    } else {
+       FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
+       exit( -1 );
+    }
 }