1998-05-20 20:53:53 +00:00
|
|
|
// tilecache.cxx -- routines to handle scenery tile caching
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started January 1998.
|
|
|
|
//
|
2000-06-23 00:30:04 +00:00
|
|
|
// Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
|
1998-05-20 20:53:53 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
|
1998-04-24 00:51:07 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
1998-04-03 22:09:02 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
1998-01-24 00:03:27 +00:00
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <GL/glut.h>
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/xgl/xgl.h>
|
1998-01-24 00:03:27 +00:00
|
|
|
|
2000-02-04 22:50:04 +00:00
|
|
|
#include <plib/ssg.h> // plib include
|
1999-06-28 00:02:52 +00:00
|
|
|
|
2000-02-16 23:01:03 +00:00
|
|
|
#include <simgear/bucket/newbucket.hxx>
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
|
|
|
#include <simgear/misc/fgstream.hxx>
|
|
|
|
#include <simgear/misc/fgpath.hxx>
|
2000-02-15 03:30:01 +00:00
|
|
|
|
1998-05-13 18:25:34 +00:00
|
|
|
#include <Main/options.hxx>
|
1998-04-22 13:21:26 +00:00
|
|
|
#include <Main/views.hxx>
|
1998-08-25 16:52:38 +00:00
|
|
|
#include <Objects/obj.hxx>
|
1999-11-03 21:01:59 +00:00
|
|
|
#include <Scenery/scenery.hxx> // for scenery.center
|
1998-04-22 13:21:26 +00:00
|
|
|
|
|
|
|
#include "tilecache.hxx"
|
1999-06-12 21:12:13 +00:00
|
|
|
#include "tileentry.hxx"
|
1998-01-24 00:03:27 +00:00
|
|
|
|
2000-05-24 06:10:01 +00:00
|
|
|
FG_USING_NAMESPACE(std);
|
1998-01-24 00:03:27 +00:00
|
|
|
|
1999-06-28 00:02:52 +00:00
|
|
|
// a cheesy hack (to be fixed later)
|
|
|
|
extern ssgBranch *terrain;
|
|
|
|
extern ssgEntity *penguin;
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// the tile cache
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache global_tile_cache;
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Constructor
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache::FGTileCache( void ) {
|
|
|
|
tile_cache.clear();
|
1998-05-20 20:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the tile cache subsystem
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache::init( void )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1998-01-24 00:03:27 +00:00
|
|
|
int i;
|
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
|
1998-01-24 00:03:27 +00:00
|
|
|
|
1999-06-12 21:12:13 +00:00
|
|
|
// expand cache if needed. For best results ... i.e. to avoid
|
|
|
|
// tile load problems and blank areas:
|
|
|
|
//
|
|
|
|
// target_cache_size >= (current.options.tile_diameter + 1) ** 2
|
|
|
|
//
|
1999-06-14 13:47:44 +00:00
|
|
|
int side = current_options.get_tile_diameter() + 2;
|
|
|
|
int target_cache_size = (side*side);
|
1999-06-12 21:12:13 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " target cache size = "
|
|
|
|
<< target_cache_size );
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " current cache size = "
|
|
|
|
<< tile_cache.size() );
|
|
|
|
FGTileEntry e;
|
1999-10-26 03:45:33 +00:00
|
|
|
e.mark_unused();
|
1999-10-27 00:52:25 +00:00
|
|
|
e.vec3_ptrs.clear();
|
|
|
|
e.vec2_ptrs.clear();
|
|
|
|
e.index_ptrs.clear();
|
1999-10-26 03:45:33 +00:00
|
|
|
|
1999-06-12 21:12:13 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " size of tile = "
|
|
|
|
<< sizeof( e ) );
|
|
|
|
if ( target_cache_size > (int)tile_cache.size() ) {
|
|
|
|
// FGTileEntry e;
|
|
|
|
int expansion_amt = target_cache_size - (int)tile_cache.size();
|
|
|
|
for ( i = 0; i < expansion_amt; ++i ) {
|
|
|
|
tile_cache.push_back( e );
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " expanding cache size = "
|
|
|
|
<< tile_cache.size() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " done expanding cache, size = "
|
|
|
|
<< tile_cache.size() );
|
|
|
|
|
|
|
|
for ( i = 0; i < (int)tile_cache.size(); i++ ) {
|
1999-06-13 05:58:02 +00:00
|
|
|
if ( !tile_cache[i].is_unused() ) {
|
1999-04-27 15:52:32 +00:00
|
|
|
entry_free(i);
|
|
|
|
}
|
1999-06-13 05:58:02 +00:00
|
|
|
tile_cache[i].mark_unused();
|
1999-05-06 21:14:06 +00:00
|
|
|
tile_cache[i].tile_bucket.make_bad();
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
1999-08-14 22:07:35 +00:00
|
|
|
|
|
|
|
// and ... just in case we missed something ...
|
|
|
|
terrain->removeAllKids();
|
|
|
|
|
2000-07-23 21:32:59 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_INFO, " done with init()" );
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Search for the specified "bucket" in the cache
|
1998-09-14 12:45:23 +00:00
|
|
|
int
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache::exists( const FGBucket& p )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1998-01-29 00:51:38 +00:00
|
|
|
int i;
|
|
|
|
|
1999-06-12 21:12:13 +00:00
|
|
|
for ( i = 0; i < (int)tile_cache.size(); i++ ) {
|
1998-11-09 23:40:46 +00:00
|
|
|
if ( tile_cache[i].tile_bucket == p ) {
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
|
|
|
"TILE EXISTS in cache ... index = " << i );
|
|
|
|
return( i );
|
1998-01-29 00:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-26 03:45:33 +00:00
|
|
|
#if 0
|
|
|
|
static void print_refs( ssgSelector *sel, ssgTransform *trans,
|
|
|
|
ssgRangeSelector *range)
|
|
|
|
{
|
|
|
|
cout << "selector -> " << sel->getRef()
|
|
|
|
<< " transform -> " << trans->getRef()
|
|
|
|
<< " range -> " << range->getRef() << endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Fill in a tile cache entry with real data for the specified bucket
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
1999-06-13 05:58:02 +00:00
|
|
|
FGTileCache::fill_in( int index, const FGBucket& p )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1999-09-01 18:52:31 +00:00
|
|
|
// cout << "FILL IN CACHE ENTRY = " << index << endl;
|
1999-07-25 01:52:36 +00:00
|
|
|
|
1999-10-26 03:45:33 +00:00
|
|
|
tile_cache[index].center = Point3D( 0.0 );
|
1999-10-27 00:52:25 +00:00
|
|
|
if ( tile_cache[index].vec3_ptrs.size() ||
|
|
|
|
tile_cache[index].vec2_ptrs.size() ||
|
|
|
|
tile_cache[index].index_ptrs.size() )
|
1999-10-26 03:45:33 +00:00
|
|
|
{
|
|
|
|
FG_LOG( FG_TERRAIN, FG_ALERT,
|
|
|
|
"Attempting to overwrite existing or"
|
|
|
|
<< " not properly freed leaf data." );
|
|
|
|
exit(-1);
|
|
|
|
}
|
1999-07-04 07:37:30 +00:00
|
|
|
|
1999-11-03 21:01:59 +00:00
|
|
|
tile_cache[index].select_ptr = new ssgSelector;
|
|
|
|
tile_cache[index].transform_ptr = new ssgTransform;
|
|
|
|
tile_cache[index].range_ptr = new ssgRangeSelector;
|
|
|
|
tile_cache[index].tile_bucket = p;
|
|
|
|
|
2000-07-14 21:19:18 +00:00
|
|
|
FGPath tile_path;
|
|
|
|
if ( current_options.get_fg_scenery() != "" ) {
|
|
|
|
tile_path.set( current_options.get_fg_scenery() );
|
|
|
|
} else {
|
|
|
|
tile_path.set( current_options.get_fg_root() );
|
|
|
|
tile_path.append( "Scenery" );
|
|
|
|
}
|
1999-04-27 19:27:45 +00:00
|
|
|
tile_path.append( p.gen_base_path() );
|
1999-11-03 21:01:59 +00:00
|
|
|
|
2000-06-23 00:30:04 +00:00
|
|
|
// Load the appropriate data file
|
1999-11-03 21:01:59 +00:00
|
|
|
FGPath tile_base = tile_path;
|
|
|
|
tile_base.append( p.gen_index_str() );
|
|
|
|
ssgBranch *new_tile = fgObjLoad( tile_base.str(), &tile_cache[index],
|
|
|
|
true );
|
1999-07-04 07:37:30 +00:00
|
|
|
|
1999-11-03 21:01:59 +00:00
|
|
|
if ( new_tile != NULL ) {
|
|
|
|
tile_cache[index].range_ptr->addKid( new_tile );
|
|
|
|
}
|
|
|
|
|
|
|
|
// load custom objects
|
|
|
|
cout << "CUSTOM OBJECTS" << endl;
|
|
|
|
|
|
|
|
FGPath index_path = tile_path;
|
|
|
|
index_path.append( p.gen_index_str() );
|
|
|
|
index_path.concat( ".ind" );
|
|
|
|
|
|
|
|
cout << "Looking in " << index_path.str() << endl;
|
|
|
|
|
|
|
|
fg_gzifstream in( index_path.str() );
|
|
|
|
|
|
|
|
if ( in.is_open() ) {
|
|
|
|
string token, name;
|
|
|
|
|
|
|
|
while ( ! in.eof() ) {
|
|
|
|
in >> token;
|
|
|
|
in >> name;
|
2000-09-10 00:04:50 +00:00
|
|
|
#if defined ( macintosh ) || defined ( _MSC_VER )
|
2000-02-10 23:37:56 +00:00
|
|
|
in >> ::skipws;
|
|
|
|
#else
|
1999-11-03 21:01:59 +00:00
|
|
|
in >> skipws;
|
2000-02-10 23:37:56 +00:00
|
|
|
#endif
|
1999-11-03 21:01:59 +00:00
|
|
|
cout << "token = " << token << " name = " << name << endl;
|
|
|
|
|
|
|
|
FGPath custom_path = tile_path;
|
|
|
|
custom_path.append( name );
|
|
|
|
ssgBranch *custom_obj = fgObjLoad( custom_path.str(),
|
|
|
|
&tile_cache[index], false );
|
|
|
|
if ( (new_tile != NULL) && (custom_obj != NULL) ) {
|
|
|
|
new_tile -> addKid( custom_obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-07-04 07:37:30 +00:00
|
|
|
|
|
|
|
tile_cache[index].transform_ptr->addKid( tile_cache[index].range_ptr );
|
1999-11-03 21:01:59 +00:00
|
|
|
|
|
|
|
// calculate initial tile offset
|
|
|
|
tile_cache[index].SetOffset( scenery.center );
|
|
|
|
sgCoord sgcoord;
|
|
|
|
sgSetCoord( &sgcoord,
|
|
|
|
tile_cache[index].offset.x(),
|
|
|
|
tile_cache[index].offset.y(), tile_cache[index].offset.z(),
|
|
|
|
0.0, 0.0, 0.0 );
|
|
|
|
tile_cache[index].transform_ptr->setTransform( &sgcoord );
|
|
|
|
|
1999-07-04 07:37:30 +00:00
|
|
|
tile_cache[index].select_ptr->addKid( tile_cache[index].transform_ptr );
|
|
|
|
terrain->addKid( tile_cache[index].select_ptr );
|
|
|
|
|
|
|
|
if ( tile_cache[index].is_scheduled_for_cache() ) {
|
1999-09-01 18:52:31 +00:00
|
|
|
// cout << "FOUND ONE SCHEDULED FOR CACHE" << endl;
|
1999-07-04 07:37:30 +00:00
|
|
|
// load, but not needed now so disable
|
|
|
|
tile_cache[index].mark_loaded();
|
|
|
|
tile_cache[index].ssg_disable();
|
|
|
|
tile_cache[index].select_ptr->select(0);
|
|
|
|
} else {
|
1999-09-01 18:52:31 +00:00
|
|
|
// cout << "FOUND ONE READY TO LOAD" << endl;
|
1999-07-04 07:37:30 +00:00
|
|
|
tile_cache[index].mark_loaded();
|
|
|
|
tile_cache[index].select_ptr->select(1);
|
|
|
|
}
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Free a tile cache entry
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
1999-07-25 01:52:36 +00:00
|
|
|
FGTileCache::entry_free( int cache_index )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1999-09-01 18:52:31 +00:00
|
|
|
// cout << "FREEING CACHE ENTRY = " << cache_index << endl;
|
1999-07-25 01:52:36 +00:00
|
|
|
tile_cache[cache_index].free_tile();
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Return index of next available slot in tile cache
|
1998-09-14 12:45:23 +00:00
|
|
|
int
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache::next_avail( void )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1999-07-25 01:52:36 +00:00
|
|
|
// Point3D delta;
|
|
|
|
Point3D abs_view_pos;
|
1998-01-29 00:51:38 +00:00
|
|
|
int i;
|
1999-07-25 01:52:36 +00:00
|
|
|
// float max, med, min, tmp;
|
1998-01-29 00:51:38 +00:00
|
|
|
float dist, max_dist;
|
|
|
|
int max_index;
|
|
|
|
|
|
|
|
max_dist = 0.0;
|
1999-06-14 13:47:44 +00:00
|
|
|
max_index = -1;
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1999-06-12 21:12:13 +00:00
|
|
|
for ( i = 0; i < (int)tile_cache.size(); i++ ) {
|
1999-06-14 13:47:44 +00:00
|
|
|
// only look at freeing NON-scheduled (i.e. ready to load
|
|
|
|
// cache entries. This assumes that the cache is always big
|
|
|
|
// enough for our tile radius!
|
|
|
|
|
1999-06-13 05:58:02 +00:00
|
|
|
if ( tile_cache[i].is_unused() ) {
|
1999-06-14 13:47:44 +00:00
|
|
|
// favor unused cache slots
|
1998-01-29 00:51:38 +00:00
|
|
|
return(i);
|
1999-07-25 01:52:36 +00:00
|
|
|
} else if ( tile_cache[i].is_loaded() || tile_cache[i].is_cached() ) {
|
1998-05-20 20:53:53 +00:00
|
|
|
// calculate approximate distance from view point
|
1998-12-09 18:50:12 +00:00
|
|
|
abs_view_pos = current_view.get_abs_view_pos();
|
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
1998-12-09 18:50:12 +00:00
|
|
|
"DIST Abs view pos = " << abs_view_pos );
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
1998-11-09 23:40:46 +00:00
|
|
|
" ref point = " << tile_cache[i].center );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1999-07-25 01:52:36 +00:00
|
|
|
/*
|
1998-12-09 18:50:12 +00:00
|
|
|
delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
|
|
|
|
delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
|
|
|
|
delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1998-10-16 00:51:46 +00:00
|
|
|
max = delta.x(); med = delta.y(); min = delta.z();
|
1998-01-29 00:51:38 +00:00
|
|
|
if ( max < med ) {
|
|
|
|
tmp = max; max = med; med = tmp;
|
|
|
|
}
|
|
|
|
if ( max < min ) {
|
|
|
|
tmp = max; max = min; min = tmp;
|
|
|
|
}
|
|
|
|
dist = max + (med + min) / 4;
|
1999-07-25 01:52:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
dist = tile_cache[i].center.distance3D( abs_view_pos );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
|
|
|
if ( dist > max_dist ) {
|
|
|
|
max_dist = dist;
|
|
|
|
max_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// If we made it this far, then there were no open cache entries.
|
|
|
|
// We will instead free the furthest cache entry and return it's
|
|
|
|
// index.
|
1999-07-25 01:52:36 +00:00
|
|
|
|
|
|
|
if ( max_index >=0 ) {
|
1999-09-01 18:52:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " max_dist = " << max_dist );
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " index = " << max_index );
|
1999-07-25 01:52:36 +00:00
|
|
|
entry_free( max_index );
|
|
|
|
return( max_index );
|
|
|
|
} else {
|
|
|
|
FG_LOG( FG_TERRAIN, FG_ALERT, "WHOOPS!!! Dying in next_avail()" );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2000-05-24 06:10:01 +00:00
|
|
|
|
|
|
|
// avoid a potential compiler warning
|
|
|
|
return -1;
|
1998-01-26 15:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Destructor
|
1999-06-12 21:12:13 +00:00
|
|
|
FGTileCache::~FGTileCache( void ) {
|
1998-05-20 20:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|