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