]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/newcache.cxx
Removed tilecache.cxx tilecache.hxx.
[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, int inc, float bright ) {
137     // generate a repeatable random seed
138     float *p1 = lights->get( 0 );
139     unsigned int *seed = (unsigned int *)p1;
140     sg_srandom( *seed );
141
142     int size = lights->getNum() / inc;
143
144     // Allocate ssg structure
145     ssgVertexArray   *vl = new ssgVertexArray( size + 1 );
146     ssgNormalArray   *nl = NULL;
147     ssgTexCoordArray *tl = NULL;
148     ssgColourArray   *cl = new ssgColourArray( size + 1 );
149
150     sgVec4 color;
151     for ( int i = 0; i < lights->getNum(); ++i ) {
152         // this loop is slightly less efficient than it otherwise
153         // could be, but we want a red light to always be red, and a
154         // yellow light to always be yellow, etc. so we are trying to
155         // preserve the random sequence.
156         float zombie = sg_random();
157         if ( i % inc == 0 ) {
158             vl->add( lights->get(i) );
159
160             // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
161             float factor = sg_random();
162             factor *= factor;
163
164             if ( zombie > 0.5 ) {
165                 // 50% chance of yellowish
166                 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
167             } else if ( zombie > 0.15 ) {
168                 // 35% chance of whitish
169                 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
170             } else if ( zombie > 0.05 ) {
171                 // 10% chance of orangish
172                 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
173             } else {
174                 // 5% chance of redish
175                 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
176             }
177             cl->add( color );
178         }
179     }
180
181     // create ssg leaf
182     ssgLeaf *leaf = 
183         new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
184
185     // assign state
186     FGNewMat *newmat = material_lib.find( "LIGHTS" );
187     leaf->setState( newmat->get_state() );
188
189     return leaf;
190 }
191
192
193 // Fill in a tile cache entry with real data for the specified bucket
194 void FGNewCache::fill_in( const FGBucket& b ) {
195     FG_LOG( FG_TERRAIN, FG_INFO, "FILL IN CACHE ENTRY = " << b.gen_index() );
196
197     // clear out a distant entry in the cache if needed.
198     make_space();
199
200     // create the entry
201     FGTileEntry *e = new FGTileEntry;
202
203     // register it in the cache
204     long tile_index = b.gen_index();
205     tile_cache[tile_index] = e;
206
207     // update the contents
208     e->center = Point3D( 0.0 );
209     if ( e->vec3_ptrs.size() || e->vec2_ptrs.size() || e->index_ptrs.size() ) {
210         FG_LOG( FG_TERRAIN, FG_ALERT, 
211                 "Attempting to overwrite existing or"
212                 << " not properly freed leaf data." );
213         exit(-1);
214     }
215
216     e->terra_transform = new ssgTransform;
217     e->terra_range = new ssgRangeSelector;
218     e->tile_bucket = b;
219
220     FGPath tile_path;
221     if ( globals->get_options()->get_fg_scenery() != "" ) {
222         tile_path.set( globals->get_options()->get_fg_scenery() );
223     } else {
224         tile_path.set( globals->get_options()->get_fg_root() );
225         tile_path.append( "Scenery" );
226     }
227     tile_path.append( b.gen_base_path() );
228     
229     // fgObjLoad will generate ground lighting for us ...
230     ssgVertexArray *light_pts = new ssgVertexArray( 100 );
231
232     // Load the appropriate data file
233     FGPath tile_base = tile_path;
234     tile_base.append( b.gen_index_str() );
235     ssgBranch *new_tile = fgObjLoad( tile_base.str(), e, light_pts, true );
236
237     if ( new_tile != NULL ) {
238         e->terra_range->addKid( new_tile );
239     }
240   
241     // load custom objects
242     cout << "CUSTOM OBJECTS" << endl;
243
244     FGPath index_path = tile_path;
245     index_path.append( b.gen_index_str() );
246     index_path.concat( ".ind" );
247
248     cout << "Looking in " << index_path.str() << endl;
249
250     fg_gzifstream in( index_path.str() );
251
252     if ( in.is_open() ) {
253         string token, name;
254
255         while ( ! in.eof() ) {
256             in >> token;
257             in >> name;
258 #if defined ( macintosh ) || defined ( _MSC_VER )
259             in >> ::skipws;
260 #else
261             in >> skipws;
262 #endif
263             cout << "token = " << token << " name = " << name << endl;
264
265             FGPath custom_path = tile_path;
266             custom_path.append( name );
267             ssgBranch *custom_obj = 
268                 fgObjLoad( custom_path.str(), e, NULL, false );
269             if ( (new_tile != NULL) && (custom_obj != NULL) ) {
270                 new_tile -> addKid( custom_obj );
271             }
272         }
273     }
274
275     e->terra_transform->addKid( e->terra_range );
276
277     // calculate initial tile offset
278     e->SetOffset( scenery.center );
279     sgCoord sgcoord;
280     sgSetCoord( &sgcoord,
281                 e->offset.x(), e->offset.y(), e->offset.z(),
282                 0.0, 0.0, 0.0 );
283     e->terra_transform->setTransform( &sgcoord );
284     terrain->addKid( e->terra_transform );
285
286     e->lights_transform = NULL;
287     e->lights_range = NULL;
288     /* uncomment this section for testing ground lights */
289     if ( light_pts->getNum() ) {
290         cout << "generating lights" << endl;
291         e->lights_transform = new ssgTransform;
292         e->lights_range = new ssgRangeSelector;
293         e->lights_brightness = new ssgSelector;
294         ssgLeaf *lights;
295
296         lights = gen_lights( light_pts, 4, 0.7 );
297         e->lights_brightness->addKid( lights );
298
299         lights = gen_lights( light_pts, 2, 0.85 );
300         e->lights_brightness->addKid( lights );
301
302         lights = gen_lights( light_pts, 1, 1.0 );
303         e->lights_brightness->addKid( lights );
304
305         e->lights_range->addKid( e->lights_brightness );
306         e->lights_transform->addKid( e->lights_range );
307         e->lights_transform->setTransform( &sgcoord );
308         ground->addKid( e->lights_transform );
309     }
310     /* end of ground light section */
311 }
312
313
314 // Ensure at least one entry is free in the cache
315 void FGNewCache::make_space() {
316     FG_LOG( FG_TERRAIN, FG_INFO, "Make space in cache" );
317
318     
319     cout << "cache size = " << tile_cache.size() << endl;
320     cout << "max size = " << max_cache_size << endl;
321
322     if ( (int)tile_cache.size() < max_cache_size ) {
323         // space in the cache, return
324         return;
325     }
326
327     while ( (int)tile_cache.size() >= max_cache_size ) {
328         sgdVec3 abs_view_pos;
329         float dist;
330         float max_dist = 0.0;
331         int max_index = -1;
332
333         // we need to free the furthest entry
334         tile_map_iterator current = tile_cache.begin();
335         tile_map_iterator end = tile_cache.end();
336     
337         for ( ; current != end; ++current ) {
338             long index = current->first;
339             FGTileEntry *e = current->second;
340
341             // calculate approximate distance from view point
342             sgdCopyVec3( abs_view_pos,
343                          globals->get_current_view()->get_abs_view_pos() );
344
345             FG_LOG( FG_TERRAIN, FG_DEBUG, "DIST Abs view pos = " 
346                     << abs_view_pos[0] << ","
347                     << abs_view_pos[1] << ","
348                     << abs_view_pos[2] );
349             FG_LOG( FG_TERRAIN, FG_DEBUG,
350                     "    ref point = " << e->center );
351
352             sgdVec3 center;
353             sgdSetVec3( center, e->center.x(), e->center.y(), e->center.z() );
354             dist = sgdDistanceVec3( center, abs_view_pos );
355
356             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
357
358             if ( dist > max_dist ) {
359                 max_dist = dist;
360                 max_index = index;
361             }
362         }
363
364         // If we made it this far, then there were no open cache entries.
365         // We will instead free the furthest cache entry and return it's
366         // index.
367
368         if ( max_index >= 0 ) {
369             FG_LOG( FG_TERRAIN, FG_DEBUG, "    max_dist = " << max_dist );
370             FG_LOG( FG_TERRAIN, FG_DEBUG, "    index = " << max_index );
371             entry_free( max_index );
372         } else {
373             FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
374             exit( -1 );
375         }
376     }
377 }