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/globals.hxx>
43 #include <Objects/obj.hxx>
44 #include <Scenery/scenery.hxx> // for scenery.center
46 #include "tilecache.hxx"
47 #include "tileentry.hxx"
49 FG_USING_NAMESPACE(std);
51 // a cheesy hack (to be fixed later)
52 extern ssgBranch *terrain;
53 extern ssgEntity *penguin;
57 FGTileCache global_tile_cache;
61 FGTileCache::FGTileCache( void ) {
66 // Initialize the tile cache subsystem
68 FGTileCache::init( void )
72 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
74 // expand cache if needed. For best results ... i.e. to avoid
75 // tile load problems and blank areas:
77 // target_cache_size >= (current.options.tile_diameter + 1) ** 2
79 int side = globals->get_options()->get_tile_diameter() + 2;
80 int target_cache_size = (side*side);
81 FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
82 << target_cache_size );
83 FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
84 << tile_cache.size() );
91 FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
93 if ( target_cache_size > (int)tile_cache.size() ) {
95 int expansion_amt = target_cache_size - (int)tile_cache.size();
96 for ( i = 0; i < expansion_amt; ++i ) {
97 tile_cache.push_back( e );
98 FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
99 << tile_cache.size() );
102 FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
103 << tile_cache.size() );
105 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
106 if ( !tile_cache[i].is_unused() ) {
109 tile_cache[i].mark_unused();
110 tile_cache[i].tile_bucket.make_bad();
113 // and ... just in case we missed something ...
114 terrain->removeAllKids();
116 FG_LOG( FG_TERRAIN, FG_INFO, " done with init()" );
120 // Search for the specified "bucket" in the cache
122 FGTileCache::exists( const FGBucket& p )
126 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
127 if ( tile_cache[i].tile_bucket == p ) {
128 FG_LOG( FG_TERRAIN, FG_DEBUG,
129 "TILE EXISTS in cache ... index = " << i );
139 static void print_refs( ssgSelector *sel, ssgTransform *trans,
140 ssgRangeSelector *range)
142 cout << "selector -> " << sel->getRef()
143 << " transform -> " << trans->getRef()
144 << " range -> " << range->getRef() << endl;
149 // Fill in a tile cache entry with real data for the specified bucket
151 FGTileCache::fill_in( int index, const FGBucket& p )
153 // cout << "FILL IN CACHE ENTRY = " << index << endl;
155 tile_cache[index].center = Point3D( 0.0 );
156 if ( tile_cache[index].vec3_ptrs.size() ||
157 tile_cache[index].vec2_ptrs.size() ||
158 tile_cache[index].index_ptrs.size() )
160 FG_LOG( FG_TERRAIN, FG_ALERT,
161 "Attempting to overwrite existing or"
162 << " not properly freed leaf data." );
166 tile_cache[index].select_ptr = new ssgSelector;
167 tile_cache[index].transform_ptr = new ssgTransform;
168 tile_cache[index].range_ptr = new ssgRangeSelector;
169 tile_cache[index].tile_bucket = p;
172 if ( globals->get_options()->get_fg_scenery() != "" ) {
173 tile_path.set( globals->get_options()->get_fg_scenery() );
175 tile_path.set( globals->get_options()->get_fg_root() );
176 tile_path.append( "Scenery" );
178 tile_path.append( p.gen_base_path() );
180 // Load the appropriate data file
181 FGPath tile_base = tile_path;
182 tile_base.append( p.gen_index_str() );
183 ssgBranch *new_tile = fgObjLoad( tile_base.str(), &tile_cache[index],
186 if ( new_tile != NULL ) {
187 tile_cache[index].range_ptr->addKid( new_tile );
190 // load custom objects
191 cout << "CUSTOM OBJECTS" << endl;
193 FGPath index_path = tile_path;
194 index_path.append( p.gen_index_str() );
195 index_path.concat( ".ind" );
197 cout << "Looking in " << index_path.str() << endl;
199 fg_gzifstream in( index_path.str() );
201 if ( in.is_open() ) {
204 while ( ! in.eof() ) {
207 #if defined ( macintosh ) || defined ( _MSC_VER )
212 cout << "token = " << token << " name = " << name << endl;
214 FGPath custom_path = tile_path;
215 custom_path.append( name );
216 ssgBranch *custom_obj = fgObjLoad( custom_path.str(),
217 &tile_cache[index], false );
218 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
219 new_tile -> addKid( custom_obj );
224 tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
226 // calculate initial tile offset
227 tile_cache[index].SetOffset( scenery.center );
229 sgSetCoord( &sgcoord,
230 tile_cache[index].offset.x(),
231 tile_cache[index].offset.y(), tile_cache[index].offset.z(),
233 tile_cache[index].transform_ptr->setTransform( &sgcoord );
235 tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
236 terrain->addKid( tile_cache[index].select_ptr );
238 if ( tile_cache[index].is_scheduled_for_cache() ) {
239 // cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
240 // load, but not needed now so disable
241 tile_cache[index].mark_loaded();
242 tile_cache[index].ssg_disable();
243 tile_cache[index].select_ptr->select(0);
245 // cout << "FOUND ONE READY TO LOAD" << endl;
246 tile_cache[index].mark_loaded();
247 tile_cache[index].select_ptr->select(1);
252 // Free a tile cache entry
254 FGTileCache::entry_free( int cache_index )
256 // cout << "FREEING CACHE ENTRY = " << cache_index << endl;
257 tile_cache[cache_index].free_tile();
261 // Return index of next available slot in tile cache
263 FGTileCache::next_avail( void )
266 sgdVec3 abs_view_pos;
268 // float max, med, min, tmp;
269 float dist, max_dist;
275 for ( i = 0; i < (int)tile_cache.size(); i++ ) {
276 // only look at freeing NON-scheduled (i.e. ready to load
277 // cache entries. This assumes that the cache is always big
278 // enough for our tile radius!
280 if ( tile_cache[i].is_unused() ) {
281 // favor unused cache slots
283 } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
284 // calculate approximate distance from view point
285 sgdCopyVec3( abs_view_pos,
286 globals->get_current_view()->get_abs_view_pos() );
288 FG_LOG( FG_TERRAIN, FG_DEBUG, "DIST Abs view pos = "
289 << abs_view_pos[0] << ","
290 << abs_view_pos[1] << ","
291 << abs_view_pos[2] );
292 FG_LOG( FG_TERRAIN, FG_DEBUG,
293 " ref point = " << tile_cache[i].center );
296 delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
297 delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
298 delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
300 max = delta.x(); med = delta.y(); min = delta.z();
302 tmp = max; max = med; med = tmp;
305 tmp = max; max = min; min = tmp;
307 dist = max + (med + min) / 4;
312 tile_cache[i].center[0],
313 tile_cache[i].center[1],
314 tile_cache[i].center[2] );
315 dist = sgdDistanceVec3( center, abs_view_pos );
317 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
319 if ( dist > max_dist ) {
326 // If we made it this far, then there were no open cache entries.
327 // We will instead free the furthest cache entry and return it's
330 if ( max_index >=0 ) {
331 FG_LOG( FG_TERRAIN, FG_DEBUG, " max_dist = " << max_dist );
332 FG_LOG( FG_TERRAIN, FG_DEBUG, " index = " << max_index );
333 entry_free( max_index );
336 FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
340 // avoid a potential compiler warning
346 FGTileCache::~FGTileCache( void ) {