1 // newcache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started December 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
33 #include <simgear/xgl/xgl.h>
35 #include <plib/ssg.h> // plib include
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/sgstream.hxx>
42 #include <simgear/misc/sg_path.hxx>
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
50 #include "newcache.hxx"
51 #include "tileentry.hxx"
52 #include "tilemgr.hxx" // temp, need to delete later
54 SG_USING_NAMESPACE(std);
56 // a cheesy hack (to be fixed later)
57 extern ssgBranch *terrain;
58 extern ssgBranch *ground;
62 FGNewCache global_tile_cache;
66 FGNewCache::FGNewCache( void ) {
72 FGNewCache::~FGNewCache( void ) {
76 // Free a tile cache entry
77 void FGNewCache::entry_free( long cache_index ) {
78 SG_LOG( SG_TERRAIN, SG_INFO, "FREEING CACHE ENTRY = " << cache_index );
79 FGTileEntry *e = tile_cache[cache_index];
82 tile_cache.erase( cache_index );
86 // Initialize the tile cache subsystem
87 void FGNewCache::init( void ) {
88 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing the tile cache." );
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 SG_LOG( SG_TERRAIN, SG_INFO, " max cache size = "
95 SG_LOG( SG_TERRAIN, SG_INFO, " current cache size = "
96 << tile_cache.size() );
98 tile_map_iterator current = tile_cache.begin();
99 tile_map_iterator end = tile_cache.end();
101 for ( ; current != end; ++current ) {
102 long index = current->first;
103 SG_LOG( SG_TERRAIN, SG_DEBUG, "clearing " << index );
104 FGTileEntry *e = current->second;
105 e->tile_bucket.make_bad();
109 // and ... just in case we missed something ...
110 terrain->removeAllKids();
112 SG_LOG( SG_TERRAIN, SG_INFO, " done with init()" );
116 // Search for the specified "bucket" in the cache
117 bool FGNewCache::exists( const SGBucket& b ) {
118 long tile_index = b.gen_index();
119 tile_map_iterator it = tile_cache.find( tile_index );
121 return ( it != tile_cache.end() );
126 static void print_refs( ssgSelector *sel, ssgTransform *trans,
127 ssgRangeSelector *range)
129 cout << "selector -> " << sel->getRef()
130 << " transform -> " << trans->getRef()
131 << " range -> " << range->getRef() << endl;
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;
142 int size = lights->getNum() / inc;
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 );
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) );
160 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
161 float factor = sg_random();
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 );
174 // 5% chance of redish
175 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
183 new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
186 FGNewMat *newmat = material_lib.find( "LIGHTS" );
187 leaf->setState( newmat->get_state() );
193 // Fill in a tile cache entry with real data for the specified bucket
194 void FGNewCache::fill_in( const SGBucket& b ) {
195 SG_LOG( SG_TERRAIN, SG_INFO, "FILL IN CACHE ENTRY = " << b.gen_index() );
197 // clear out a distant entry in the cache if needed.
201 FGTileEntry *e = new FGTileEntry;
203 // register it in the cache
204 long tile_index = b.gen_index();
205 tile_cache[tile_index] = e;
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 SG_LOG( SG_TERRAIN, SG_ALERT,
211 "Attempting to overwrite existing or"
212 << " not properly freed leaf data." );
216 e->terra_transform = new ssgTransform;
217 e->terra_range = new ssgRangeSelector;
221 if ( globals->get_fg_scenery() != (string)"" ) {
222 tile_path.set( globals->get_fg_scenery() );
224 tile_path.set( globals->get_fg_root() );
225 tile_path.append( "Scenery" );
227 tile_path.append( b.gen_base_path() );
229 // fgObjLoad will generate ground lighting for us ...
230 ssgVertexArray *light_pts = new ssgVertexArray( 100 );
232 // Load the appropriate data file
233 SGPath tile_base = tile_path;
234 tile_base.append( b.gen_index_str() );
235 ssgBranch *new_tile = fgObjLoad( tile_base.str(), e, light_pts, true );
237 if ( new_tile != NULL ) {
238 e->terra_range->addKid( new_tile );
241 // load custom objects
242 SG_LOG( SG_TERRAIN, SG_DEBUG, "CUSTOM OBJECTS" );
244 SGPath index_path = tile_path;
245 index_path.append( b.gen_index_str() );
246 index_path.concat( ".ind" );
248 SG_LOG( SG_TERRAIN, SG_DEBUG, "Looking in " << index_path.str() );
250 sg_gzifstream in( index_path.str() );
252 if ( in.is_open() ) {
255 while ( ! in.eof() ) {
258 #if defined ( macintosh ) || defined ( _MSC_VER )
263 SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
264 << " name = " << name );
266 SGPath custom_path = tile_path;
267 custom_path.append( name );
268 ssgBranch *custom_obj =
269 fgObjLoad( custom_path.str(), e, NULL, false );
270 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
271 new_tile -> addKid( custom_obj );
276 e->terra_transform->addKid( e->terra_range );
278 // calculate initial tile offset
279 e->SetOffset( scenery.center );
281 sgSetCoord( &sgcoord,
282 e->offset.x(), e->offset.y(), e->offset.z(),
284 e->terra_transform->setTransform( &sgcoord );
285 terrain->addKid( e->terra_transform );
287 e->lights_transform = NULL;
288 e->lights_range = NULL;
289 /* uncomment this section for testing ground lights */
290 if ( light_pts->getNum() ) {
291 SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
292 e->lights_transform = new ssgTransform;
293 e->lights_range = new ssgRangeSelector;
294 e->lights_brightness = new ssgSelector;
297 lights = gen_lights( light_pts, 4, 0.7 );
298 e->lights_brightness->addKid( lights );
300 lights = gen_lights( light_pts, 2, 0.85 );
301 e->lights_brightness->addKid( lights );
303 lights = gen_lights( light_pts, 1, 1.0 );
304 e->lights_brightness->addKid( lights );
306 e->lights_range->addKid( e->lights_brightness );
307 e->lights_transform->addKid( e->lights_range );
308 e->lights_transform->setTransform( &sgcoord );
309 ground->addKid( e->lights_transform );
311 /* end of ground light section */
315 // Ensure at least one entry is free in the cache
316 void FGNewCache::make_space() {
317 SG_LOG( SG_TERRAIN, SG_INFO, "Make space in cache" );
320 SG_LOG( SG_TERRAIN, SG_DEBUG, "cache entries = " << tile_cache.size() );
321 SG_LOG( SG_TERRAIN, SG_INFO, "max size = " << max_cache_size );
323 if ( (int)tile_cache.size() < max_cache_size ) {
324 // space in the cache, return
328 while ( (int)tile_cache.size() >= max_cache_size ) {
329 sgdVec3 abs_view_pos;
331 float max_dist = 0.0;
334 // we need to free the furthest entry
335 tile_map_iterator current = tile_cache.begin();
336 tile_map_iterator end = tile_cache.end();
338 for ( ; current != end; ++current ) {
339 long index = current->first;
340 FGTileEntry *e = current->second;
342 // calculate approximate distance from view point
343 sgdCopyVec3( abs_view_pos,
344 globals->get_current_view()->get_abs_view_pos() );
346 SG_LOG( SG_TERRAIN, SG_DEBUG, "DIST Abs view pos = "
347 << abs_view_pos[0] << ","
348 << abs_view_pos[1] << ","
349 << abs_view_pos[2] );
350 SG_LOG( SG_TERRAIN, SG_DEBUG,
351 " ref point = " << e->center );
354 sgdSetVec3( center, e->center.x(), e->center.y(), e->center.z() );
355 dist = sgdDistanceVec3( center, abs_view_pos );
357 SG_LOG( SG_TERRAIN, SG_DEBUG, " distance = " << dist );
359 if ( dist > max_dist ) {
365 // If we made it this far, then there were no open cache entries.
366 // We will instead free the furthest cache entry and return it's
369 if ( max_index >= 0 ) {
370 SG_LOG( SG_TERRAIN, SG_DEBUG, " max_dist = " << max_dist );
371 SG_LOG( SG_TERRAIN, SG_DEBUG, " index = " << max_index );
372 entry_free( max_index );
374 SG_LOG( SG_TERRAIN, SG_ALERT, "WHOOPS!!! Dying in next_avail()" );