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