1 // tilecache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1998 - 2000 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.
33 #include <simgear/xgl/xgl.h>
35 #include <plib/ssg.h> // plib include
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/fgstream.hxx>
40 #include <simgear/misc/fgpath.hxx>
42 #include <Main/options.hxx>
43 #include <Main/views.hxx>
44 #include <Objects/obj.hxx>
45 #include <Scenery/scenery.hxx> // for scenery.center
47 #include "tilecache.hxx"
48 #include "tileentry.hxx"
50 FG_USING_NAMESPACE(std);
52 // a cheesy hack (to be fixed later)
53 extern ssgBranch *terrain;
54 extern ssgEntity *penguin;
58 FGTileCache global_tile_cache;
62 FGTileCache::FGTileCache( void ) {
67 // Initialize the tile cache subsystem
69 FGTileCache::init( void )
73 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
75 // expand cache if needed. For best results ... i.e. to avoid
76 // tile load problems and blank areas:
78 // target_cache_size >= (current.options.tile_diameter + 1) ** 2
80 int side = current_options.get_tile_diameter() + 2;
81 int target_cache_size = (side*side);
82 FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
83 << target_cache_size );
84 FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
85 << tile_cache.size() );
92 FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
94 if ( target_cache_size > (int)tile_cache.size() ) {
96 int expansion_amt = target_cache_size - (int)tile_cache.size();
97 for ( i = 0; i < expansion_amt; ++i ) {
98 tile_cache.push_back( e );
99 FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
100 << tile_cache.size() );
103 FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
104 << tile_cache.size() );
106 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
107 if ( !tile_cache[i].is_unused() ) {
110 tile_cache[i].mark_unused();
111 tile_cache[i].tile_bucket.make_bad();
114 // and ... just in case we missed something ...
115 terrain->removeAllKids();
117 FG_LOG( FG_TERRAIN, FG_DEBUG, " done with init()" );
121 // Search for the specified "bucket" in the cache
123 FGTileCache::exists( const FGBucket& p )
127 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
128 if ( tile_cache[i].tile_bucket == p ) {
129 FG_LOG( FG_TERRAIN, FG_DEBUG,
130 "TILE EXISTS in cache ... index = " << i );
140 static void print_refs( ssgSelector *sel, ssgTransform *trans,
141 ssgRangeSelector *range)
143 cout << "selector -> " << sel->getRef()
144 << " transform -> " << trans->getRef()
145 << " range -> " << range->getRef() << endl;
150 // Fill in a tile cache entry with real data for the specified bucket
152 FGTileCache::fill_in( int index, const FGBucket& p )
154 // cout << "FILL IN CACHE ENTRY = " << index << endl;
156 tile_cache[index].center = Point3D( 0.0 );
157 if ( tile_cache[index].vec3_ptrs.size() ||
158 tile_cache[index].vec2_ptrs.size() ||
159 tile_cache[index].index_ptrs.size() )
161 FG_LOG( FG_TERRAIN, FG_ALERT,
162 "Attempting to overwrite existing or"
163 << " not properly freed leaf data." );
167 tile_cache[index].select_ptr = new ssgSelector;
168 tile_cache[index].transform_ptr = new ssgTransform;
169 tile_cache[index].range_ptr = new ssgRangeSelector;
170 tile_cache[index].tile_bucket = p;
172 FGPath tile_path( current_options.get_fg_root() );
173 tile_path.append( "Scenery" );
174 tile_path.append( p.gen_base_path() );
176 // Load the appropriate data file
177 FGPath tile_base = tile_path;
178 tile_base.append( p.gen_index_str() );
179 ssgBranch *new_tile = fgObjLoad( tile_base.str(), &tile_cache[index],
182 if ( new_tile != NULL ) {
183 tile_cache[index].range_ptr->addKid( new_tile );
186 // load custom objects
187 cout << "CUSTOM OBJECTS" << endl;
189 FGPath index_path = tile_path;
190 index_path.append( p.gen_index_str() );
191 index_path.concat( ".ind" );
193 cout << "Looking in " << index_path.str() << endl;
195 fg_gzifstream in( index_path.str() );
197 if ( in.is_open() ) {
200 while ( ! in.eof() ) {
203 #if defined ( MACOS ) || defined ( _MSC_VER )
208 cout << "token = " << token << " name = " << name << endl;
210 FGPath custom_path = tile_path;
211 custom_path.append( name );
212 ssgBranch *custom_obj = fgObjLoad( custom_path.str(),
213 &tile_cache[index], false );
214 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
215 new_tile -> addKid( custom_obj );
220 tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
222 // calculate initial tile offset
223 tile_cache[index].SetOffset( scenery.center );
225 sgSetCoord( &sgcoord,
226 tile_cache[index].offset.x(),
227 tile_cache[index].offset.y(), tile_cache[index].offset.z(),
229 tile_cache[index].transform_ptr->setTransform( &sgcoord );
231 tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
232 terrain->addKid( tile_cache[index].select_ptr );
234 if ( tile_cache[index].is_scheduled_for_cache() ) {
235 // cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
236 // load, but not needed now so disable
237 tile_cache[index].mark_loaded();
238 tile_cache[index].ssg_disable();
239 tile_cache[index].select_ptr->select(0);
241 // cout << "FOUND ONE READY TO LOAD" << endl;
242 tile_cache[index].mark_loaded();
243 tile_cache[index].select_ptr->select(1);
248 // Free a tile cache entry
250 FGTileCache::entry_free( int cache_index )
252 // cout << "FREEING CACHE ENTRY = " << cache_index << endl;
253 tile_cache[cache_index].free_tile();
257 // Return index of next available slot in tile cache
259 FGTileCache::next_avail( void )
262 Point3D abs_view_pos;
264 // float max, med, min, tmp;
265 float dist, max_dist;
271 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
272 // only look at freeing NON-scheduled (i.e. ready to load
273 // cache entries. This assumes that the cache is always big
274 // enough for our tile radius!
276 if ( tile_cache[i].is_unused() ) {
277 // favor unused cache slots
279 } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
280 // calculate approximate distance from view point
281 abs_view_pos = current_view.get_abs_view_pos();
283 FG_LOG( FG_TERRAIN, FG_DEBUG,
284 "DIST Abs view pos = " << abs_view_pos );
285 FG_LOG( FG_TERRAIN, FG_DEBUG,
286 " ref point = " << tile_cache[i].center );
289 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
290 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
291 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
293 max = delta.x(); med = delta.y(); min = delta.z();
295 tmp = max; max = med; med = tmp;
298 tmp = max; max = min; min = tmp;
300 dist = max + (med + min) / 4;
303 dist = tile_cache[i].center.distance3D( abs_view_pos );
305 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
307 if ( dist > max_dist ) {
314 // If we made it this far, then there were no open cache entries.
315 // We will instead free the furthest cache entry and return it's
318 if ( max_index >=0 ) {
319 FG_LOG( FG_TERRAIN, FG_DEBUG, " max_dist = " << max_dist );
320 FG_LOG( FG_TERRAIN, FG_DEBUG, " index = " << max_index );
321 entry_free( max_index );
324 FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
328 // avoid a potential compiler warning
334 FGTileCache::~FGTileCache( void ) {