]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilecache.cxx
c996087f295f5cc43c7da6fa2786cebbf446a79a
[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     // Load the appropriate data file and build tile fragment list
133     FGPath tile_path( current_options.get_fg_root() );
134     tile_path.append( "Scenery" );
135     tile_path.append( p.gen_base_path() );
136     tile_path.append( p.gen_index_str() );
137
138     tile_cache[index].mark_loaded();
139     tile_cache[index].tile_bucket = p;
140     tile_cache[index].branch_ptr = new ssgTransform;
141     tile_cache[index].range_ptr = new ssgRangeSelector;
142     ssgBranch *new_tile = fgObjLoad( tile_path.str(), &tile_cache[index] );
143     if ( new_tile != NULL ) {
144         tile_cache[index].range_ptr->addKid( new_tile );
145     }
146     tile_cache[index].branch_ptr->addKid( tile_cache[index].range_ptr );
147     terrain->addKid( tile_cache[index].branch_ptr );
148
149     // cout << " ncount before = " << tile_cache[index].ncount << "\n";
150     // cout << " fragments before = " << tile_cache[index].fragment_list.size()
151     //      << "\n";
152
153     string apt_path = tile_path.str();
154     apt_path += ".apt";
155     fgAptGenerate( apt_path, &tile_cache[index] );
156
157     // cout << " ncount after = " << tile_cache[index].ncount << "\n";
158     // cout << " fragments after = " << tile_cache[index].fragment_list.size()
159     //      << "\n";
160 }
161
162
163 // Free a tile cache entry
164 void
165 FGTileCache::entry_free( int index )
166 {
167     tile_cache[index].free_tile();
168 }
169
170
171 // Return index of next available slot in tile cache
172 int
173 FGTileCache::next_avail( void )
174 {
175     Point3D delta, abs_view_pos;
176     int i;
177     float max, med, min, tmp;
178     float dist, max_dist;
179     int max_index;
180     
181     max_dist = 0.0;
182     max_index = -1;
183
184     for ( i = 0; i < (int)tile_cache.size(); i++ ) {
185         // only look at freeing NON-scheduled (i.e. ready to load
186         // cache entries.  This assumes that the cache is always big
187         // enough for our tile radius!
188
189         if ( tile_cache[i].is_unused() ) {
190             // favor unused cache slots
191             return(i);
192         } else if ( tile_cache[i].is_loaded() ) {
193             // calculate approximate distance from view point
194             abs_view_pos = current_view.get_abs_view_pos();
195
196             FG_LOG( FG_TERRAIN, FG_DEBUG,
197                     "DIST Abs view pos = " << abs_view_pos );
198             FG_LOG( FG_TERRAIN, FG_DEBUG,
199                     "    ref point = " << tile_cache[i].center );
200
201             delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
202             delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
203             delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
204
205             max = delta.x(); med = delta.y(); min = delta.z();
206             if ( max < med ) {
207                 tmp = max; max = med; med = tmp;
208             }
209             if ( max < min ) {
210                 tmp = max; max = min; min = tmp;
211             }
212             dist = max + (med + min) / 4;
213
214             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
215
216             if ( dist > max_dist ) {
217                 max_dist = dist;
218                 max_index = i;
219             }
220         }
221     }
222
223     // If we made it this far, then there were no open cache entries.
224     // We will instead free the furthest cache entry and return it's
225     // index.
226     
227     entry_free( max_index );
228     return( max_index );
229 }
230
231
232 // Destructor
233 FGTileCache::~FGTileCache( void ) {
234 }
235
236