]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.cxx
f60bad41bece96f0e61084dcb2a2bc10e7efb549
[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) 1997  Curtis L. Olson  - curt@infoplane.com
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 <Airports/genapt.hxx>
39 #include <Bucket/newbucket.hxx>
40 #include <Main/options.hxx>
41 #include <Main/views.hxx>
42 #include <Misc/fgpath.hxx>
43 #include <Objects/obj.hxx>
44
45 #include "tilecache.hxx"
46 #include "tileentry.hxx"
47
48
49 // a cheesy hack (to be fixed later)
50 extern ssgBranch *terrain;
51 extern ssgEntity *penguin;
52
53
54 // the tile cache
55 FGTileCache global_tile_cache;
56
57
58 // Constructor
59 FGTileCache::FGTileCache( void ) {
60     tile_cache.clear();
61 }
62
63
64 // Initialize the tile cache subsystem
65 void
66 FGTileCache::init( void )
67 {
68     int i;
69
70     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
71
72     // expand cache if needed.  For best results ... i.e. to avoid
73     // tile load problems and blank areas: 
74     // 
75     //   target_cache_size >= (current.options.tile_diameter + 1) ** 2 
76     // 
77     int side = current_options.get_tile_diameter() + 2;
78     int target_cache_size = (side*side);
79     FG_LOG( FG_TERRAIN, FG_DEBUG, "  target cache size = " 
80             << target_cache_size );
81     FG_LOG( FG_TERRAIN, FG_DEBUG, "  current cache size = " 
82             << tile_cache.size() );
83     FGTileEntry e;
84     FG_LOG( FG_TERRAIN, FG_DEBUG, "  size of tile = " 
85             << sizeof( e ) );
86     if ( target_cache_size > (int)tile_cache.size() ) {
87         // FGTileEntry e;
88         e.mark_unused();
89         int expansion_amt = target_cache_size - (int)tile_cache.size();
90         for ( i = 0; i < expansion_amt; ++i ) {
91             tile_cache.push_back( e );
92             FG_LOG( FG_TERRAIN, FG_DEBUG, "  expanding cache size = " 
93                     << tile_cache.size() );
94         }
95     }
96     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done expanding cache, size = " 
97             << tile_cache.size() );
98
99     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
100         if ( !tile_cache[i].is_unused() ) {
101             entry_free(i);
102         }
103         tile_cache[i].mark_unused();
104         tile_cache[i].tile_bucket.make_bad();
105     }
106     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done with init()"  );
107 }
108
109
110 // Search for the specified "bucket" in the cache
111 int
112 FGTileCache::exists( const FGBucket& p )
113 {
114     int i;
115
116     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
117         if ( tile_cache[i].tile_bucket == p ) {
118             FG_LOG( FG_TERRAIN, FG_DEBUG, 
119                     "TILE EXISTS in cache ... index = " << i );
120             return( i );
121         }
122     }
123     
124     return( -1 );
125 }
126
127
128 // Fill in a tile cache entry with real data for the specified bucket
129 void
130 FGTileCache::fill_in( int index, const FGBucket& p )
131 {
132     cout << "FILL IN CACHE ENTRY = " << index << endl;
133
134     // Force some values in case the tile fails to load (i.e. fill
135     // doesn't exist)
136     tile_cache[index].center = Point3D( 0.0 );
137     tile_cache[index].vtlist = NULL;
138     tile_cache[index].vnlist = NULL;
139     tile_cache[index].tclist = NULL;
140
141     // Load the appropriate data file and build tile fragment list
142     FGPath tile_path( current_options.get_fg_root() );
143     tile_path.append( "Scenery" );
144     tile_path.append( p.gen_base_path() );
145     tile_path.append( p.gen_index_str() );
146
147     tile_cache[index].tile_bucket = p;
148
149     tile_cache[index].select_ptr = new ssgSelector;
150     tile_cache[index].transform_ptr = new ssgTransform;
151     tile_cache[index].range_ptr = new ssgRangeSelector;
152
153     ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
154     if ( new_tile != NULL ) {
155         tile_cache[index].range_ptr->addKid( new_tile );
156     }
157     tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
158     tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
159     terrain->addKid( tile_cache[index].select_ptr );
160
161     if ( tile_cache[index].is_scheduled_for_cache() ) {
162         cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
163         // load, but not needed now so disable
164         tile_cache[index].mark_loaded();
165         tile_cache[index].ssg_disable();
166         tile_cache[index].select_ptr->select(0);
167     } else {
168         cout << "FOUND ONE READY TO LOAD" << endl;
169         tile_cache[index].mark_loaded();
170         tile_cache[index].select_ptr->select(1);
171     }
172 }
173
174
175 // Free a tile cache entry
176 void
177 FGTileCache::entry_free( int cache_index )
178 {
179     cout << "FREEING CACHE ENTRY = " << cache_index << endl;
180     tile_cache[cache_index].free_tile();
181 }
182
183
184 // Return index of next available slot in tile cache
185 int
186 FGTileCache::next_avail( void )
187 {
188     // Point3D delta;
189     Point3D abs_view_pos;
190     int i;
191     // float max, med, min, tmp;
192     float dist, max_dist;
193     int max_index;
194     
195     max_dist = 0.0;
196     max_index = -1;
197
198     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
199         // only look at freeing NON-scheduled (i.e. ready to load
200         // cache entries.  This assumes that the cache is always big
201         // enough for our tile radius!
202
203         if ( tile_cache[i].is_unused() ) {
204             // favor unused cache slots
205             return(i);
206         } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
207             // calculate approximate distance from view point
208             abs_view_pos = current_view.get_abs_view_pos();
209
210             FG_LOG( FG_TERRAIN, FG_DEBUG,
211                     "DIST Abs view pos = " << abs_view_pos );
212             FG_LOG( FG_TERRAIN, FG_DEBUG,
213                     "    ref point = " << tile_cache[i].center );
214
215             /*
216             delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
217             delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
218             delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
219
220             max = delta.x(); med = delta.y(); min = delta.z();
221             if ( max < med ) {
222                 tmp = max; max = med; med = tmp;
223             }
224             if ( max < min ) {
225                 tmp = max; max = min; min = tmp;
226             }
227             dist = max + (med + min) / 4;
228             */
229
230             dist = tile_cache[i].center.distance3D( abs_view_pos );
231
232             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
233
234             if ( dist > max_dist ) {
235                 max_dist = dist;
236                 max_index = i;
237             }
238         }
239     }
240
241     // If we made it this far, then there were no open cache entries.
242     // We will instead free the furthest cache entry and return it's
243     // index.
244
245     if ( max_index >=0 ) {
246         FG_LOG( FG_TERRAIN, FG_INFO, "    max_dist = " << max_dist );
247         FG_LOG( FG_TERRAIN, FG_INFO, "    index = " << max_index );
248         entry_free( max_index );
249         return( max_index );
250     } else {
251         FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
252         exit( -1 );
253     }
254 }
255
256
257 // Destructor
258 FGTileCache::~FGTileCache( void ) {
259 }
260
261