]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
2f0a489af03aece77a68851ae54bd76532b14092
[flightgear.git] / src / Scenery / newcache.cxx
1 // newcache.cxx -- routines to handle scenery tile caching
2 //
3 // Written by Curtis Olson, started December 2000.
4 //
5 // Copyright (C) 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/math/sg_geodesy.hxx>
40 #include <simgear/math/sg_random.h>
41 #include <simgear/misc/fgstream.hxx>
42 #include <simgear/misc/fgpath.hxx>
43
44 #include <Main/globals.hxx>
45 #include <Objects/matlib.hxx>
46 #include <Objects/newmat.hxx>
47 #include <Objects/obj.hxx>
48 #include <Scenery/scenery.hxx>  // for scenery.center
49
50 #include "newcache.hxx"
51 #include "tileentry.hxx"
52 #include "tilemgr.hxx"          // temp, need to delete later
53
54 FG_USING_NAMESPACE(std);
55
56 // a cheesy hack (to be fixed later)
57 extern ssgBranch *terrain;
58 extern ssgBranch *ground;
59
60
61 // the tile cache
62 FGNewCache global_tile_cache;
63
64
65 // Constructor
66 FGNewCache::FGNewCache( void ) {
67     tile_cache.clear();
68 }
69
70
71 // Destructor
72 FGNewCache::~FGNewCache( void ) {
73 }
74
75
76 // Free a tile cache entry
77 void FGNewCache::entry_free( long cache_index ) {
78     FG_LOG( FG_TERRAIN, FG_INFO, "FREEING CACHE ENTRY = " << cache_index );
79     FGTileEntry *e = tile_cache[cache_index];
80     e->free_tile();
81     delete( e );
82     tile_cache.erase( cache_index );
83 }
84
85
86 // Initialize the tile cache subsystem
87 void FGNewCache::init( void ) {
88     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
89
90     // expand cache if needed.  For best results ... i.e. to avoid
91     // tile load problems and blank areas: 
92     max_cache_size = 50;        // a random number to start with
93     FG_LOG( FG_TERRAIN, FG_INFO, "  max cache size = " 
94             << max_cache_size );
95     FG_LOG( FG_TERRAIN, FG_INFO, "  current cache size = " 
96             << tile_cache.size() );
97     
98     tile_map_iterator current = tile_cache.begin();
99     tile_map_iterator end = tile_cache.end();
100     
101     for ( ; current != end; ++current ) {
102         long index = current->first;
103         cout << "clearing " << index << endl;
104         FGTileEntry *e = current->second;
105         e->tile_bucket.make_bad();
106         entry_free(index);
107     }
108
109     // and ... just in case we missed something ... 
110     terrain->removeAllKids();
111
112     FG_LOG( FG_TERRAIN, FG_INFO, "  done with init()"  );
113 }
114
115
116 // Search for the specified "bucket" in the cache
117 bool FGNewCache::exists( const FGBucket& b ) {
118     long tile_index = b.gen_index();
119     tile_map_iterator it = tile_cache.find( tile_index );
120
121     return ( it != tile_cache.end() );
122 }
123
124
125 #if 0
126 static void print_refs( ssgSelector *sel, ssgTransform *trans, 
127                  ssgRangeSelector *range) 
128 {
129     cout << "selector -> " << sel->getRef()
130          << "  transform -> " << trans->getRef()
131          << "  range -> " << range->getRef() << endl;
132 }
133 #endif
134
135
136 static ssgLeaf *gen_lights( ssgVertexArray *lights ) {
137     // Allocate ssg structure
138     ssgNormalArray   *nl = NULL;
139     ssgTexCoordArray *tl = NULL;
140     ssgColourArray   *cl = new ssgColourArray( 1 );
141
142     // default to slightly yellow lights for now
143     sgVec4 color;
144     sgSetVec4( color, 1.0, 1.0, 0.7, 1.0 );
145     cl->add( color );
146
147     // create ssg leaf
148     ssgLeaf *leaf = 
149         new ssgVtxTable ( GL_POINTS, lights, nl, tl, cl );
150
151     // assign state
152     FGNewMat *newmat = material_lib.find( "LIGHTS" );
153     leaf->setState( newmat->get_state() );
154
155     return leaf;
156 }
157
158
159 // Fill in a tile cache entry with real data for the specified bucket
160 void FGNewCache::fill_in( const FGBucket& b ) {
161     FG_LOG( FG_TERRAIN, FG_INFO, "FILL IN CACHE ENTRY = " << b.gen_index() );
162
163     // clear out a distant entry in the cache if needed.
164     make_space();
165
166     // create the entry
167     FGTileEntry *e = new FGTileEntry;
168
169     // register it in the cache
170     long tile_index = b.gen_index();
171     tile_cache[tile_index] = e;
172
173     // update the contents
174     e->center = Point3D( 0.0 );
175     if ( e->vec3_ptrs.size() || e->vec2_ptrs.size() || e->index_ptrs.size() ) {
176         FG_LOG( FG_TERRAIN, FG_ALERT, 
177                 "Attempting to overwrite existing or"
178                 << " not properly freed leaf data." );
179         exit(-1);
180     }
181
182     e->terra_transform = new ssgTransform;
183     e->terra_range = new ssgRangeSelector;
184     e->tile_bucket = b;
185
186     FGPath tile_path;
187     if ( globals->get_options()->get_fg_scenery() != "" ) {
188         tile_path.set( globals->get_options()->get_fg_scenery() );
189     } else {
190         tile_path.set( globals->get_options()->get_fg_root() );
191         tile_path.append( "Scenery" );
192     }
193     tile_path.append( b.gen_base_path() );
194     
195     // fgObjLoad will generate ground lighting for us ...
196     ssgVertexArray *light_pts = new ssgVertexArray( 100 );
197
198     // Load the appropriate data file
199     FGPath tile_base = tile_path;
200     tile_base.append( b.gen_index_str() );
201     ssgBranch *new_tile = fgObjLoad( tile_base.str(), e, light_pts, true );
202
203     if ( new_tile != NULL ) {
204         e->terra_range->addKid( new_tile );
205     }
206   
207     // load custom objects
208     cout << "CUSTOM OBJECTS" << endl;
209
210     FGPath index_path = tile_path;
211     index_path.append( b.gen_index_str() );
212     index_path.concat( ".ind" );
213
214     cout << "Looking in " << index_path.str() << endl;
215
216     fg_gzifstream in( index_path.str() );
217
218     if ( in.is_open() ) {
219         string token, name;
220
221         while ( ! in.eof() ) {
222             in >> token;
223             in >> name;
224 #if defined ( macintosh ) || defined ( _MSC_VER )
225             in >> ::skipws;
226 #else
227             in >> skipws;
228 #endif
229             cout << "token = " << token << " name = " << name << endl;
230
231             FGPath custom_path = tile_path;
232             custom_path.append( name );
233             ssgBranch *custom_obj = 
234                 fgObjLoad( custom_path.str(), e, NULL, false );
235             if ( (new_tile != NULL) && (custom_obj != NULL) ) {
236                 new_tile -> addKid( custom_obj );
237             }
238         }
239     }
240
241     e->terra_transform->addKid( e->terra_range );
242
243     // calculate initial tile offset
244     e->SetOffset( scenery.center );
245     sgCoord sgcoord;
246     sgSetCoord( &sgcoord,
247                 e->offset.x(), e->offset.y(), e->offset.z(),
248                 0.0, 0.0, 0.0 );
249     e->terra_transform->setTransform( &sgcoord );
250     terrain->addKid( e->terra_transform );
251
252     e->lights_transform = NULL;
253     /* uncomment this section for testing ground lights */
254     ssgLeaf *lights = gen_lights( light_pts );
255     if ( lights ) {
256         e->lights_transform = new ssgTransform;
257         e->lights_range = new ssgRangeSelector;
258         e->lights_range->addKid( lights );
259         e->lights_transform->addKid( e->lights_range );
260         e->lights_transform->setTransform( &sgcoord );
261         ground->addKid( e->lights_transform );
262     }
263     /* end of ground light section */
264 }
265
266
267 // Ensure at least one entry is free in the cache
268 void FGNewCache::make_space() {
269     FG_LOG( FG_TERRAIN, FG_INFO, "Make space in cache" );
270
271     
272     cout << "cache size = " << tile_cache.size() << endl;
273     cout << "max size = " << max_cache_size << endl;
274
275     if ( (int)tile_cache.size() < max_cache_size ) {
276         // space in the cache, return
277         return;
278     }
279
280     while ( (int)tile_cache.size() >= max_cache_size ) {
281         sgdVec3 abs_view_pos;
282         float dist;
283         float max_dist = 0.0;
284         int max_index = -1;
285
286         // we need to free the furthest entry
287         tile_map_iterator current = tile_cache.begin();
288         tile_map_iterator end = tile_cache.end();
289     
290         for ( ; current != end; ++current ) {
291             long index = current->first;
292             FGTileEntry *e = current->second;
293
294             // calculate approximate distance from view point
295             sgdCopyVec3( abs_view_pos,
296                          globals->get_current_view()->get_abs_view_pos() );
297
298             FG_LOG( FG_TERRAIN, FG_DEBUG, "DIST Abs view pos = " 
299                     << abs_view_pos[0] << ","
300                     << abs_view_pos[1] << ","
301                     << abs_view_pos[2] );
302             FG_LOG( FG_TERRAIN, FG_DEBUG,
303                     "    ref point = " << e->center );
304
305             sgdVec3 center;
306             sgdSetVec3( center, e->center.x(), e->center.y(), e->center.z() );
307             dist = sgdDistanceVec3( center, abs_view_pos );
308
309             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
310
311             if ( dist > max_dist ) {
312                 max_dist = dist;
313                 max_index = index;
314             }
315         }
316
317         // If we made it this far, then there were no open cache entries.
318         // We will instead free the furthest cache entry and return it's
319         // index.
320
321         if ( max_index >= 0 ) {
322             FG_LOG( FG_TERRAIN, FG_DEBUG, "    max_dist = " << max_dist );
323             FG_LOG( FG_TERRAIN, FG_DEBUG, "    index = " << max_index );
324             entry_free( max_index );
325         } else {
326             FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
327             exit( -1 );
328         }
329     }
330 }