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/fgstream.hxx>
42 #include <simgear/misc/fgpath.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 FG_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 FG_LOG( FG_TERRAIN, FG_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 FG_LOG( FG_TERRAIN, FG_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 FG_LOG( FG_TERRAIN, FG_INFO, " max cache size = "
95 FG_LOG( FG_TERRAIN, FG_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 cout << "clearing " << index << endl;
104 FGTileEntry *e = current->second;
105 e->tile_bucket.make_bad();
109 // and ... just in case we missed something ...
110 terrain->removeAllKids();
112 FG_LOG( FG_TERRAIN, FG_INFO, " done with init()" );
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 );
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( const FGBucket &b ) {
138 FGTileEntry *t = global_tile_cache.get_tile( b );
139 Point3D center = t->get_offset() + scenery.center;
141 double lon, lat, elev;
142 double w = b.get_width();
143 double h = b.get_height();
145 double area = b.get_width_m() * b.get_height_m();
146 int num = (int)(area / 1000000); // number of point lights to create
147 cout << "generating " << num << " lights" << endl;
153 // Allocate ssg structure
154 ssgVertexArray *vl = new ssgVertexArray( num );
155 ssgNormalArray *nl = NULL;
156 ssgTexCoordArray *tl = NULL;
157 ssgColourArray *cl = new ssgColourArray( 1 );
159 // default to white lights for now
161 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
164 for ( int i = 0; i < num; ++i ) {
165 lon = b.get_center_lon() - w * 0.5 + sg_random() * w;
166 lat = b.get_center_lat() - h * 0.5 + sg_random() * h;
168 Point3D geod = Point3D( lon * DEG_TO_RAD, lat * DEG_TO_RAD, 0.0);
169 Point3D tmp = sgGeodToCart( geod );
171 sgdSetVec3( cart, tmp.x(), tmp.y(), tmp.z() );
173 if ( ! global_tile_mgr.current_elev_ssg( cart, &elev ) ) {
176 // cout << " lon = " << lon << " lat = " << lat << " elev = " << elev
179 geod.setz( elev + 8.0 + sg_random() * 4);
180 tmp = sgGeodToCart( geod ) - center;
182 sgSetVec3( p, tmp.x(), tmp.y(), tmp.z() );
183 // cout << " x = " << cart[0] << " y = " << cart[1]
184 // << " z = " << cart[2] << endl;
190 new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
193 FGNewMat *newmat = material_lib.find( "LIGHTS" );
194 leaf->setState( newmat->get_state() );
200 // Fill in a tile cache entry with real data for the specified bucket
201 void FGNewCache::fill_in( const FGBucket& b ) {
202 FG_LOG( FG_TERRAIN, FG_INFO, "FILL IN CACHE ENTRY = " << b.gen_index() );
204 // clear out a distant entry in the cache if needed.
208 FGTileEntry *e = new FGTileEntry;
210 // register it in the cache
211 long tile_index = b.gen_index();
212 tile_cache[tile_index] = e;
214 // update the contents
215 e->center = Point3D( 0.0 );
216 if ( e->vec3_ptrs.size() || e->vec2_ptrs.size() || e->index_ptrs.size() ) {
217 FG_LOG( FG_TERRAIN, FG_ALERT,
218 "Attempting to overwrite existing or"
219 << " not properly freed leaf data." );
223 e->terra_transform = new ssgTransform;
224 e->terra_range = new ssgRangeSelector;
225 e->lights_transform = new ssgTransform;
226 e->lights_range = new ssgRangeSelector;
230 if ( globals->get_options()->get_fg_scenery() != "" ) {
231 tile_path.set( globals->get_options()->get_fg_scenery() );
233 tile_path.set( globals->get_options()->get_fg_root() );
234 tile_path.append( "Scenery" );
236 tile_path.append( b.gen_base_path() );
238 // Load the appropriate data file
239 FGPath tile_base = tile_path;
240 tile_base.append( b.gen_index_str() );
241 ssgBranch *new_tile = fgObjLoad( tile_base.str(), e, true );
243 if ( new_tile != NULL ) {
244 e->terra_range->addKid( new_tile );
247 // load custom objects
248 cout << "CUSTOM OBJECTS" << endl;
250 FGPath index_path = tile_path;
251 index_path.append( b.gen_index_str() );
252 index_path.concat( ".ind" );
254 cout << "Looking in " << index_path.str() << endl;
256 fg_gzifstream in( index_path.str() );
258 if ( in.is_open() ) {
261 while ( ! in.eof() ) {
264 #if defined ( macintosh ) || defined ( _MSC_VER )
269 cout << "token = " << token << " name = " << name << endl;
271 FGPath custom_path = tile_path;
272 custom_path.append( name );
273 ssgBranch *custom_obj = fgObjLoad( custom_path.str(), e, false );
274 if ( (new_tile != NULL) && (custom_obj != NULL) ) {
275 new_tile -> addKid( custom_obj );
280 e->terra_transform->addKid( e->terra_range );
282 // calculate initial tile offset
283 e->SetOffset( scenery.center );
285 sgSetCoord( &sgcoord,
286 e->offset.x(), e->offset.y(), e->offset.z(),
288 e->terra_transform->setTransform( &sgcoord );
289 terrain->addKid( e->terra_transform );
291 e->lights_transform = NULL;
292 /* uncomment this section for testing ground lights
293 ssgLeaf *lights = gen_lights( b );
295 e->lights_range->addKid( lights );
296 e->lights_transform->addKid( e->lights_range );
297 e->lights_transform->setTransform( &sgcoord );
298 ground->addKid( e->lights_transform );
304 // Ensure at least one entry is free in the cache
305 void FGNewCache::make_space() {
306 FG_LOG( FG_TERRAIN, FG_INFO, "Make space in cache" );
309 cout << "cache size = " << tile_cache.size() << endl;
310 cout << "max size = " << max_cache_size << endl;
312 if ( (int)tile_cache.size() < max_cache_size ) {
313 // space in the cache, return
317 while ( (int)tile_cache.size() >= max_cache_size ) {
318 sgdVec3 abs_view_pos;
320 float max_dist = 0.0;
323 // we need to free the furthest entry
324 tile_map_iterator current = tile_cache.begin();
325 tile_map_iterator end = tile_cache.end();
327 for ( ; current != end; ++current ) {
328 long index = current->first;
329 FGTileEntry *e = current->second;
331 // calculate approximate distance from view point
332 sgdCopyVec3( abs_view_pos,
333 globals->get_current_view()->get_abs_view_pos() );
335 FG_LOG( FG_TERRAIN, FG_DEBUG, "DIST Abs view pos = "
336 << abs_view_pos[0] << ","
337 << abs_view_pos[1] << ","
338 << abs_view_pos[2] );
339 FG_LOG( FG_TERRAIN, FG_DEBUG,
340 " ref point = " << e->center );
343 sgdSetVec3( center, e->center.x(), e->center.y(), e->center.z() );
344 dist = sgdDistanceVec3( center, abs_view_pos );
346 FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
348 if ( dist > max_dist ) {
354 // If we made it this far, then there were no open cache entries.
355 // We will instead free the furthest cache entry and return it's
358 if ( max_index >= 0 ) {
359 FG_LOG( FG_TERRAIN, FG_DEBUG, " max_dist = " << max_dist );
360 FG_LOG( FG_TERRAIN, FG_DEBUG, " index = " << max_index );
361 entry_free( max_index );
363 FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );