]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.cxx
Updates to time parsing and setting by David Megginson.
[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 - 2000  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 <Main/globals.hxx>
43 #include <Objects/obj.hxx>
44 #include <Scenery/scenery.hxx>  // for scenery.center
45
46 #include "tilecache.hxx"
47 #include "tileentry.hxx"
48
49 FG_USING_NAMESPACE(std);
50
51 // a cheesy hack (to be fixed later)
52 extern ssgBranch *terrain;
53 extern ssgEntity *penguin;
54
55
56 // the tile cache
57 FGTileCache global_tile_cache;
58
59
60 // Constructor
61 FGTileCache::FGTileCache( void ) {
62     tile_cache.clear();
63 }
64
65
66 // Initialize the tile cache subsystem
67 void
68 FGTileCache::init( void )
69 {
70     int i;
71
72     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
73
74     // expand cache if needed.  For best results ... i.e. to avoid
75     // tile load problems and blank areas: 
76     // 
77     //   target_cache_size >= (current.options.tile_diameter + 1) ** 2 
78     // 
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() );
85     FGTileEntry e;
86     e.mark_unused();
87     e.vec3_ptrs.clear();
88     e.vec2_ptrs.clear();
89     e.index_ptrs.clear();
90     
91     FG_LOG( FG_TERRAIN, FG_DEBUG, "  size of tile = " 
92             << sizeof( e ) );
93     if ( target_cache_size > (int)tile_cache.size() ) {
94         // FGTileEntry e;
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() );
100         }
101     }
102     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done expanding cache, size = " 
103             << tile_cache.size() );
104
105     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
106         if ( !tile_cache[i].is_unused() ) {
107             entry_free(i);
108         }
109         tile_cache[i].mark_unused();
110         tile_cache[i].tile_bucket.make_bad();
111     }
112
113     // and ... just in case we missed something ... 
114     terrain->removeAllKids();
115
116     FG_LOG( FG_TERRAIN, FG_INFO, "  done with init()"  );
117 }
118
119
120 // Search for the specified "bucket" in the cache
121 int
122 FGTileCache::exists( const FGBucket& p )
123 {
124     int i;
125
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 );
130             return( i );
131         }
132     }
133     
134     return( -1 );
135 }
136
137
138 #if 0
139 static void print_refs( ssgSelector *sel, ssgTransform *trans, 
140                  ssgRangeSelector *range) 
141 {
142     cout << "selector -> " << sel->getRef()
143          << "  transform -> " << trans->getRef()
144          << "  range -> " << range->getRef() << endl;
145 }
146 #endif
147
148
149 // Fill in a tile cache entry with real data for the specified bucket
150 void
151 FGTileCache::fill_in( int index, const FGBucket& p )
152 {
153     // cout << "FILL IN CACHE ENTRY = " << index << endl;
154
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() )
159     {
160         FG_LOG( FG_TERRAIN, FG_ALERT, 
161                 "Attempting to overwrite existing or"
162                 << " not properly freed leaf data." );
163         exit(-1);
164     }
165
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;
170
171     FGPath tile_path;
172     if ( globals->get_options()->get_fg_scenery() != "" ) {
173         tile_path.set( globals->get_options()->get_fg_scenery() );
174     } else {
175         tile_path.set( globals->get_options()->get_fg_root() );
176         tile_path.append( "Scenery" );
177     }
178     tile_path.append( p.gen_base_path() );
179     
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], 
184                                      true );
185
186     if ( new_tile != NULL ) {
187         tile_cache[index].range_ptr->addKid( new_tile );
188     }
189   
190     // load custom objects
191     cout << "CUSTOM OBJECTS" << endl;
192
193     FGPath index_path = tile_path;
194     index_path.append( p.gen_index_str() );
195     index_path.concat( ".ind" );
196
197     cout << "Looking in " << index_path.str() << endl;
198
199     fg_gzifstream in( index_path.str() );
200
201     if ( in.is_open() ) {
202         string token, name;
203
204         while ( ! in.eof() ) {
205             in >> token;
206             in >> name;
207 #if defined ( macintosh ) || defined ( _MSC_VER )
208             in >> ::skipws;
209 #else
210             in >> skipws;
211 #endif
212             cout << "token = " << token << " name = " << name << endl;
213
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 );
220             }
221         }
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     sgdVec3 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             sgdCopyVec3( abs_view_pos,
286                          globals->get_current_view()->get_abs_view_pos() );
287
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 );
294
295             /*
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() ) );
299
300             max = delta.x(); med = delta.y(); min = delta.z();
301             if ( max < med ) {
302                 tmp = max; max = med; med = tmp;
303             }
304             if ( max < min ) {
305                 tmp = max; max = min; min = tmp;
306             }
307             dist = max + (med + min) / 4;
308             */
309
310             sgdVec3 center;
311             sgdSetVec3( center,
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 );
316
317             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
318
319             if ( dist > max_dist ) {
320                 max_dist = dist;
321                 max_index = i;
322             }
323         }
324     }
325
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
328     // index.
329
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 );
334         return( max_index );
335     } else {
336         FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
337         exit( -1 );
338     }
339
340     // avoid a potential compiler warning
341     return -1;
342 }
343
344
345 // Destructor
346 FGTileCache::~FGTileCache( void ) {
347 }
348
349