]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.cxx
41dc85b8bb33417e6caddae550fe2ed997712e9a
[flightgear.git] / src / Scenery / tilecache.cxx
1 // tilecache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1998, 1999  Curtis L. Olson  - curt@flightgear.org
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <simgear/xgl/xgl.h>
34
35 #include <plib/ssg.h>           // plib include
36
37 #include <simgear/bucket/newbucket.hxx>
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/misc/fgstream.hxx>
40 #include <simgear/misc/fgpath.hxx>
41
42 #include <Airports/genapt.hxx>
43 #include <Clouds/cloudobj.hxx>
44 #include <Main/options.hxx>
45 #include <Main/views.hxx>
46 #include <Objects/obj.hxx>
47 #include <Scenery/scenery.hxx>  // for scenery.center
48
49 #include "tilecache.hxx"
50 #include "tileentry.hxx"
51
52
53 // a cheesy hack (to be fixed later)
54 extern ssgBranch *terrain;
55 extern ssgEntity *penguin;
56
57
58 // the tile cache
59 FGTileCache global_tile_cache;
60
61
62 // Constructor
63 FGTileCache::FGTileCache( void ) {
64     tile_cache.clear();
65 }
66
67
68 // Initialize the tile cache subsystem
69 void
70 FGTileCache::init( void )
71 {
72     int i;
73
74     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
75
76     // expand cache if needed.  For best results ... i.e. to avoid
77     // tile load problems and blank areas: 
78     // 
79     //   target_cache_size >= (current.options.tile_diameter + 1) ** 2 
80     // 
81     int side = current_options.get_tile_diameter() + 2;
82     int target_cache_size = (side*side);
83     FG_LOG( FG_TERRAIN, FG_DEBUG, "  target cache size = " 
84             << target_cache_size );
85     FG_LOG( FG_TERRAIN, FG_DEBUG, "  current cache size = " 
86             << tile_cache.size() );
87     FGTileEntry e;
88     e.mark_unused();
89     e.vec3_ptrs.clear();
90     e.vec2_ptrs.clear();
91     e.index_ptrs.clear();
92     
93     FG_LOG( FG_TERRAIN, FG_DEBUG, "  size of tile = " 
94             << sizeof( e ) );
95     if ( target_cache_size > (int)tile_cache.size() ) {
96         // FGTileEntry e;
97         int expansion_amt = target_cache_size - (int)tile_cache.size();
98         for ( i = 0; i < expansion_amt; ++i ) {
99             tile_cache.push_back( e );
100             FG_LOG( FG_TERRAIN, FG_DEBUG, "  expanding cache size = " 
101                     << tile_cache.size() );
102         }
103     }
104     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done expanding cache, size = " 
105             << tile_cache.size() );
106
107     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
108         if ( !tile_cache[i].is_unused() ) {
109             entry_free(i);
110         }
111         tile_cache[i].mark_unused();
112         tile_cache[i].tile_bucket.make_bad();
113     }
114
115     // and ... just in case we missed something ... 
116     terrain->removeAllKids();
117
118     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done with init()"  );
119 }
120
121
122 // Search for the specified "bucket" in the cache
123 int
124 FGTileCache::exists( const FGBucket& p )
125 {
126     int i;
127
128     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
129         if ( tile_cache[i].tile_bucket == p ) {
130             FG_LOG( FG_TERRAIN, FG_DEBUG, 
131                     "TILE EXISTS in cache ... index = " << i );
132             return( i );
133         }
134     }
135     
136     return( -1 );
137 }
138
139
140 #if 0
141 static void print_refs( ssgSelector *sel, ssgTransform *trans, 
142                  ssgRangeSelector *range) 
143 {
144     cout << "selector -> " << sel->getRef()
145          << "  transform -> " << trans->getRef()
146          << "  range -> " << range->getRef() << endl;
147 }
148 #endif
149
150
151 // Fill in a tile cache entry with real data for the specified bucket
152 void
153 FGTileCache::fill_in( int index, const FGBucket& p )
154 {
155     // cout << "FILL IN CACHE ENTRY = " << index << endl;
156
157     tile_cache[index].center = Point3D( 0.0 );
158     if ( tile_cache[index].vec3_ptrs.size() || 
159          tile_cache[index].vec2_ptrs.size() || 
160          tile_cache[index].index_ptrs.size() )
161     {
162         FG_LOG( FG_TERRAIN, FG_ALERT, 
163                 "Attempting to overwrite existing or"
164                 << " not properly freed leaf data." );
165         exit(-1);
166     }
167
168     tile_cache[index].select_ptr = new ssgSelector;
169     tile_cache[index].transform_ptr = new ssgTransform;
170     tile_cache[index].range_ptr = new ssgRangeSelector;
171     tile_cache[index].tile_bucket = p;
172
173     FGPath tile_path( current_options.get_fg_root() );
174     tile_path.append( "Scenery" );
175     tile_path.append( p.gen_base_path() );
176     
177     // Load the appropriate data file and build tile fragment list
178     FGPath tile_base = tile_path;
179     tile_base.append( p.gen_index_str() );
180     ssgBranch *new_tile = fgObjLoad( tile_base.str(), &tile_cache[index], 
181                                      true );
182
183     if ( new_tile != NULL ) {
184         tile_cache[index].range_ptr->addKid( new_tile );
185     }
186   
187     // load custom objects
188     cout << "CUSTOM OBJECTS" << endl;
189
190     FGPath index_path = tile_path;
191     index_path.append( p.gen_index_str() );
192     index_path.concat( ".ind" );
193
194     cout << "Looking in " << index_path.str() << endl;
195
196     fg_gzifstream in( index_path.str() );
197
198     if ( in.is_open() ) {
199         string token, name;
200
201         while ( ! in.eof() ) {
202             in >> token;
203             in >> name;
204 #ifdef MACOS
205             in >> ::skipws;
206 #else
207             in >> skipws;
208 #endif
209             cout << "token = " << token << " name = " << name << endl;
210
211             FGPath custom_path = tile_path;
212             custom_path.append( name );
213             ssgBranch *custom_obj = fgObjLoad( custom_path.str(),
214                                                &tile_cache[index], false );
215             if ( (new_tile != NULL) && (custom_obj != NULL) ) {
216                 new_tile -> addKid( custom_obj );
217             }
218         }
219     }
220
221     // generate cloud layer
222     if ( current_options.get_clouds() ) {
223         ssgLeaf *cloud_layer = fgGenCloudLayer( &tile_cache[index],
224                                          current_options.get_clouds_asl() );
225         cloud_layer->clrTraversalMaskBits( SSGTRAV_HOT );
226         new_tile -> addKid( cloud_layer );
227     }
228
229     tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
230
231     // calculate initial tile offset
232     tile_cache[index].SetOffset( scenery.center );
233     sgCoord sgcoord;
234     sgSetCoord( &sgcoord,
235                 tile_cache[index].offset.x(), 
236                 tile_cache[index].offset.y(), tile_cache[index].offset.z(),
237                 0.0, 0.0, 0.0 );
238     tile_cache[index].transform_ptr->setTransform( &sgcoord );
239
240     tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
241     terrain->addKid( tile_cache[index].select_ptr );
242
243     if ( tile_cache[index].is_scheduled_for_cache() ) {
244         // cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
245         // load, but not needed now so disable
246         tile_cache[index].mark_loaded();
247         tile_cache[index].ssg_disable();
248         tile_cache[index].select_ptr->select(0);
249     } else {
250         // cout << "FOUND ONE READY TO LOAD" << endl;
251         tile_cache[index].mark_loaded();
252         tile_cache[index].select_ptr->select(1);
253     }
254 }
255
256
257 // Free a tile cache entry
258 void
259 FGTileCache::entry_free( int cache_index )
260 {
261     // cout << "FREEING CACHE ENTRY = " << cache_index << endl;
262     tile_cache[cache_index].free_tile();
263 }
264
265
266 // Return index of next available slot in tile cache
267 int
268 FGTileCache::next_avail( void )
269 {
270     // Point3D delta;
271     Point3D abs_view_pos;
272     int i;
273     // float max, med, min, tmp;
274     float dist, max_dist;
275     int max_index;
276     
277     max_dist = 0.0;
278     max_index = -1;
279
280     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
281         // only look at freeing NON-scheduled (i.e. ready to load
282         // cache entries.  This assumes that the cache is always big
283         // enough for our tile radius!
284
285         if ( tile_cache[i].is_unused() ) {
286             // favor unused cache slots
287             return(i);
288         } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
289             // calculate approximate distance from view point
290             abs_view_pos = current_view.get_abs_view_pos();
291
292             FG_LOG( FG_TERRAIN, FG_DEBUG,
293                     "DIST Abs view pos = " << abs_view_pos );
294             FG_LOG( FG_TERRAIN, FG_DEBUG,
295                     "    ref point = " << tile_cache[i].center );
296
297             /*
298             delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
299             delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
300             delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
301
302             max = delta.x(); med = delta.y(); min = delta.z();
303             if ( max < med ) {
304                 tmp = max; max = med; med = tmp;
305             }
306             if ( max < min ) {
307                 tmp = max; max = min; min = tmp;
308             }
309             dist = max + (med + min) / 4;
310             */
311
312             dist = tile_cache[i].center.distance3D( abs_view_pos );
313
314             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
315
316             if ( dist > max_dist ) {
317                 max_dist = dist;
318                 max_index = i;
319             }
320         }
321     }
322
323     // If we made it this far, then there were no open cache entries.
324     // We will instead free the furthest cache entry and return it's
325     // index.
326
327     if ( max_index >=0 ) {
328         FG_LOG( FG_TERRAIN, FG_DEBUG, "    max_dist = " << max_dist );
329         FG_LOG( FG_TERRAIN, FG_DEBUG, "    index = " << max_index );
330         entry_free( max_index );
331         return( max_index );
332     } else {
333         FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
334         exit( -1 );
335     }
336 }
337
338
339 // Destructor
340 FGTileCache::~FGTileCache( void ) {
341 }
342
343