1 // tilecache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1998, 1999 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
35 #include <ssg.h> // plib include
37 #include <Debug/logstream.hxx>
38 #include <Airports/genapt.hxx>
39 #include <Bucket/newbucket.hxx>
40 #include <Clouds/cloudobj.hxx>
41 #include <Main/options.hxx>
42 #include <Main/views.hxx>
43 #include <Misc/fgpath.hxx>
44 #include <Objects/obj.hxx>
46 #include "tilecache.hxx"
47 #include "tileentry.hxx"
50 // a cheesy hack (to be fixed later)
51 extern ssgBranch *terrain;
52 extern ssgEntity *penguin;
56 FGTileCache global_tile_cache;
60 FGTileCache::FGTileCache( void ) {
65 // Initialize the tile cache subsystem
67 FGTileCache::init( void )
71 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
73 // expand cache if needed. For best results ... i.e. to avoid
74 // tile load problems and blank areas:
76 // target_cache_size >= (current.options.tile_diameter + 1) ** 2
78 int side = current_options.get_tile_diameter() + 2;
79 int target_cache_size = (side*side);
80 FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
81 << target_cache_size );
82 FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
83 << tile_cache.size() );
90 FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
92 if ( target_cache_size > (int)tile_cache.size() ) {
94 int expansion_amt = target_cache_size - (int)tile_cache.size();
95 for ( i = 0; i < expansion_amt; ++i ) {
96 tile_cache.push_back( e );
97 FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
98 << tile_cache.size() );
101 FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
102 << tile_cache.size() );
104 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
105 if ( !tile_cache[i].is_unused() ) {
108 tile_cache[i].mark_unused();
109 tile_cache[i].tile_bucket.make_bad();
112 // and ... just in case we missed something ...
113 terrain->removeAllKids();
115 FG_LOG( FG_TERRAIN, FG_DEBUG, " done with init()" );
119 // Search for the specified "bucket" in the cache
121 FGTileCache::exists( const FGBucket& p )
125 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
126 if ( tile_cache[i].tile_bucket == p ) {
127 FG_LOG( FG_TERRAIN, FG_DEBUG,
128 "TILE EXISTS in cache ... index = " << i );
138 static void print_refs( ssgSelector *sel, ssgTransform *trans,
139 ssgRangeSelector *range)
141 cout << "selector -> " << sel->getRef()
142 << " transform -> " << trans->getRef()
143 << " range -> " << range->getRef() << endl;
148 // Fill in a tile cache entry with real data for the specified bucket
150 FGTileCache::fill_in( int index, const FGBucket& p )
152 // cout << "FILL IN CACHE ENTRY = " << index << endl;
154 tile_cache[index].center = Point3D( 0.0 );
155 if ( tile_cache[index].vec3_ptrs.size() ||
156 tile_cache[index].vec2_ptrs.size() ||
157 tile_cache[index].index_ptrs.size() )
159 FG_LOG( FG_TERRAIN, FG_ALERT,
160 "Attempting to overwrite existing or"
161 << " not properly freed leaf data." );
165 // Load the appropriate data file and build tile fragment list
166 FGPath tile_path( current_options.get_fg_root() );
167 tile_path.append( "Scenery" );
168 tile_path.append( p.gen_base_path() );
169 tile_path.append( p.gen_index_str() );
171 tile_cache[index].tile_bucket = p;
173 tile_cache[index].select_ptr = new ssgSelector;
174 tile_cache[index].transform_ptr = new ssgTransform;
175 tile_cache[index].range_ptr = new ssgRangeSelector;
177 ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
178 if ( current_options.get_clouds() ) {
179 ssgLeaf *cloud_layer = fgGenCloudLayer( &tile_cache[index],
180 current_options.get_clouds_asl() );
181 new_tile -> addKid( cloud_layer );
184 if ( new_tile != NULL ) {
185 tile_cache[index].range_ptr->addKid( new_tile );
187 tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
188 tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
189 terrain->addKid( tile_cache[index].select_ptr );
191 if ( tile_cache[index].is_scheduled_for_cache() ) {
192 // cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
193 // load, but not needed now so disable
194 tile_cache[index].mark_loaded();
195 tile_cache[index].ssg_disable();
196 tile_cache[index].select_ptr->select(0);
198 // cout << "FOUND ONE READY TO LOAD" << endl;
199 tile_cache[index].mark_loaded();
200 tile_cache[index].select_ptr->select(1);
205 // Free a tile cache entry
207 FGTileCache::entry_free( int cache_index )
209 // cout << "FREEING CACHE ENTRY = " << cache_index << endl;
210 tile_cache[cache_index].free_tile();
214 // Return index of next available slot in tile cache
216 FGTileCache::next_avail( void )
219 Point3D abs_view_pos;
221 // float max, med, min, tmp;
222 float dist, max_dist;
228 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
229 // only look at freeing NON-scheduled (i.e. ready to load
230 // cache entries. This assumes that the cache is always big
231 // enough for our tile radius!
233 if ( tile_cache[i].is_unused() ) {
234 // favor unused cache slots
236 } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
237 // calculate approximate distance from view point
238 abs_view_pos = current_view.get_abs_view_pos();
240 FG_LOG( FG_TERRAIN, FG_DEBUG,
241 "DIST Abs view pos = " << abs_view_pos );
242 FG_LOG( FG_TERRAIN, FG_DEBUG,
243 " ref point = " << tile_cache[i].center );
246 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
247 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
248 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
250 max = delta.x(); med = delta.y(); min = delta.z();
252 tmp = max; max = med; med = tmp;
255 tmp = max; max = min; min = tmp;
257 dist = max + (med + min) / 4;
260 dist = tile_cache[i].center.distance3D( abs_view_pos );
262 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
264 if ( dist > max_dist ) {
271 // If we made it this far, then there were no open cache entries.
272 // We will instead free the furthest cache entry and return it's
275 if ( max_index >=0 ) {
276 FG_LOG( FG_TERRAIN, FG_DEBUG, " max_dist = " << max_dist );
277 FG_LOG( FG_TERRAIN, FG_DEBUG, " index = " << max_index );
278 entry_free( max_index );
281 FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
288 FGTileCache::~FGTileCache( void ) {