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