]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.cxx
2fb9ada68cee73af8c54bac1f612862653c514e9
[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 <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
107     // and ... just in case we missed something ... 
108     terrain->removeAllKids();
109
110     FG_LOG( FG_TERRAIN, FG_DEBUG, "  done with init()"  );
111 }
112
113
114 // Search for the specified "bucket" in the cache
115 int
116 FGTileCache::exists( const FGBucket& p )
117 {
118     int i;
119
120     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
121         if ( tile_cache[i].tile_bucket == p ) {
122             FG_LOG( FG_TERRAIN, FG_DEBUG, 
123                     "TILE EXISTS in cache ... index = " << i );
124             return( i );
125         }
126     }
127     
128     return( -1 );
129 }
130
131
132 // Fill in a tile cache entry with real data for the specified bucket
133 void
134 FGTileCache::fill_in( int index, const FGBucket& p )
135 {
136     cout << "FILL IN CACHE ENTRY = " << index << endl;
137
138     // Force some values in case the tile fails to load (i.e. fill
139     // doesn't exist)
140     tile_cache[index].center = Point3D( 0.0 );
141     tile_cache[index].vtlist = NULL;
142     tile_cache[index].vnlist = NULL;
143     tile_cache[index].tclist = NULL;
144
145     // Load the appropriate data file and build tile fragment list
146     FGPath tile_path( current_options.get_fg_root() );
147     tile_path.append( "Scenery" );
148     tile_path.append( p.gen_base_path() );
149     tile_path.append( p.gen_index_str() );
150
151     tile_cache[index].tile_bucket = p;
152
153     tile_cache[index].select_ptr = new ssgSelector;
154     tile_cache[index].transform_ptr = new ssgTransform;
155     tile_cache[index].range_ptr = new ssgRangeSelector;
156
157     ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
158     if ( new_tile != NULL ) {
159         tile_cache[index].range_ptr->addKid( new_tile );
160     }
161     tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
162     tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
163     terrain->addKid( tile_cache[index].select_ptr );
164
165     if ( tile_cache[index].is_scheduled_for_cache() ) {
166         cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
167         // load, but not needed now so disable
168         tile_cache[index].mark_loaded();
169         tile_cache[index].ssg_disable();
170         tile_cache[index].select_ptr->select(0);
171     } else {
172         cout << "FOUND ONE READY TO LOAD" << endl;
173         tile_cache[index].mark_loaded();
174         tile_cache[index].select_ptr->select(1);
175     }
176 }
177
178
179 // Free a tile cache entry
180 void
181 FGTileCache::entry_free( int cache_index )
182 {
183     cout << "FREEING CACHE ENTRY = " << cache_index << endl;
184     tile_cache[cache_index].free_tile();
185 }
186
187
188 // Return index of next available slot in tile cache
189 int
190 FGTileCache::next_avail( void )
191 {
192     // Point3D delta;
193     Point3D abs_view_pos;
194     int i;
195     // float max, med, min, tmp;
196     float dist, max_dist;
197     int max_index;
198     
199     max_dist = 0.0;
200     max_index = -1;
201
202     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
203         // only look at freeing NON-scheduled (i.e. ready to load
204         // cache entries.  This assumes that the cache is always big
205         // enough for our tile radius!
206
207         if ( tile_cache[i].is_unused() ) {
208             // favor unused cache slots
209             return(i);
210         } else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
211             // calculate approximate distance from view point
212             abs_view_pos = current_view.get_abs_view_pos();
213
214             FG_LOG( FG_TERRAIN, FG_DEBUG,
215                     "DIST Abs view pos = " << abs_view_pos );
216             FG_LOG( FG_TERRAIN, FG_DEBUG,
217                     "    ref point = " << tile_cache[i].center );
218
219             /*
220             delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
221             delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
222             delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
223
224             max = delta.x(); med = delta.y(); min = delta.z();
225             if ( max < med ) {
226                 tmp = max; max = med; med = tmp;
227             }
228             if ( max < min ) {
229                 tmp = max; max = min; min = tmp;
230             }
231             dist = max + (med + min) / 4;
232             */
233
234             dist = tile_cache[i].center.distance3D( abs_view_pos );
235
236             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
237
238             if ( dist > max_dist ) {
239                 max_dist = dist;
240                 max_index = i;
241             }
242         }
243     }
244
245     // If we made it this far, then there were no open cache entries.
246     // We will instead free the furthest cache entry and return it's
247     // index.
248
249     if ( max_index >=0 ) {
250         FG_LOG( FG_TERRAIN, FG_INFO, "    max_dist = " << max_dist );
251         FG_LOG( FG_TERRAIN, FG_INFO, "    index = " << max_index );
252         entry_free( max_index );
253         return( max_index );
254     } else {
255         FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
256         exit( -1 );
257     }
258 }
259
260
261 // Destructor
262 FGTileCache::~FGTileCache( void ) {
263 }
264
265